打印TensorFlow中优化程序最小化的损失值

时间:2016-04-12 10:56:51

标签: tensorflow

我想打印优化器已经最小化的损失值。这是一个例子:

LEARNING_RATE = 0.0001
MOMENTUM = 0.999

mean_squared_error = tf.reduce_mean(tf.square(tf.sub(predictions, training_outputs)))
train_step = tf.train.MomentumOptimizer(LEARNING_RATE, MOMENTUM).minimize(mean_squared_error)

# Load data
features = ...
labels = ...

# Launch TensorFlow session
with tf.Session() as session:
    session.run(initialize)

    print("Begin training...")
    session.run(train_step, feed_dict={training_inputs: features, training_outputs: labels})
    print("Finished training! The mean squared error is: _____")

现在我已将mean_squared_error最小化,如何打印其最小化值?

1 个答案:

答案 0 :(得分:2)

可视化损失的最简单方法是创建它的标量摘要:

mean_squared_error = tf.reduce_mean(tf.square(tf.sub(predictions, training_outputs)))
loss_summ = tf.scalar_summary("loss", mean_squared_error)

然后,您在TensorFlow会话中创建一个编写器,并将摘要loss_summ添加到sess.run()调用。然后,您可以在mse_val中获取值并打印出来。

with tf.Session() as session:
    writer = tf.train.SummaryWriter("log", session.graph_def)
    session.run(initialize)

    print("Begin training...")
    _, mse_val, summ = session.run([train_step, mean_squared_error, loss_summ], feed_dict={training_inputs: features, training_outputs: labels})
    writer.add_summary(summ)
    print("Finished training! The mean squared error is: %f" % mse_val)

作为奖励,您甚至可以通过运行tensorboard --logdir log(阅读this tutorial了解更多详情)来可视化TensorBoard中损失的演变。

P.S:您的代码只运行1次迭代训练,您可能想要添加一个循环。