In [1]:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# Enable eager execution with tensorflow to allow for simpler debugging
tf.enable_eager_execution()
Check if eager execution is enable
In [2]:
tf.executing_eagerly()
Out[2]:
In contrast to the previous example, we now use the eager execution to directly manipulate tensors without requiring a graph first. Here we can see how eager execution works on an example
In [3]:
a = tf.constant([1,2,3], dtype=tf.float32)
b = tf.constant([5,6,7], dtype=tf.float32)
result = tf.multiply(a,b)
print(result)
In [4]:
print(tf.add(1, 2))
print(tf.add([1, 2], [3, 4]))
print(tf.square(5))
print(tf.reduce_sum([1, 2, 3]))
print(tf.square(2) + tf.square(3))
Tensorflow as support for numpy a can convert numpy arrays automatically
In [5]:
tensor = tf.multiply(np.ones([3,3]), 42)
print(tensor)
You can also use numpy functions to operate on tensors and internally, the functions are converted adequately
In [6]:
print(np.add(tensor, 1))
You can explicitly convert a TensorFlow tensor to a numpy array using tne .numpy()
method
In [7]:
print(tensor.numpy())
This is just the beginning of what eager executions can do. In the future, TensorFlow will switch completely to eager executions (as of version 2.0). As V2.0 is not released yet, and production models will rely on graphs, we do not focus on eager execution here.