Adding two numbers

We will look at an example of adding two numbers using TensorFlow. A placeholder is a node in a TensorFlow graph to which values can be passed during a session. A session is when the graph is initialized and ready to process the values:

  1. We will define two placeholders to take two integer values:
a = tf.Placeholder(tf.int32)
b = tf.Placeholder(tf.int32)
  1. Add these two variables and store them in a variable:
c = a + b
Note that this operation is just defined and not yet executed.
  1. A dictionary of values is created to load to the placeholders. The keys are variables with placeholders themselves, as shown here:
values = {a: 5, b: 3}
  1. Create a session. Once a session is initialized, the graph is loaded into the memory and is ready for the consumption of values into placeholders, and to do the processing:
sess = tf.Session()
  1. Run the session by passing values into the graph. The output from the c node is requested back, so use the following code:
print(sess.run([c], values))

The output should be [8.0]. This example illustrates how values can be fed into the graph and processed. 

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

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