印刷列车精度每100步骤张量流量

时间:2018-05-04 23:55:23

标签: tensorflow

我在这里遵循MNIST教程:https://www.tensorflow.org/tutorials/layers。我想注销每100步的准确度。我尝试修改cnn_model_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])

我在accuracy

之上定义了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>.

我的完整代码是:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Imports
import numpy as np
import tensorflow as tf


# Our application logic will be added here
def cnn_model_fn(features, labels, mode):
    """Model function for CNN."""
    # Input Layer
    input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])

    # Convolutional Layer #1
    conv1 = tf.layers.conv2d(
            inputs=input_layer,
            filters=32,
            kernel_size=[5, 5],
            padding="same",
            activation=tf.nn.relu)

    # Pooling Layer #1
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

    # Convolutional Layer #2 and Pooling Layer #2
    conv2 = tf.layers.conv2d(
            inputs=pool1,
            filters=64,
            kernel_size=[5, 5],
            padding="same",
            activation=tf.nn.relu)
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

    # Dense Layer
    pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
    dropout = tf.layers.dropout(
            inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

    # Logits Layer
    logits = tf.layers.dense(inputs=dropout, units=10)

    predictions = {
            # Generate predictions (for PREDICT and EVAL mode)
            "classes": tf.argmax(input=logits, axis=1),
            # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
            # `logging_hook`.
            "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
    }

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    # Calculate Loss (for both TRAIN and EVAL modes)
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
    accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])

    # Configure the Training Op (for TRAIN mode)
    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])

    # Add evaluation metrics (for EVAL mode)
    eval_metric_ops = {
            "accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])}

    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

def main(unused_argv):
    # Load training and eval data
    mnist = tf.contrib.learn.datasets.load_dataset("mnist")
    train_data = mnist.train.images # Returns np.array
    train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
    eval_data = mnist.test.images # Returns np.array
    eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
    # Create the Estimator
    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")

    # Set up logging for predictions
    init = tf.global_variables_initializer()
    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Session() as sess:

        sess.run(init)

        # Train the model
        train_input_fn = tf.estimator.inputs.numpy_input_fn(
                x={"x": train_data},
                y=train_labels,
                batch_size=100,
                num_epochs=None,
                shuffle=True)
        mnist_classifier.train(
                input_fn=train_input_fn,
                steps=2000)

        # Evaluate the model and print results
        eval_input_fn = tf.estimator.inputs.numpy_input_fn(
                x={"x": eval_data},
                y=eval_labels,
                num_epochs=1,
                shuffle=False)
        eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
        print(eval_results)

        # merged = tf.summary.merge_all()
        # train_writer = tf.summary.FileWriter('./train', sess.graph)
        # test_writer = tf.summary.FileWriter('./test')
        # tf.global_variables_initializer().run()

if __name__ == "__main__":
    tf.app.run()

2 个答案:

答案 0 :(得分:2)

模块tf.metrics.accuracy返回定义为here的两个参数accuracyupdate_op。 因此,您需要将代码更改为:

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

答案 1 :(得分:0)

尝试:

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