joyner_zhang
joyner_zhang
采纳率77.8%
2020-05-06 13:20 浏览 128

用Python实现MNIST字体识别语法错误

您好,我安装的是Python3.7,但我觉得TensorFlow官网上给的示例不是3.7版本的,导致我最后一行语法错误,想问一下:该怎么修改呢?

错误信息如下:

图片说明

代码如下:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder("float", [None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.compat.v1.Session()
sess.run(init)
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  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, "float"))
print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2020-05-06 13:50

    print (sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
    加上括号看看

    点赞 1 评论 复制链接分享

相关推荐