Python TensorFlow-从MNIST中识别特定数字(如7)

Python TensorFlow-从MNIST中识别特定数字(如7),python,tensorflow,flow,mnist,tensor,Python,Tensorflow,Flow,Mnist,Tensor,我刚刚开始学习ML和TensorFlow,我学的第一个例子是数字识别() 我已经理解了这个概念,但是我想对它做一个简单的修改:它检查是否是数字7。 下面是我当前的所有数字识别代码(python 3)。准确度为90%: from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) import tensorflow

我刚刚开始学习ML和TensorFlow,我学的第一个例子是数字识别() 我已经理解了这个概念,但是我想对它做一个简单的修改:它检查是否是数字7。 下面是我当前的所有数字识别代码(python 3)。准确度为90%:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)

import tensorflow as tf

#Inputs
x = tf.placeholder(tf.float32,[None,784])

#layer
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,w)+b)

#expected result
y_ = tf.placeholder(tf.float32, [None, 10])

#cost function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

#gradient descent
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)

#begin session
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(1000):
    #utilização de batches para economia de computação (treino estocástico)
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

# Test
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))
我的想法是减少输出层,因此看起来如下:

x = tf.placeholder(tf.float32,[None,784])
w = tf.Variable(tf.zeros([784,1]))
b = tf.Variable(tf.zeros([1]))

y = tf.nn.softmax(tf.matmul(x,w)+b)

y_ = tf.placeholder(tf.float32, [None, 1])
所以只有一个神经元的输出。我的问题是我的培训标签。我不知道如何只使用mnist.train.label的第7个字段进行训练。我做了一些类似的测试

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_[7] * tf.log(y), reduction_indices=[1]))

但它不起作用。

您必须更改标签。将所有的7设为“1”,其他任何数字设为“0”(或相反)。您可以使用“and”或其他逻辑函数动态地执行此操作。或者使用非TensorFlow python编辑数据集可能更简单。这很聪明。。。我会尝试一下,稍后再给出反馈。多谢各位