繁体   English   中英

Tensorflow 2.0 使用 tf.compat.v1.disable_eager_execution() 时无法将张量转换为 numpy 数组

[英]Tensorflow 2.0 Cannot convert tensor to numpy array when using tf.compat.v1.disable_eager_execution()

我构建了一个网络,它返回与输入大小相同的 numpy 数组。 然而,这个网络真的很慢,需要超过 10 秒来处理 12 张图像。

当我使用tf.compat.v1.disable_eager_execution() ,同一网络在不到 0.1 秒的时间内处理了 12 张图像。 但是,现在网络返回我无法转换为 numpy 数组的张量。 这是我执行print(output)时网络的print(output)

Tensor("mul_20:0", shape=(12, 224, 224, 3), dtype=float32)

我试过output.numpy()并得到以下错误:

AttributeError: 'Tensor' object has no attribute 'numpy'

我能做些什么来解决这个问题?

当您使用tf.Tensor.numpy时,您只能使用tf.Tensor.numpy 当 Eager Execution 被禁用时,您的所有操作都只是在构建一个图。 您需要使用 Session 来实际运行它并获得结果。

with tf.compat.v1.Session() as sess:
    result = sess.run(output)
    print(result)  # result is a numpy array

https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM