我只是编辑https://github.com/tensorflow/tensorflow/blob/r0.10/tensorflow/examples/tutorials/mnist/mnist_softmax.py以使用验证监视器启用日志记录
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Import data
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
sess = tf.InteractiveSession()
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
validation_metrics = {"accuracy": tf.contrib.metrics.streaming_accuracy,
"precision": tf.contrib.metrics.streaming_precision,
"recall": tf.contrib.metrics.streaming_recall}
validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(
mnist.test.images,
mnist.test.labels,
every_n_steps=50, metrics=validation_metrics,
early_stopping_metric="loss",
early_stopping_metric_minimize=True,
early_stopping_rounds=200)
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
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)
# Train
tf.initialize_all_variables().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_: batch_ys})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
但我很困惑如何在此程序中设置 validation_monitor 。我在 DNNClassfier 中学到了,validation_monitor以flowwing方式使用
# Fit model.
classifier.fit(x=training_set.data,
y=training_set.target,
steps=2000, monitors=[validation_monitor])
那么,我如何在softmax_classifer中使用validation_monitor?
答案 0 :(得分:1)
我认为没有一种简单的方法可以做到这一点,因为ValidationMonitor
是tf.contrib
的一部分,例如TensorFlow团队不支持的贡献代码。因此,除非您使用tf.contrib
中的某些更高级别的API(例如DNNClassfier
),否则您可能无法将ValidationMonitor
实例简单地传递给优化程序的minimize
方法。
我相信您的选择是:
DNNClassfier
fit
方法的实施方式,并通过在图表和会话中手动处理ValidationMonitor
实例来使用相同的方法。ValidationMonitor
的任何内容。