我是Tensorflow的新手,我经历了有关MNIST(https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html)的初学者教程。 它可以很好地处理给定的数据,但当我尝试使用自己的数据时会失败。 这里就是我试图使网络学习加法(I,J)实现的例子 - >(I + J):未能在初学者教程中使用自定义数据
def generateData(n, r=100) :
inputData = []
outputData = []
for k in range(n):
i, j = random.randrange(0, r), random.randrange(0, r)
inputData.append([i, j])
outputData.append([i + j])
return inputData, outputData
x = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([2, 1]))
b = tf.Variable(tf.zeros([1]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 1])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(10):
batch_xs, batch_ys = generateData(10)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
batch_xs, batch_ys = generateData(10)
print(sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}))
feed_dict = {x: batch_xs}
classification = sess.run(y, feed_dict)
print(classification)
其结果是,我得到1.0准确性和向量[1.0]进行分类。 100%的准确性是可能的,因为模型非常简单,但预测显然不是。事实上,如果我用随机数替换生成的输出数据i + j,我会得到完全相同的结果。在这种情况下,我不可能有1.0的准确性。 这就好像网络没有学到任何东西。 问题在哪里?