简体   繁体   中英

TF 2.0: How to convert Tensor("mul_1:0", shape=(1000, 64), dtype=float32) to Numpy array

I use Tensorflow 2.0 and have a tensor X that I would like to process with Numpy.

If I print the tensor I get the following:

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

I tried to convert the tensor to a numpy array using X.numpy() and X.as_numpy() which throws the following errors:

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

How can I access the tensor's values?

EDIT:

When I use print(type(X)) I get:

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

You could try reproducing your problem in Google Colab Environment and/or Try Updating your TensorFlow version to the latest (2.1.0 as of now).

Here is a simulation of your scenario successfully executed as intended:

%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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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