Since TensorFlow 1 does not use eager execution by default, the results of the operations are not directly available. For instance, when summing two constants, the output object is an operation:
import tensorflow as tf1 # TensorFlow 1.13
a = tf1.constant([1,2,3])
b = tf1.constant([1,2,3])
c = a + b
print(c) # Prints <tf.Tensor 'add:0' shape=(3,) dtype=int32
To compute a result, you need to manually create tf1.Session. A session takes care of the following:
- Managing the memory
- Running operations on CPU or GPU
- Running on several machines if necessary
The most common way of using a session is through the with statement in Python. As with other unmanaged resources, the with statement guarantees that the session is properly closed after we use it. If the session is not closed, it may keep using memory. Sessions in TensorFlow 1 are, therefore, typically instantiated and used as follows:
with tf1.Session() as sess: result = sess.run(c)
print(result) # Prints array([2...