[英]Tensorflow 2.0 Cannot convert tensor to numpy array when using tf.compat.v1.disable_eager_execution()
I build a network which returns numpy arrays of the same size as the input.我构建了一个网络,它返回与输入大小相同的 numpy 数组。 However this network is really slow and needs more than 10 seconds to process 12 images.
然而,这个网络真的很慢,需要超过 10 秒来处理 12 张图像。
When I use tf.compat.v1.disable_eager_execution()
the same network processes 12 images in less than 0.1 seconds.当我使用
tf.compat.v1.disable_eager_execution()
,同一网络在不到 0.1 秒的时间内处理了 12 张图像。 Now, however, the network returns tensors that I can not convert to numpy arrays.但是,现在网络返回我无法转换为 numpy 数组的张量。 This is the output of the network when I do
print(output)
:这是我执行
print(output)
时网络的print(output)
:
Tensor("mul_20:0", shape=(12, 224, 224, 3), dtype=float32)
I tried output.numpy()
and get the following error:我试过
output.numpy()
并得到以下错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
What can I do to solve this problem?我能做些什么来解决这个问题?
You can only use tf.Tensor.numpy
when you are using eager execution.当您使用
tf.Tensor.numpy
时,您只能使用tf.Tensor.numpy
。 When eager execution is disabled, all of your operations are just constructing a graph.当 Eager Execution 被禁用时,您的所有操作都只是在构建一个图。 You need to use a Session to actually run it and get the result.
您需要使用 Session 来实际运行它并获得结果。
with tf.compat.v1.Session() as sess:
result = sess.run(output)
print(result) # result is a numpy array
Seehttps://www.tensorflow.org/api_docs/python/tf/compat/v1/Session见https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.