Pytorch和Tensorflow在相同資料規模規模下的降維KNN(K-NearestNeighbor)演算法中的運算速度對比
阿新 • • 發佈:2021-02-09
技術標籤:機器學習深度學習tensorflowpytorch
Pytorch和Tensorflow在相同資料規模規模下的降維KNN(K-NearestNeighbor)演算法中的運算速度對比
今天介紹一下比較簡單的機器學習演算法KNN演算法。它最初由 Cover和Hart於1968年提出,是一個理論上比較成熟的方法,也是最簡單的機器學習演算法之一。
KNN演算法基本原理
該方法的思路非常簡單直觀:離已知的某一個類中的所有點的平均距離距離最小的,就將這個點歸為該類。
該方法的不足之處是計算量較大,因為對每一個待分類的文字都要計算它到全體已知樣本的距離,會佔用很大的時間與記憶體。
KNN分類演算法包括以下4個步驟:
①準備資料,對資料進行預處理
②計算測試樣本點(也就是待分類點)到其他每個樣本點的距離
③對每個距離進行排序,然後選擇出距離最小的點
④將測試點歸類到這個最小距離的類中。
本文中用的是L2距離實現:
L
2
=
(
x
2
−
x
1
)
2
+
(
y
2
−
y
1
)
2
L2=\sqrt{(x_2-x1)^2+(y_2-y_1)^2}
L2=(x2−x1)2+(y2−y1)2
程式碼實現(使用的CPU為銳龍R5-3500U)
MNIST資料集下
pytorch實現
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import time
#資料準備
train_dataset = torchvision.datasets.MNIST(root='/data',
train=True,
transform=transforms.ToTensor(),
download=True )
test_dataset = torchvision.datasets.MNIST(root='/data',
train=False,
transform=transforms.ToTensor())
x_train = train_dataset.data.float()#訓練資料
y_train = train_dataset.targets#訓練資料的標籤
x_test = test_dataset.data.float()#測試資料
y_test = test_dataset.targets#測試資料的標籤
accuracy=0
number=1000
#執行網路
start = time.perf_counter()
for epoch in range(number):
# 計算L2距離
distance = torch.sqrt(torch.sum(torch.sum(torch.pow(x_train-x_test[epoch],2),dim=2),dim=1))
# 獲取最小距離的索引
nn_index = torch.argmin(distance, 0)
if epoch ==0:
print("Test", epoch, "Prediction:", y_train[nn_index].item(),"True Class:", y_test[epoch].item())
# 計算精確度
if y_train[nn_index].item() == y_test[epoch].item():
accuracy += 1/number
elapsed = (time.perf_counter() - start)#計算所用時間
print("Time used:",elapsed)#輸出結果
print(f'accuracy:{accuracy}')
Tensorflow實現
import tensorflow.compat.v1 as tf
import numpy as np
from tensorflow.keras.datasets import mnist
import time
import matplotlib.pyplot as plt
from sklearn import datasets
#準備資料
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 輸入佔位符
tf.compat.v1.disable_eager_execution()
xtr = tf.placeholder("float32", [None,25,25])
xte = tf.placeholder("float32", [25,25])
# 計算L2距離
distance = tf.sqrt(tf.reduce_sum(tf.reduce_sum(tf.pow(tf.add(xtr, tf.negative(xte)),2),reduction_indices=1),reduction_indices=1))
# 獲取最小距離的索引
pred = tf.arg_min(distance, 0)
# 初始化變數
init = tf.global_variables_initializer()
number=1000
#分類精確度
accuracy = 0
# 執行會話,訓練模型
start = time.perf_counter()
with tf.Session() as sess:
# 執行初始化
sess.run(init)
# 遍歷測試資料
for i in range(number):
# 獲取當前樣本的最近鄰索引
nn_index = sess.run(pred, feed_dict={xtr: x_train, xte: x_test[i]}) #向佔位符傳入訓練資料
#最近鄰分類標籤與真實標籤比較
if i %50 == 0:
print("Test", i, "Prediction:", y_train[nn_index], \"True Class:", y_test[i])
# 計算精確度
if y_train[nn_index] == y_test[i]:
accuracy += 1/number
print("Done!")
print(f"Accuracy:{accuracy}")
elapsed = (time.perf_counter()-start)
print(f'time use :{elapsed}')
對比可知,在執行KNN時,tensorflow在cpu狀態下比pytorch快,兩個框架的正確率不相上下。(不喜勿噴)