The programming model

A TensorFlow program is generally divided into three phases:

  • Construction of the computational graph
  • Running a session, which is performed for the operations defined in the graph
  • Resulting data collection and analysis

These main steps define the programming model in TensorFlow.

Consider the following example, in which we want to multiply two numbers.

import tensorflow as tf 
with tf.Session() as session:
x = tf.placeholder(tf.float32,[1],name="x")
y = tf.placeholder(tf.float32,[1],name="y")
z = tf.constant(2.0)
y = x * z
x_in = [100]
y_output = session.run(y,{x:x_in})
print(y_output)

Surely TensorFlow is not necessary to multiply two numbers; also the number of lines of the code for this simple operation is so many. However, the example wants to clarify how to structure any code, from the simplest as in this instance, to the most complex.

Furthermore, the example also contains some basic instructions will found in all the other examples given in the course of this book.

Note that, for this and the remaining chapters, we will provide all the source code with Python 2.7 computable. However, you will find all of them with Python 3.3+ compatible on the Packt repository.

The first line tells us that the TensorFlow library is imported as tf:

import tensorflow as tf

The TensorFlow operator will then be expressed by tf and the dot . and by the name of the operator to use. In the next line, we construct the object session, by means of the instruction tf.Session():

 with tf.Session() as session:

This object contains the computation graph, which as we said earlier, are the calculations to be carried out.

For our example, the computational graph will be the following:

A computational graph that represents multiplying the constant by 1.0 to an input array x

The following two lines define variables x and y, using the notion of a placeholder. Through a placeholder you may define both an input (such as the variable x of our example) and an output variable (such as the variable y):

x = tf.placeholder(tf.float32, [1], name='x') 
y = tf.placeholder(tf.float32, [1], name='y')

A placeholder is, therefore, an interface between the elements of the graph and the computational data of the problem; it allows us to create our operations and build our computation graph, without needing the data, but only a reference to it.

To define a data or tensor via the placeholder function three arguments are required; the first is the data type, the second argument is the shape of a placeholder, which is a single dimensional Tensor (https://www.tensorflow.org/versions/r0.8/api_docs/python/framework.html#Tensor) with one entry, and the third argument is a name, very useful for debugging and code analysis purposes.

The following statement defines a constant (=1.0) to which we give the name b:

     b = tf.constant(1.0)

So, we may introduce the model that we want to compute; it has as arguments the placeholder and the constant previously defined:

     y = x * b

This statement, inside the session, builds the data structures of the x product with b, and the subsequent assignment of the result of the operation to the placeholder y.

In the next lines, we define the computational model. Our model has one input x, so we create a list x_in that will be associated with the evaluated placeholder x:

     x_in = [2]

Variable x_ in contains the value 2 which will be during the computation, the value that will be passed to placeholder x.

Finally, we execute the graph, through the session.run statement:

y_final = session.run([y], {x: x_in})

The first argument is a list of graph elements to evaluate, in our case we are considering only the [y], while the second argument {x: x_in} indicates to what values we are evaluating our argument.

session.run returns y_output as the output value for each graph element that you pass in as the first argument, and the value will correspond to the evaluated value for that element.

In the final instruction, we print out the result:

     9. print(y_output)

We recall that the defined graph elements are processed only when session.run() is executed.

It can manipulate very large and complex values of x, so we create a list x_in that will be associated with the placeholder x.

..................Content has been hidden....................

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