Operations in a computational graph
Now that we can put objects into our computational graph, we will introduce operations that act on such objects.
Getting ready
To start a graph, we load TensorFlow and create a session, as follows:
import tensorflow as tf sess = tf.Session()
How to do it...
In this example, we will combine what we have learned and feed each number in a list into an operation in a graph and print the output:
First, we declare our tensors and placeholders. Here, we will create a NumPy array to feed into our operation:
import numpy as np x_vals = np.array([1., 3., 5., 7., 9.]) x_data = tf.placeholder(tf.float32) m_const = tf.constant(3.) my_product = tf.multiply(x_data, m_const) for x_val in x_vals: print(sess.run(my_product, feed_dict={x_data: x_val}))
The output of the preceding code is as follows:
3.0
9.0
15.0
21.0
27.0
How it works...
The code in this section creates the data and operations on the computational graph. The following figure is what the computational...