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:
- We will define two placeholders to take two integer values:
a = tf.Placeholder(tf.int32)
b = tf.Placeholder(tf.int32)
- Add these two variables and store them in a variable:
c = a + b
Note that this operation is just defined and not yet executed.
- 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}
- 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()
- 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.