我正在看TensorFlow“ MNIST对于ML初学者”教程,我想在每个训练步骤之后打印出训练损失。
我的训练循环目前看起来像这样:
for i in range(100): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
现在,train_step定义为:
train_step
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
cross_entropy我要打印的损失在哪里:
cross_entropy
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
一种打印方式是cross_entropy在训练循环中显式计算:
for i in range(100): batch_xs, batch_ys = mnist.train.next_batch(100) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) print 'loss = ' + str(cross_entropy) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
我现在有两个问题:
鉴于cross_entropy已经在期间进行了计算sess.run(train_step, ...),因此将其计算两次效率低下,这需要所有训练数据的前向通过次数的两倍。有没有一种方法可以访问在cross_entropy计算期间的value sess.run(train_step, ...)?
sess.run(train_step, ...)
我如何打印tf.Variable?使用str(cross_entropy)给我一个错误…
tf.Variable
str(cross_entropy)
谢谢!
您可以cross_entropy通过将的值添加到的参数列表中来获取sess.run(...)。例如,您的for-loop可以重写如下:
sess.run(...)
for
for i in range(100): batch_xs, batch_ys = mnist.train.next_batch(100) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) _, loss_val = sess.run([train_step, cross_entropy], feed_dict={x: batch_xs, y_: batch_ys}) print 'loss = ' + loss_val
可以使用相同的方法来打印变量的当前值。假设,除了的值cross_entropy,您还想打印被tf.Variable调用的值W,您可以执行以下操作:
W
for i in range(100): batch_xs, batch_ys = mnist.train.next_batch(100) cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) _, loss_val, W_val = sess.run([train_step, cross_entropy, W], feed_dict={x: batch_xs, y_: batch_ys}) print 'loss = %s' % loss_val print 'W = %s' % W_val