Development of ML models using TensorFlow

TensorFlow is a machine learning framework that performs high-performance numerical computations. TensorFlow owes its popularity to its high quality and vast amount of documentation, its ability to easily serve models at scale in production environments, and the friendly interface to GPUs and TPUs.

TensorFlow, to facilitate the development and deployment of ML models, has many high-level APIs, including Keras, Eager Execution, and Estimators. These APIs are very useful in many contexts, but, in order to develop RL algorithms, we'll only use low-level APIs.

Now, let's code immediately using TensorFlow. The following lines of code execute the sum of the constants, a and b, created with tf.constant():

import tensorflow as tf

# create two constants: a and b
a = tf.constant(4)
b = tf.constant(3)

# perform a computation
c = a + b

# create a session
session = tf.Session()
# run the session. It computes the sum
res = session.run(c)
print(res)

A particularity of TensorFlow is the fact that it expresses all computations as a computational graph that has to first be defined and later executed. Only after execution will the results be available. In the following example, after the operation, c = a + b, c doesn't hold the end value. Indeed, if you print c before creating the session, you'll obtain the following:

>> Tensor("add:0", shape=(), dtype=int32)

This is the class of the c variable, not the result of the addition. 

Moreover, execution has to be done inside a session that is instantiated with tf.Session(). Then, to perform the computation, the operation has to be passed as input to the run function of the session just created. Thus, to actually compute the graph and consequently sum a and b, we need to create a session and pass c as an input to session.run:

session = tf.Session()
res = session.run(c)
print(res)

>> 7
If you are using Jupyter Notebook, make sure to reset the previous graph by running tf.reset_default_graph().
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset