Use TensorFlow reshape To Infer Reshaped Tensor's New Dimensions

Use the TensorFlow reshape operation to infer a tensor's new dimensions when reshaping a tensor

Use the TensorFlow reshape operation to infer a tensor's new dimensions when reshaping a tensor

Video Transcript


We import TensorFlow as tf.

import tensorflow as tf


Then we print out the version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.5.0.


In this video, we’re going to use the TensorFlow reshape operation to infer a tensor’s new dimensions when reshaping a tensor.

This is useful in some areas where you know some of the dimensions that you need the tensor to be in but you don’t know all of the dimensions.


Let’s start out with our initial TensorFlow constant tensor that we’ll later reshape.

tf_initial_tensor_constant = tf.constant(
[
    [
        [ 1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12],
        [13, 14, 15, 16, 17, 18],
        [19, 20, 21, 22, 23, 24]
    ]
    ,
    [
        [25, 26, 27, 28, 29, 30],
        [31, 32, 33, 34, 35, 36],
        [37, 38, 39, 40, 41, 42],
        [43, 44, 45, 46, 47, 48]
    ]
]
, dtype="int32"
)

We create a tensor shaped 2x4x6 with numerical integer values between 1 and 48, all of which have the data type int32.

We use tf.constant and we assign it to the Python variable tf_initial_tensor_constant.


Let’s print out the tf_initial_tensor_constant Python variable to see what we have.

print(tf_initial_tensor_constant)

We see that it’s a TensorFlow tensor, we see that the shape is 2x4x6, and we see that the data type is int32.

Because we haven’t run it in a TensorFlow session, it doesn’t have values yet even though we just defined it as a constant.

The same will apply to the other reshapes we’re about to create.


For the first example, let’s say that you don’t know what the initial tensor shape coming in is.

We just know that we want it to be a 2x12, and then the third dimension can be figured out.

So we’ll be going from a rank 3 tensor to a rank 3 tensor.


So we use tf.reshape, we pass in our initial tensor constant, and we want the shape to be 2, 12, -1.

tf_ex_one_reshaped_tensor_2_by_12_by_x = tf.reshape(tf_initial_tensor_constant, [2, 12, -1])

By using this -1, we’re telling TensorFlow to infer from the tensor we pass in what the third dimension should be.

So we’re going to assign that result to the Python variable tf_ex_one_reshaped_tensor_2_by_12_by_x.


Let’s print out this variable to see what we have.

print(tf_ex_one_reshaped_tensor_2_by_12_by_x)

We see that it’s a TensorFlow tensor, we see that the data type is int32, and we see that the shape is 2x12x2.

So you can see that TensorFlow automatically inferred that the third dimension should be the number 2.

This makes sense because 2 times 12 is 24, and since there are 48 elements because our initial shape was 2x4x6, so 8 times 6 is 48.

So 48 divided 24 is 2.


For the second example, let’s say that you don’t know what the initial tensor shape coming in is.

We just know that we want it to be 6 by another number.

So we’ll be going from a rank 3 tensor to a rank 2 tensor.


So we use tf.reshape:

tf_ex_two_reshaped_tensor_6_by_x = tf.reshape(tf_initial_tensor_constant, [6, -1])

We pass in our initial tensor, and we say that we want the shape to be 6, -1.

Again, remember that we use the -1 to infer the shape that will be used. We assign it to the Python variable tf_ex_two_reshaped_tensor_6_by_x.


When we print this variable:

print(tf_ex_two_reshaped_tensor_6_by_x)

We see that it’s a TensorFlow tensor.

We see that the shape is 6x8.

So we can see that TensorFlow automatically inferred that the second dimension should be the number 8 and this makes sense because 6 times 8 is 48, and since there are 48 elements, it means that 48 divided 6 is 8.

Also note that TensorFlow didn’t complain about going from a rank 3 tensor to a rank 2 tensor.


For this third example, let’s say that you don’t know what the initial tensor shape is coming in.

We just know that we want it to be a 2x2x3 by another number.

So we’ll be going from a rank 3 tensor to a rank 4 tensor.


So we use tf.reshape:

tf_ex_tre_reshaped_tensor_2_by_2_by_3_by_x = tf.reshape(tf_initial_tensor_constant, [2, 2, 3, -1])

We pass in our tf_initial_tensor_constant, and now we’re saying that we want the shape to be 2, 2, 3, -1, remembering that the -1 tells TensorFlow that we want it to infer the shape of this dimension.

Then we assign it to the tf_ex_tre_reshaped_tensor_2_by_2_by_3_by_x Python variable.


Then we can print this variable:

print(tf_ex_tre_reshaped_tensor_2_by_2_by_3_by_x)

And we see that it’s a TensorFlow tensor, the shape is 2x2x3x4.

So we can see that TensorFlow automatically inferred that the fourth dimension should be the number 4.

This makes sense because 2 times 2 is 4 times 3 is 12, and 12 times 4 is 48.

Since there are 48 elements, it makes sense that this number would be the number 4.

Again, note that TensorFlow didn’t complain about going from a rank 3 tensor to a rank 4 tensor.


Now that we have created our TensorFlow tensors, it’s time to run the computational graph.


First, we launch the graph in a session.

sess = tf.Session()


Then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


Next, we’re going to print out our four tensors to see how TensorFlow’s reshape works when we ask TensorFlow to infer a dimension.


We print our initial tensor to remind ourselves what it was.

print(sess.run(tf_initial_tensor_constant))

So we see the numbers 1 through 48, and we see that it’s 2x4x6.


Let’s now print our first TensorFlow example which was tf_ex_one_reshaped_tensor_2_by_12_by_x.

print(sess.run(tf_ex_one_reshaped_tensor_2_by_12_by_x))

When we do that, we see that it is a tensor that contains two matrices.

The first matrix has 12 rows and two columns, the second matrix has 12 rows and two columns, and all our 48 numbers are there.

So we see that TensorFlow was able to adapt to the dynamic shape requirement and infer that the correct number of columns was two.


Let’s now print our second TensorFlow example, tf_ex_two_reshaped_tensor_6_by_x.

print(sess.run(tf_ex_two_reshaped_tensor_6_by_x))

Awesome! We see that it’s a tensor that has one matrix.

The first and only matrix has one, two, three, four, five, six rows, and one, two, three, four, five, six, seven, eight columns, and all our 48 numbers are there.

We see that TensorFlow was able to adapt to the dynamic shape requirement and infered that the correct number of columns was eight.


Finally, let’s print our third TensorFlow example.

print(sess.run(tf_ex_tre_reshaped_tensor_2_by_2_by_3_by_x))

This was tf_ex_tre_reshaped_tensor_2_by_2_by_3_by_x.

When we see that, we see that it’s a tensor that has two interior tensors, each of which contains two matrices and then another two matrices here, so 2x2x3.

Each of these internal matrices has three rows and four columns, and we see that TensorFlow was able to adapt to the dynamic shape requirement and recognized that we wanted to increase the rank of the tensor, and that the correct number of columns for the fourth dimension was four.


Perfect - It worked!

We were able to use the tf.reshape operation to infer a tensor’s new dimension when reshaping a tensor.

Receive the Data Science Weekly Newsletter every Thursday

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

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