Tensorflow Tutorial - Part 2

This code is provided as supplementary material of the lecture Machine Learning and Optimization in Communications (MLOC).

This code illustrates

  • eager execution scheme of Tensorflow for easier debugging

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()


WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:tensorflow:From C:\Users\schmalen\Anaconda3\envs\Lecture_MLOC\lib\site-packages\tensorflow_core\python\compat\v2_compat.py:88: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING:tensorflow:From C:\Users\schmalen\Anaconda3\envs\Lecture_MLOC\lib\site-packages\tensorflow_core\python\compat\v2_compat.py:88: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term

Check if eager execution is enable


In [2]:
tf.executing_eagerly()


Out[2]:
True

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)


tf.Tensor([ 5. 12. 21.], shape=(3,), dtype=float32)

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))


tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([4 6], shape=(2,), dtype=int32)
tf.Tensor(25, shape=(), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor(13, shape=(), dtype=int32)

Tensorflow as support for numpy a can convert numpy arrays automatically


In [5]:
tensor = tf.multiply(np.ones([3,3]), 42)
print(tensor)


tf.Tensor(
[[42. 42. 42.]
 [42. 42. 42.]
 [42. 42. 42.]], shape=(3, 3), dtype=float64)

You can also use numpy functions to operate on tensors and internally, the functions are converted adequately


In [6]:
print(np.add(tensor, 1))


[[43. 43. 43.]
 [43. 43. 43.]
 [43. 43. 43.]]

You can explicitly convert a TensorFlow tensor to a numpy array using tne .numpy() method


In [7]:
print(tensor.numpy())


[[42. 42. 42.]
 [42. 42. 42.]
 [42. 42. 42.]]

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.