0

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.

whitepanda
  • 471
  • 2
  • 12

1 Answers1

0

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() ?

a11apurva
  • 138
  • 10
  • Thanks! Yes, it was on, but I restarted the kernel and now even ````tf.print```` works fine. – whitepanda Sep 26 '21 at 20:03
  • However, what I intend to do is still not working. when I call ````tf.compat.v1.print(model.optimizer.learning_rate)```` , it prints````keras.optimizer_v2.learning_rate_schedule.ExponentialDecay object at 0x0000021BDCC520D0>```` – whitepanda Sep 26 '21 at 20:06
  • sorry I missed that part, can you please provide how you are creating the model? Maybe the complete code block until you hit the error. – a11apurva Sep 27 '21 at 07:05
  • No worries! Here's the solution I found: ````lr = model.optimizer._decayed_lr(tf.float32), print(lr)```` – whitepanda Sep 27 '21 at 12:25