Layering Nested Operations
In this recipe, we will learn how to put multiple operations on the same computational graph.
Getting ready
It's important to know how to chain operations together. This will set up layered operations in the computational graph. For a demonstration we will multiply a placeholder by two matrices and then perform addition. We will feed in two matrices in the form of a three-dimensional numpy
array:
import tensorflow as tf sess = tf.Session()
How to do it…
It is also important to note how the data will change shape as it passes through. We will feed in two numpy
arrays of size 3x5. We will multiply each matrix by a constant of size 5x1, which will result in a matrix of size 3x1. We will then multiply this by 1x1 matrix resulting in a 3x1 matrix again. Finally, we add a 3x1 matrix at the end, as follows:
First we create the data to feed in and the corresponding placeholder:
my_array = np.array([[1., 3., 5., 7., 9.], [-2., 0., 2., 4., 6.], ...