在TensorFlow中实现KNN的问题

我在TensorFlow中尝试实现K最近邻算法时遇到了困难。我认为要么是我忽略了某个错误,要么是做了一些非常糟糕的事情。

以下代码总是预测Mnist标签为0。

from __future__ import print_functionimport numpy as npimport tensorflow as tf# Import MNIST datafrom tensorflow.examples.tutorials.mnist import input_dataK = 4mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# In this example, we limit mnist dataXtr, Ytr = mnist.train.next_batch(55000)  # whole training setXte, Yte = mnist.test.next_batch(10000)  # whole test set# tf Graph Inputxtr = tf.placeholder("float", [None, 784])ytr = tf.placeholder("float", [None, 10])xte = tf.placeholder("float", [784])# Euclidean Distancedistance = tf.neg(tf.sqrt(tf.reduce_sum(tf.square(tf.sub(xtr, xte)), reduction_indices=1)))# Prediction: Get min distance neighborsvalues, indices = tf.nn.top_k(distance, k=K, sorted=False)nearest_neighbors = []for i in range(K):    nearest_neighbors.append(np.argmax(ytr[indices[i]]))sorted_neighbors, counts = np.unique(nearest_neighbors, return_counts=True)pred = tf.Variable(nearest_neighbors[np.argmax(counts)])# not works either# neighbors_tensor = tf.pack(nearest_neighbors)# y, idx, count = tf.unique_with_counts(neighbors_tensor)# pred = tf.slice(y, begin=[tf.arg_max(count, 0)], size=tf.constant([1], dtype=tf.int64))[0]accuracy = 0.# Initializing the variablesinit = tf.initialize_all_variables()# Launch the graphwith 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:", nn_index,              "True Class:", np.argmax(Yte[i]))        # Calculate accuracy        if nn_index == np.argmax(Yte[i]):            accuracy += 1. / len(Xte)    print("Done!")    print("Accuracy:", accuracy)

任何帮助都将不胜感激。


回答:

总的来说,在定义TensorFlow模型时使用numpy函数并不是一个好主意。这正是你的代码无法工作的原因。我只对你的代码做了两个改动。我用tf.argmax替换了np.argmax。我还删除了#This doesn't work either的注释。

这是完整的工作代码:

from __future__ import print_functionimport numpy as npimport tensorflow as tf# Import MNIST datafrom tensorflow.examples.tutorials.mnist import input_dataK = 4mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# In this example, we limit mnist dataXtr, Ytr = mnist.train.next_batch(55000)  # whole training setXte, Yte = mnist.test.next_batch(10000)  # whole test set# tf Graph Inputxtr = tf.placeholder("float", [None, 784])ytr = tf.placeholder("float", [None, 10])xte = tf.placeholder("float", [784])# Euclidean Distancedistance = tf.negative(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(xtr, xte)), reduction_indices=1)))# Prediction: Get min distance neighborsvalues, indices = tf.nn.top_k(distance, k=K, sorted=False)nearest_neighbors = []for i in range(K):    nearest_neighbors.append(tf.argmax(ytr[indices[i]], 0))neighbors_tensor = tf.stack(nearest_neighbors)y, idx, count = tf.unique_with_counts(neighbors_tensor)pred = tf.slice(y, begin=[tf.argmax(count, 0)], size=tf.constant([1], dtype=tf.int64))[0]accuracy = 0.# Initializing the variablesinit = tf.initialize_all_variables()# Launch the graphwith 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, ytr: Ytr, xte: Xte[i, :]})        # Get nearest neighbor class label and compare it to its true label        print("Test", i, "Prediction:", nn_index,             "True Class:", np.argmax(Yte[i]))        #Calculate accuracy        if nn_index == np.argmax(Yte[i]):            accuracy += 1. / len(Xte)    print("Done!")    print("Accuracy:", accuracy)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注