tf.reshape: Use TensorFlow reshape To Convert A Tensor To A Vector

tf.reshape - Use TensorFlow reshape to convert a tensor to a vector by understanding the two arguments you must pass to the reshape operation and how the special value of negative one flattens the input tensor

tf.reshape - Use TensorFlow reshape to convert a tensor to a vector by understanding the two arguments you must pass to the reshape operation and how the special value of negative one flattens the input tensor

Video Transcript


We import TensorFlow as tf.

import tensorflow as tf


Then we print the TensorFlow version.

print(tf.__version__)

We are using TensorFlow version 1.0.1.


We’re going to create two TensorFlow variables that will hold random numbers.

We’re going to create the TensorFlow variable using the random_uniform functionality.


For the first TensorFlow variable, we’re going to have it be a data type of int32 and for the initializer, it’s going to be a 2x3x4 tensor.

random_int_var = tf.get_variable("random_int_var",
                                 initializer=tf.random_uniform([2, 3, 4],
                                                               minval=0,
                                                               maxval=10,
                                                               dtype=tf.int32))

We create the TensorFlow variable and we assign it to the Python variable random_int_var.

Note that because we’re using the initializer as a constant, we don’t have to specify the shape for the variable.


We now create a random_float_var variable built the exact same way as the int one.

random_float_var =  tf.get_variable("random_float_var",
                                 initializer=tf.random_uniform([1, 2, 3],
                                                               minval=0,
                                                               maxval=10,
                                                               dtype=tf.float32))

The difference is that the data type is tf.float32.


Next, we create a TensorFlow operation that initializes all the global variables in the graph.

init_var = tf.global_variables_initializer()


Then we launch the graph in a session

sess = tf.Session()


And initialize all the variables.

sess.run(init_var)


The first thing we want to do is we want to print the random_int_var TensorFlow variable to look at the values.

print(sess.run(random_int_var))

We see that they are indeed integers.


Next, let’s check out the shape.

print(sess.run(tf.shape(random_int_var)))

We can obviously tell that it is 2x3x4.

But if you are doing a very large tensor, it’s not always easy to see what the tensor shape is, so here we use the tf.shape functionality and it gives us 2x3x4.


Before we reshape the 2x3x4 tensor into a one-dimensional vector, we can calculate what dimension the vector will have.

2 * 3 * 4

So 2 x 3 x 4 is 24.

So we expect our one-dimensional vector to have a shape that is 24.


To reshape our tensor, we’re going to use tf.reshape.

random_int_vector = tf.reshape(random_int_var, [-1])

The first argument we pass to tf.reshape is the tensor we want to reshape.

The second argument we pass is the shape of the new tensor we want.

It’s worth noting that -1 is a special value that makes it so the total size remains constant and the tensor is flattened into a one-dimensional vector.


Let’s now print our random_int_vector

print(sess.run(random_int_vector))

And we see that it is one dimensional.


Looking up here at our tensor, we see 2, 1, 8, 0; 2, 1, 8, 0, and it goes on, so on and so forth until the last number is 4, 9, 9, 0; 4, 9, 9, 0.

So we successfully reshaped our TensorFlow tensor into a vector.


We can print the shape using tf.shape and it is indeed a size 24.

print(sess.run(tf.shape(random_int_vector)))


Next, let’s do the same thing with the float variable.


When we defined it above, we saw that it was a shape of 1x2x3.

So here when we print it out to look at the values

print(sess.run(random_float_var))

We see that it is indeed 1x2x3.


We can check the shape using the tf.shape functionality

print(sess.run(tf.shape(random_float_var)))

and we see that it is indeed 1x2x3.


Before we reshape the tensor into a vector, we can calculate the dimensions of that vector.

It will be 1 multiplied by 2 multiplied by 3.

1 * 2 * 3

So it will be a vector of size 6.


We can reshape our random_float_var using the tf.reshape functionality.

random_float_vector = tf.reshape(random_float_var, [-1])

Again, the first argument is the tensor we want to reshape.

The second argument is the shape we want to give to the reshaped tensor.

We’re using the special value of -1 which will flatten it into a one-dimensional vector.


We can print our random float vector to see what it looks like now

print(sess.run(random_float_vector))

and we see 7.32, 3.08, 9.19.

We see that those are the first three, and then we see the second three here are exactly the same.


Lastly, we can print the shape using tf.shape to see that it is in fact a vector of size 6.

print(sess.run(tf.shape(random_float_vector)))


So again, the way we reshape our tensor is using tf.reshape where we pass the first argument of the tensor we wanted to reshape and the second argument was the shape and we used the special value of -1 to make it a one-dimensional vector.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.