1. 程式人生 > >TensorFlow學習筆記之原始碼分析(1)----最近演算法nearest_neighbor

TensorFlow學習筆記之原始碼分析(1)----最近演算法nearest_neighbor

import numpy as np
import tensorflow as tf

# Import MINST data
import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
#input_data.py  Dataset downloading Loading the entire dataset into numpy array:
#這裡主要是匯入資料,資料通過input_data.py已經下載到/tmp/data/目錄之下了,這裡下載資料的時候,需要提前用瀏覽器嘗試是否可以開啟
#http://yann.lecun.com/exdb/mnist/,如果打不開,下載資料階段會報錯。而且一旦資料下載中斷,需要將之前下載的未完成的資料清空,重新
#進行下載,否則會出現CRC Check錯誤。read_data_sets是input_data.py裡面的一個函式,主要是將資料解壓之後,放到對應的位置。
# In this example, we limit mnist data
Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200 for testing
#mnist.train.next_batch,其中train和next_batch都是在input_data.py裡定義好的資料項和函式。此處主要是取得一定數量的資料。
#next_batch
函式可以遍歷整個資料集,只返回所需的樣本資料集的一部分(為了節省記憶體和避免載入整個資料集)。 # Reshape images to 1D Xtr = np.reshape(Xtr, newshape=(-1, 28*28)) Xte = np.reshape(Xte, newshape=(-1, 28*28)) #將二維的影象資料一維化,利於後面的相加操作。 # tf Graph Input xtr = tf.placeholder("float", [None, 784]) xte = tf.placeholder("float", [784]) #設立兩個空的型別,並沒有給具體的資料。這也是為了基於這兩個型別,去實現部分的graph。 # Nearest Neighbor calculation using L1 Distance # Calculate L1 Distance distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.neg(xte))), reduction_indices=1) # Predict: Get min distance index (Nearest neighbor) pred = tf.arg_min(distance, 0) #最近鄰居演算法,算最近的距離的鄰居,並且獲取該鄰居的下標,這裡只是基於空的型別,實現的graph,並未進行真實的計算。 accuracy = 0. # Initializing the variables init = tf.initialize_all_variables() #初始化所有的變數和未分配數值的佔位符,這個過程是所有程式中必須做的,否則可能會讀出隨機數值。 # Launch the graph with tf.Session() as sess: sess.run(init) # loop over test data for i in range(len(Xte)): # Get nearest neighbor nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i,:]}) # Get nearest neighbor class label and compare it to its true label print "Test", i, "Prediction:", np.argmax(Ytr[nn_index]), "True Class:", np.argmax(Yte[i]) # Calculate accuracy if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]): accuracy += 1./len(Xte) print "Done!" print "Accuracy:", accuracy #for迴圈迭代計算每一個測試資料的預測值,並且和真正的值進行對比,並計算精確度。該演算法比較經典的是不需要提前訓練,直接在測試階段進行識別



相關API:

tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None)

Computes the sum of elements across dimensions of a tensor.

Reduces input_tensor along the dimensions given in reduction_indices. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in reduction_indices

. If keep_dims is true, the reduced dimensions are retained with length 1.

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.

For example:

# 'x' is [[1, 1, 1]
#         [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
tf.reduce_sum(x, [0, 1]) ==> 6
Args:
  • input_tensor: The tensor to reduce. Should have numeric type.
  • reduction_indices: The dimensions to reduce. If None (the default), reduces all dimensions.
  • keep_dims: If true, retains reduced dimensions with length 1.
  • name: A name for the operation (optional).
Returns:

The reduced tensor.

點評:這個API主要是降維使用,在這個例子中,將測試圖片和所有圖片相加後的二維矩陣,降為每個圖片只有一個最終結果的一維矩陣。