返回信息流import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_actual = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y_predict = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict),reduction_indies=1))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
init = tf.initialize_all_variables()
with tf.Session() as sess:
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_actual: batch_ys})
if(i%100==0):
print "accuracy:",sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels})
这是一条镜像帖。来源:北邮人论坛 / python / #18448同步于 2017/7/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
【问题】新人刚接触python!问一下下面2版本的代码怎么改成3版
buptlemon
2017/7/25镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
if(i%100==0):可以写成
if i % 100 == 0:
其他的我没玩过tf,不懂
【 在 buptlemon 的大作中提到: 】
: 除了print函数,还有其他语法错误吗?
Python2的if也不需要加括号。实际上Python中的if和for,常见情况中,只有“刻意改变条件的表达时运算顺序”,以及“调用方法”两种情况会有小括号。
其他的我能想到常见的,就是print不一样,Py3中的str本质上是Py2的unicode类型有些地方需要处理,Py3默认`/`是数学除法,`//`是不留小数的floor除法。