I am receiving different errors when trying to print a TensorFlow object.
import numpy as np
import tensorflow as tf
The versions of both TensorFlow and numpy are 2.6 and 1.19.5, respectively.
print("np version:", np.__version__)
print("tf version:" ,tf.version.VERSION)
print("eager is on? ", tf.executing_eagerly())
#np version: 1.19.5
#tf version: 2.6.0
#eager is on? True
Now, let me create a small array and turn it into a tf
object.
arr= [0,1.2,-0.8]
arr = tf.constant(arr, dtype = tf.float32)
When I use tf.print
or tf.compat.v1.print(arr)
, nothing happens. When I call numpy
, I do receive an error.
tf.compat.v1.print(arr.numpy())
AttributeError: 'Tensor' object has no attribute 'numpy'
The only thing that has worked so far is ;
with tf.compat.v1.Session() as sess: print(arr.eval())
#[ 0. 1.2 -0.8]
However, I would like to use numpy
since my goal is to print certain features of the network during the traning phase. For instance, if I want to print the learning rate, I call with tf.compat.v1.Session() as sess: print(model.optimizer.learning_rate.eval())
. Yet, it returns me another error.
'ExponentialDecay' object has no attribute 'eval'
I was able to use numpy
to print everything before, however, I updated both TensorFlow
and numpy
packages and now am facing so many incompatibilities. The worst thing is that I don't remember which versions I was using.
I followed every step explainedd in this post AttributeError: 'Tensor' object has no attribute 'numpy'. It did not help me.
CodePudding user response:
Following code gives me an output -
import numpy as np
import tensorflow as tf
print("np version:", np.__version__)
print("tf version:" ,tf.version.VERSION)
print("eager is on? ", tf.executing_eagerly())
tf.enable_eager_execution()
arr= [0,1.2,-0.8]
arr = tf.constant(arr, dtype = tf.float32)
tf.compat.v1.print(arr.numpy())
Output: array([ 0. , 1.2, -0.8], dtype=float32)
Did you add tf.enable_eager_execution()
?