每100步tensorflow打印列精度

每100步tensorflow打印列精度,tensorflow,Tensorflow,我在这里遵循MNIST教程:。我想注销每100步的精度。我试图在cnn\u model\u fn中修改列车部件,但它不起作用 这是我的修改: if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) train_op = optimizer.minimize( loss=loss,

我在这里遵循MNIST教程:。我想注销每100步的精度。我试图在cnn\u model\u fn中修改列车部件,但它不起作用

这是我的修改:

if mode == tf.estimator.ModeKeys.TRAIN:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
    train_op = optimizer.minimize(
                loss=loss,
                global_step=tf.train.get_global_step())
    logging_hook = tf.train.LoggingTensorHook({"accuracy": accuracy}, every_n_iter=100)
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op, training_hooks=[logging_hook])
我定义了一个
精度
,正好在
if

accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])
但我犯了以下错误

Extracting MNIST-data/train-images-idx3-ubyte.gz
Extracting MNIST-data/train-labels-idx1-ubyte.gz
Extracting MNIST-data/t10k-images-idx3-ubyte.gz
Extracting MNIST-data/t10k-labels-idx1-ubyte.gz
2018-05-04 18:54:05.819366: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-04 18:54:05.819388: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-04 18:54:05.819396: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-05-04 18:54:05.819402: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-05-04 18:54:05.819408: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
INFO:tensorflow:Create CheckpointSaverHook.
Traceback (most recent call last):
  File "cnn_mnist.py", line 119, in <module>
    tf.app.run()
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "cnn_mnist.py", line 102, in main
    steps=2000)
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 217, in train
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 577, in _train_model
    config=config_pb2.ConfigProto(allow_soft_placement=True)) as mon_sess:
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 333, in MonitoredTrainingSession
    stop_grace_period_secs=stop_grace_period_secs)
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 627, in __init__
    stop_grace_period_secs=stop_grace_period_secs)
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 449, in __init__
    h.begin()
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 162, in begin
    for (tag, tensor) in self._tensors.items()}
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 162, in <dictcomp>
    for (tag, tensor) in self._tensors.items()}
  File "/Users/caitlinwen/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 688, in _as_graph_element
    "to current graph %s." % (obj, graph))
ValueError: Passed (<tf.Tensor 'accuracy/value:0' shape=() dtype=float32>, <tf.Tensor 'accuracy/update_op:0' shape=() dtype=float32>) should have graph attribute that is equal to current graph <tensorflow.python.framework.ops.Graph object at 0x18139a8310>.

模块
tf.metrics.accurity
返回定义的两个参数
accurity
update\u op
。 因此,您需要将代码更改为:

accuracy, update_op = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])
尝试:

accuracy = tf.compat.v1.metrics.accuracy(
            labels=labels,
            predictions= prediction )
accuracy = tf.compat.v1.metrics.accuracy(
            labels=labels,
            predictions= prediction )