【发布时间】:2020-03-20 02:45:03
【问题描述】:
我正在尝试将作为张量的 input_image 转换为 numpy 数组。按照已经回答的问题here 和其他几个建议使用 input_image.eval() 或等效的 sess.run() 进行此转换的问题,我做了同样的事情,但它会引发错误,并且显然需要 sess.run() 的 feed_dict 值。但由于在这里我不是试图运行依赖于未知值的操作,所以我认为这里不需要 feed_dict,因为我在这里所做的只是转换。
此外,为了检查,我还尝试使用相同的方法转换其正上方的 tf.constant([1,2,3]) 值,尽管其数据类型与 input_image 相同,但它仍成功编译。这是我的代码,它是较大脚本的一部分:
def call(self, x):
input_image = Input(shape=(None, None, 3))
print(input_image.shape)
print(type(tf.constant([1,2,3])))
print(type(input_image))
print(type(K.get_session().run(tf.constant([1,2,3]))))
print(type(K.get_session().run(input_image)))
这是错误:
(?, ?, ?, 3)
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call
return fn(*args)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn
target_list, run_metadata)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,?,?,3]
[[{{node input_1}}]]
[[input_1/_1051]]
(1) Invalid argument: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,?,?,3]
[[{{node input_1}}]]
0 successful operations.
0 derived errors ignored.
我想知道为什么前者会起作用而后者不会。
【问题讨论】:
标签: python-3.x numpy tensorflow keras tensor