0

I'm doing a beginner course on TensorFlow. The version I have installed is 2.3.0. I have had problems with eager execution since the course's TensorFlow version is different from the one I have installed. Does anyone know how I can perform an eager execution?

As an example,

    import tensorflow.compat.v1 as tf 
        
        
    x = tf.constant([3,5,7])
    y = tf.constant([1,2,3])
    z1 = tf.add(x,y)
    z2 = x*y
    z3 = z2-z1
        
    print(z2)
       
    with tf.compat.v1.Session() as sess:
        a1, a2, a3 = sess.run([z1,z2,z3])
        print(a1)
        print(a2)
        print(a3)


where I get as the output for the eager execution Tensor("mul_6:0", shape=(3,), dtype=int32)

Poe Dator
  • 4,535
  • 2
  • 14
  • 35
  • 2
    Eager execution is on by default in tensorflow 2.x. If you provide a reproducible, minimal example, it will be much easier for stackoverflow users to helpful answers. – jkr Oct 22 '20 at 14:50
  • Hi jakub, thanks for your recommendation. I included the example and only the output for eager execution – Alex Delarge Oct 22 '20 at 15:05

1 Answers1

3

if you want to have eager execution - import tf the regular way, not tensorflow.compat.v1. Then there is no need to use session at all. just enter formulas and print results:

import tensorflow as tf

x = tf.constant([3,5,7])
y = tf.constant([1,2,3])
z1 = tf.add(x,y)
z2 = x*y
z3 = z2-z1

print(z1)
print(z2)
print(z3)

tf.Tensor([ 4  7 10], shape=(3,), dtype=int32)
tf.Tensor([ 3 10 21], shape=(3,), dtype=int32)
tf.Tensor([-1  3 11], shape=(3,), dtype=int32)
Poe Dator
  • 4,535
  • 2
  • 14
  • 35
  • Hi @Poe, the output that I get with your code, in my version of tensorflow2.3.0 is Tensor("Add_9:0", shape=(3,), dtype=int32) Tensor("mul_9:0", shape=(3,), dtype=int32) Tensor("sub_10:0", shape=(3,), dtype=int32) – Alex Delarge Oct 22 '20 at 15:34
  • 1
    make sure that you import tf as `import tensorflow as tf` and not `import tensorflow.compat.v1 as tf `. Otherwise you get no eager execution enabled. – Poe Dator Oct 22 '20 at 20:42