简体   繁体   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()

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/Sessionhttps://www.tensorflow.org/api_docs/python/tf/compat/v1/Session

暂无
暂无

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

相关问题 如何将张量从 import tensorflow.compat.v1 as tf 转换为 numpy 数组? - How to convert a tensor from import tensorflow.compat.v1 as tf to a numpy array? 将 tf.Tensor 转换为 numpy 数组,然后在不使用 eager_execution 的情况下将其保存为图像 - Convert tf.Tensor to numpy array and than save it as image in without eager_execution How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model(无法将符号 tf.Tensor 转换为 numpy 数组) - How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model (Cannot convert a symbolic tf.Tensor to a numpy array) tensorflow使用占位符vs tf.convert_to_tensor()将numpy数组传递给图形 - tensorflow pass numpy array to graph using placeholder vs tf.convert_to_tensor() Tensorflow2.0 - 如何将张量转换为 numpy() 数组 - Tensorflow2.0 - How to convert Tensor to numpy() array 将张量转换为 Tensorflow 中的 numpy 数组? - Convert a tensor to numpy array in Tensorflow? 使用 Tensorflow 2.0 和没有 Keras 的急切执行 - Using Tensorflow 2.0 and eager execution without Keras NotImplementedError:无法将符号张量转换为 tensorflow 中的 numpy 数组 - NotImplementedError: Cannot convert a symbolic Tensor to a numpy array in tensorflow TF 2.0:如何将 Tensor("mul_1:0", shape=(1000, 64), dtype=float32) 转换为 Numpy 数组 - TF 2.0: How to convert Tensor("mul_1:0", shape=(1000, 64), dtype=float32) to Numpy array AttributeError: 当在 Tensorflow federated 中使用多个特征输入时启用急切执行时,Tensor.op 毫无意义 - AttributeError: Tensor.op is meaningless when eager execution is enabled when using multiple feature inputs in Tensorflow federated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM