TensorFlow K nearest neighbor algorithm

TensorFlow K nearest neighbor algorithm

Purpose

1. Mastering TensorFlow be KNN operation

2. grasp the principle of KNN algorithm

Principle

The basic principle of knn:

KNN classification is performed by calculating the distance between the different feature values.

The overall idea is: if a sample in feature space of the k most similar (i.e. nearest feature space) most of the samples belonging to a particular category, then the sample may also fall into this category. K is usually not an integer greater than 20. KNN algorithm, the selected neighbors are already correctly classified objects. The method in the classification decision based solely on the nearest one category or several samples to determine the category to be classified sample belongs.

KNN algorithm to solve the core problem is the K value is selected, it will directly affect the classification results. If you choose a larger value of K is equivalent to the prediction with a large field of training examples, it has the advantage to reduce the estimation error learning, but the drawback is the learning approximation error increases. If selecting a smaller value of K is equivalent to a predicted small training examples in the field of "learning" will reduce the approximation error, or similar training examples only closer to the input instance will act on the prediction result the problem at the same time bring the "learning" the estimation error increases, in other words, reducing the K value means that the overall model is complicated, prone to over-fitting;

Use tensorflow KNN algorithm for the overall design process is to calculate the FIG., And then run the session, during the execution of the computation graph, data of the whole process of relatively poor visibility. Above calculation accuracy and compare the results to predict the real tags and labels actually use numpy and python variables.

lab environment

Windows10

Python 3.6.0

Pycharm

TensorFlow

Content Experiments

Use TensorFlow operate K-nearest neighbor algorithm.

Experimental Procedure

1. Open Pycharm, right click and choose New => Python File, create a file named tf_KNN of Python.

 

2. Open tf_KNN.py document prepared KNN code.

Experimental required import module

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

3. Import the required experimental data

= input_data.read_data_sets MNIST ( "C: / the Users / 32016 / Desktop / Spark / deep learning algorithm part /" , one_hot = True )

 

4. Set the batch size of the training set and test set

Xtr,Ytr=mnist.train.next_batch(5000)
Xte,Yte=mnist.test.next_batch(200)

The configuration of FIG calculation, the function configuration placeholder placeholder variables xtr, xte, code is as follows:

xtr=tf.placeholder("float",[None,784])
xte=tf.placeholder("float",[784])

6. The distance between the data request, and takes the minimum value.

distance = tf.reduce_sum(tf.abs(tf.add(xtr,tf.negative(xte))),reduction_indices=1)
pred=tf.argmin(distance,0)

7. initialize all the variables

accuracy=0
init=tf.global_variables_initializer()

8. Use tf.Session () Create Session session object encapsulates the session status and control Tensorflow runtime.

with tf.Session() as sess:
    sess.run(init)

9. training model and predict its reserve ratio by test data.

for i in range(len(Xte)):
    nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
    print("Test", i , "Prediction:", np.argmax(Ytr[nn_index]), "True Class:", np.argmax(Yte[i]))
    if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
        accuracy += 1./len(Xte)
    print("Done!")
    print("accuacy:" ,accuracy)

10. The complete code:

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# Import test data required 
MNIST input_data.read_data_sets = ( " C: / the Users / 32016 / Desktop / Spark / deep learning algorithm part / " , one_hot = True)
 # Set the training set and test set batch size 
Xtr, Ytr = mnist.train.next_batch (5000 )
XTE, YTE = mnist.test.next_batch (200 is )
 # configuration calculation map, a function configuration placeholder placeholder variables XTR, XTE 
XTR = tf.placeholder ( " a float " , [None, 784 ])
XTE = tf.placeholder ( " a float " , [784 ])
 # To find the distance between the data, and takes the minimum value of 
distance = tf.reduce_sum (tf.abs (tf.add ( xtr, tf.negative (xte)) ), reduction_indices =. 1 )
Pred = tf.argmin (Distance, 0)
 # initializes all variables 
Accuracy = 0
the init = tf.global_variables_initializer ()
 # use tf.Session () Create Session session object encapsulates the session status and control Tensorflow runtime 
with tf.Session () as sess:
    sess.run(init)
    # Training model and predict its ready to use the test data rate 
    for i in the Range (len (XTE)):
        nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
        print("Test", i , "Prediction:", np.argmax(Ytr[nn_index]), "True Class:", np.argmax(Yte[i]))
        if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
            accuracy += 1./len(Xte)
        print("Done!")
        print("accuacy:" ,accuracy)

 

11. Run results:

 

 

 

 

Guess you like

Origin www.cnblogs.com/1gaoyu/p/12595643.html