eager execution on tensorflow2.3.0

196 views Asked by At

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)

1

There are 1 answers

2
Poe Dator On

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)