我刚刚开始学习ML和TensorFlow,而我选择的第一个示例是数字识别(https://www.tensorflow.org/versions/r1.0/get_started/mnist/beginners) 我已经理解了这个概念,但是我想对其做一个简单的修改:它检查是否为数字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]))
但是没有用。