
TensorFlow Machine Learning Cookbook
By :

Tensors are the primary data structure that TensorFlow uses to operate on the computational graph. We can declare these tensors as variables and or feed them in as placeholders. First we must know how to create tensors.
When we create a tensor and declare it to be a variable, TensorFlow creates several graph structures in our computation graph. It is also important to point out that just by creating a tensor, TensorFlow is not adding anything to the computational graph. TensorFlow does this only after creating available out of the tensor. See the next section on variables and placeholders for more information.
Here we will cover the main ways to create tensors in TensorFlow:
zero_tsr = tf.zeros([row_dim, col_dim])
ones_tsr = tf.ones([row_dim, col_dim])
filled_tsr = tf.fill([row_dim, col_dim], 42)
constant_tsr = tf.constant([1,2,3])
Note that the tf.constant()
function can be used to broadcast a value into an array, mimicking the behavior of tf.fill()
by writing tf.constant(42, [row_dim, col_dim])
zeros_similar = tf.zeros_like(constant_tsr) ones_similar = tf.ones_like(constant_tsr)
Note, that since these tensors depend on prior tensors, we must initialize them in order. Attempting to initialize all the tensors all at once willwould result in an error. See the section There's more… at the end of the next chapter on variables and placeholders.
range()
outputs and numpy's linspace()
outputs. See the following function:linear_tsr = tf.linspace(start=0, stop=1, start=3)
[0.0, 0.5, 1.0]
. Note that this function includes the specified stop value. See the following function:integer_seq_tsr = tf.range(start=6, limit=15, delta=3)
randunif_tsr = tf.random_uniform([row_dim, col_dim], minval=0, maxval=1)
minval
but not the maxval
(minval
<= x
< maxval
).randnorm_tsr = tf.random_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
truncated_normal()
function always picks normal values within two standard deviations of the specified mean. See the following:runcnorm_tsr = tf.truncated_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
random_shuffle()
and random_crop()
. See the following:shuffled_output = tf.random_shuffle(input_tensor) cropped_output = tf.random_crop(input_tensor, crop_size)
cropped_output
, you must give it the maximum size in that dimension:cropped_image = tf.random_crop(my_image, [height/2, width/2, 3])
Once we have decided on how to create the tensors, then we may also create the corresponding variables by wrapping the tensor in the Variable()
function, as follows. More on this in the next section:
my_var = tf.Variable(tf.zeros([row_dim, col_dim]))
We are not limited to the built-in functions. We can convert any numpy
array to a Python list, or constant to a tensor using the function convert_to_tensor()
. Note that this function also accepts tensors as an input in case we wish to generalize a computation inside a function.