Tensorflow教程估计器无法将类型<type'dict'=“”>的对象转换为Tensor

时间:2017-08-22 04:02:12

标签: tensorflow

我正在运行教程代码“TF图层指南:在API r.1.3上构建卷积神经网络” https://www.tensorflow.org/tutorials/layers

我的代码在这里。 https://gist.github.com/Po-Hsuan-Huang/91e31d59fd3aa07f40272b75fe2a924d

错误显示:

runfile('/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py', wdir='/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST')
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
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_tf_random_seed': 1, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_save_checkpoints_steps': None, '_model_dir': '/tmp/mnist_convnet_model', '_save_summary_steps': 100}
Traceback (most recent call last):

  File "<ipython-input-1-c9b70e26f791>", line 1, in <module>
    runfile('/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py', wdir='/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST')

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)

  File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 129, in <module>
    main(None)

  File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 117, in main
    hooks=[logging_hook])

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 241, in train
    loss = self._train_model(input_fn=input_fn, hooks=hooks)

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 630, in _train_model
    model_fn_lib.ModeKeys.TRAIN)

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 615, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)

  File "/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 24, in cnn_model_fn
    input_layer = tf.reshape(features, [-1, 28, 28, 1])

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2619, in reshape
    name=name)

  File "/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 493, in apply_op
    raise err

TypeError: Failed to convert object of type <type 'dict'> to Tensor. Contents: {'x': <tf.Tensor 'random_shuffle_queue_DequeueMany:1' shape=(100, 784) dtype=float32>}. Consider casting elements to a supported type.

我追溯了一下,发现函数estimator._call_input_fn()没有使用参数&#39;模式&#39;完全没有能够创建包含功能和标签的元组。是教程需要修改,还是这个功能有问题。我不明白为什么mode在这里未被使用。

谢谢!

def _call_input_fn(self, input_fn, mode):
"""Calls the input function.

Args:
  input_fn: The input function.
  mode: ModeKeys

Returns:
  Either features or (features, labels) where features and labels are:
    features - `Tensor` or dictionary of string feature name to `Tensor`.
    labels - `Tensor` or dictionary of `Tensor` with labels.

Raises:
  ValueError: if input_fn takes invalid arguments.
"""
del mode  # unused
input_fn_args = util.fn_args(input_fn)
kwargs = {}
if 'params' in input_fn_args:
  kwargs['params'] = self.params
if 'config' in input_fn_args:
  kwargs['config'] = self.config
with ops.device('/cpu:0'):
  return input_fn(**kwargs)

1 个答案:

答案 0 :(得分:3)

你的要点实际上并不包含你的任何代码......无论哪种方式,从你的错误信息中我认为你只是错误地转录了教程中的一些代码。 您的错误日志表明您已

"/Users/pohsuanhuang/Documents/workspace/tensorflow_models/NMIST/cnn_mnist.py", line 24, in cnn_model_fn
    input_layer = tf.reshape(features, [-1, 28, 28, 1])

虽然教程有:

input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])