我正在研究数十年,并希望测试苗条的例子。当我命令./scripts/train_lenet_on_mnist.sh时,程序运行到eval_image_classifier给出一个类型错误,错误信息如下:
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so.8.0 locally
INFO:tensorflow:Scale of 0 disables regularizer.
INFO:tensorflow:Evaluating /tmp/lenet-model/model.ckpt-20002
INFO:tensorflow:Starting evaluation at 2016-11-09-02:55:57
I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties:
name: Quadro K5000
major: 3 minor: 0 memoryClockRate (GHz) 0.7055
pciBusID 0000:03:00.0
Total memory: 3.94GiB
Free memory: 3.61GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0
I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0: Y
I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Quadro K5000, pci bus id: 0000:03:00.0)
INFO:tensorflow:Executing eval ops
INFO:tensorflow:Executing eval_op 1/100
INFO:tensorflow:Error reported to Coordinator: <class 'TypeError'>, Fetch argument dict_values([<tf.Tensor 'accuracy/update_op:0' shape=() dtype=float32>, <tf.Tensor 'recall_at_5/update_op:0' shape=() dtype=float32>]) has invalid type <class 'dict_values'>, must be a string or Tensor. (Can not convert a dict_values into a Tensor or Operation.)
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 218, in init
fetch, allow_tensor=True, allow_operation=True))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2455, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2547, in _as_graph_element_locked
% (type(obj).name, types_str))
TypeError: Can not convert a dict_values into a Tensor or Operation.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "eval_image_classifier.py", line 191, in <module>
tf.app.run()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 30, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "eval_image_classifier.py", line 187, in main
variables_to_restore=variables_to_restore)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/evaluation.py", line 359, in evaluate_once
global_step=global_step)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/evaluation.py", line 260, in evaluation
sess.run(eval_op, eval_op_feed_dict)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 902, in _run
fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 358, in init
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 189, in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 222, in init
% (fetch, type(fetch), str(e)))
TypeError: Fetch argument dict_values([<tf.Tensor 'accuracy/update_op:0' shape=() dtype=float32>, <tf.Tensor 'recall_at_5/update_op:0' shape=() dtype=float32>]) has invalid type <class 'dict_values'>, must be a string or Tensor. (Can not convert a dict_values into a Tensor or Operation.)
我不知道程序发生了什么,我没有修改任何代码,只是从github下载代码包,以及数据和训练,这给出了正确的结果。有什么帮助吗?我在网上等。感谢
答案 0 :(得分:1)
问题是python2
和python3
的兼容性。由于我使用python3
进行解释,但词典中的键与p ython2
和python3
不同。
在Python2
中,只需在字典对象上调用keys()
即可返回您所期望的内容,但是,在Python3
中,keys()
不再返回list
,但是一个视图对象,因此可以避免TypeError,只需将dict_keys
对象转换为一个列表就可以保持兼容性,然后可以在Python2
和Python3
中将其编入索引。 。
我使用eval_image_classifier
编辑了eval_op=list(names_to_updates.values())
,然后它可以完美运行。
答案 1 :(得分:0)
eval_image_classifier.py的另一个python3更改是
for name, value in names_to_values.iteritems():
to
for name, value in names_to_values.items():