I have an conda environment with Tensorflow 2.0.0-beta1 installed. However whenever I import tensorflow and attempt to enable eager execution I get the error :
AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'
The only code that I have run for this is:
import tensorflow as tf
print(tf.__version__)
tf.enable_eager_execution()
Is this an error with the tensorflow 2.0 beta module or an issue with my installation ?
In ternsorflow 2.0 the enable_eager_execution
method is moved to tf.compat.v1
module. The following works on tensorflow-2.0.0-beta1
tf.compat.v1.enable_eager_execution()
In tensorflow 2.0 the eager execution is enabled by default. You don't need to enable it in your program.
E.g
import tensorflow as tf
t = tf.constant([5.0])
Now you can directly view the value of tensor without using session object.
print(t)
# tf.Tensor([5.], shape=(1,), dtype=float32)
You can also change the tensor value to numpy array
numpy_array = t.numpy()
print(numpy_array)
# [5.]
You can also disable eager execution in tensorflow-2(Tested on tensorflow-2.0.0-beta1. This might not work on future versions.)
tf.compat.v1.disable_eager_execution()
t2 = tf.constant([5.0])
print(t2)
# Tensor("Const:0", shape=(1,), dtype=float32)
Calling numpy() method on tensor after eager execution is disabled throws an error
AttributeError: 'Tensor' object has no attribute 'numpy'
One issue you should consider while disabling the eager execution is, once the eager execution is disabled it cannot be enabled in the same program, because tf.enable_eager_execution
should be called at program startup and calling this method after disabling eager execution throws an error:
ValueError: tf.enable_eager_execution must be called at program startup.