【问题标题】:TF 2.0: How to convert Tensor("mul_1:0", shape=(1000, 64), dtype=float32) to Numpy arrayTF 2.0:如何将 Tensor("mul_1:0", shape=(1000, 64), dtype=float32) 转换为 Numpy 数组
【发布时间】:2020-01-31 17:24:01
【问题描述】:

我使用 Tensorflow 2.0 并有一个张量 X 我想用 Numpy 处理。

如果我打印张量,我会得到以下信息:

print(X) = 
Tensor("mul_1:0", shape=(1000, 64), dtype=float32)

我尝试使用 X.numpy() 和 X.as_numpy() 将张量转换为 numpy 数组,这会引发以下错误:

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

如何访问张量的值?

编辑:

当我使用print(type(X)) 时,我得到:

<class 'tensorflow.python.framework.ops.Tensor'>

【问题讨论】:

  • 您是否禁用了急切执行?
  • @user3483203 没有。但我也尝试在X.numpy() 之前使用tf.executing_eagerly(),但没有解决问题。
  • 请给出一个独立的可重现示例,从问题中的信息中我们几乎无法判断。
  • 你的X 是什么?它是否存储任何值或者它是一个占位符并且 tf 2.0 不需要急切执行
  • @ShubhamShaswat X 是一个张量。 print(X) = Tensor("mul_1:0", shape=(1000, 64), dtype=float32)。 TF 2.0 中没有占位符,对吧?

标签: python numpy tensorflow tensorflow2.0


【解决方案1】:

您可以尝试在 Google Colab 环境 中重现您的问题和/或尝试更新您的 TensorFlow 版本到最新 strong>(截至目前为 2.1.0)。

这是您按预期成功执行的场景的模拟:

%tensorflow_version 2.x  
import tensorflow as tf  # TensorFlow 2.1.0

a  = tf.random.normal((5,5), seed = 26)  # Seed for Reproducibility
b  = tf.random.normal((5,5), seed = 26)

tf.linalg.matmul(a,b)  # Returns : <tf.Tensor: shape=(5, 5), dtype=float32, numpy= array([[-9.3171215e+00,  ...... 5.1605763e+00, 9.6334761e-01]], dtype=float32)>
tf.matmul(a,b)  # Returns value same as before

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 2018-04-25
    • 1970-01-01
    • 2020-06-09
    • 2019-09-17
    • 2019-10-23
    • 2019-07-18
    • 2019-11-24
    相关资源
    最近更新 更多