## TensorFlow squeeze: Use tf.squeeze to remove a dimension from Tensor

TensorFlow squeeze - Use tf.squeeze to remove a dimension from Tensor in order to transfer a 1-D Tensor to a Vector

### < > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

or Log In

### Transcript:

We import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print the TensorFlow version that we are using.

```
print(tf.__version__)
```

We are using TensorFlow 1.0.1.

In this video, we're going to transfer a 1-D tensor to a vector using the tf.squeeze transformation to remove the dimension of size 1 from the shape of the tensor.

Let's get started.

In order to do that, let's start by defining a TensorFlow variable that will hold random numbers between 0 and 10 that are of the data type 32-bit signed integers and has a shape of 1x5.

```
random_int_var_one_ex = tf.get_variable("random_int_var_one",
initializer=tf.random_uniform([1, 5],
minval=0,
maxval=10,
dtype=tf.int32))
```

We create this tensor using the tf.get_variable operation and assign it to the Python variable, random_int_var_one_ex.

Let's print our random_int_var_one_ex Python variable to see what's in it.

```
print(random_int_var_one_ex)
```

We see the name of the tensor, we see the shape which is 1x5, and we see the data type is int32.

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

We launch the graph in a session.

```
sess = tf.Session()
```

Then we initialize all the global variables in the graph, in this case just our random_int_var_one_ex tensor.

```
sess.run(tf.global_variables_initializer())
```

Let's print our random_int_var_one Python variable in a TensorFlow session to see the values.

```
print(sess.run(random_int_var_one_ex))
```

We see 5, 3, 9, 2, 5 which is between 0 and 10, which is what we would expect.

The data type, it looks like they are integers.

Let's calculate the rank of the tensor to see what we get.

```
print(sess.run(tf.rank(random_int_var_one_ex)))
```

We use the tf.rank operation and we pass in our tensor.

We see that it is of rank 2 which means it is not a vector.

To make it a vector with a rank of 1, we'll use the tf.squeeze transformation to remove the dimension of size 1 from the shape of the tensor.

Here, we can see tf.squeeze and we pass in our tensor.

```
squeezed_tensor_ex = tf.squeeze(random_int_var_one_ex)
```

Let's print the result.

```
print(sess.run(squeezed_tensor_ex))
```

We can see visually that before, we had two square brackets on each side.

Now, we have one square bracket on each side.

Let's check the shape of the tensor using tf.shape inside a TensorFlow session run

```
print(sess.run(tf.shape(squeezed_tensor_ex)))
```

And we see that it is 5, whereas before we knew that the shape was 1x5.

Which we can see when we get the shape of the random_int_var_one.

```
print(sess.run(tf.shape(random_int_var_one_ex)))
```

So it went from shape 1x5 to 5.

Lastly, let's check the rank of our squeeze tensor using the tf.rank functionality

```
print(sess.run(tf.rank(squeezed_tensor_ex)))
```

And we see that it is 1.

So it is now indeed a vector.

Finally, we close the TensorFlow session to release the TensorFlow resources we created within the session.

```
sess.close()
```

That is how you can transfer a 1-D tensor to a vector using the tf.squeeze transformation to remove the dimension of size 1 from the shape of the tensor.