## tf.stack: How To Use TensorFlow Stack Operation

tf.stack - How to use tf stack operation to stack a list of TensorFlow tensors

### < > 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:

First, we import TensorFlow as tf.

```
import tensorflow as tf
```

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

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

We are using TensorFlow 1.0.1.

In this video, we’re going to stack a list of TensorFlow tensors of the same rank into one tensor by using tf.stack.

All right, let’s get started.

The first tensor we create will be a tf.ones tensor full of ones that are all float32 numbers.

```
tf_ones_ex_one = tf.ones(shape=[2,3,4], dtype="float32")
```

So use tf.ones, the shape will be 2x3x4, and the data type will be float32.

We assign it to the Python variable tf_ones_ex_one.

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

```
print(tf_ones_ex_one)
```

We see that it’s a TensorFlow tensor, TensorFlow gave it a name, the shape is 2x3x4, and the data type is float32.

Because we haven’t run it in a TensorFlow session, right now it doesn’t have any values yet.

The same thing applies to the other two tensors we’re about to create.

The second tensor we’re going to create is going to be a tf.zeros tensor full of zeros that are all float32.

```
tf_zeros_ex = tf.zeros(shape=[2,3,4], dtype="float32")
```

We use tf.zeros, the shape is going to be 2x3x4, the data type is float32, and we assign it to the Python variable tf_zeros_ex.

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

```
print(tf_zeros_ex)
```

We see that it’s a TensorFlow tensor, the shape is 2x3x4, and the data type is float32.

The third tensor we create will be a ones tensor again, only this time we’re going to assign it to a different variable.

```
tf_ones_ex_two = tf.ones(shape=[2,3,4], dtype="float32")
```

So tf.ones, the shape is 2x3x4, the data type is float32, and we assign it to the Python variable tf_ones_ex_two.

So it’s this last underscore two versus the underscore one that will differentiate the two Python variables.

Let’s print out our tf_ones_ex_two Python variable to see what we have.

```
print(tf_ones_ex_two)
```

We see that it’s a TensorFlow tensor, the shape is 2x3x4, and the data type is float32.

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

First, we launch the TensorFlow graph in a session.

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

Then we initialize all the global variables in the graph.

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

In this case, we have three TensorFlow tensors that we have defined, so now they are initialized.

Let’s now print our first TensorFlow tensor full of ones to see what it looks like.

```
print(sess.run(tf_ones_ex_one))
```

So we print the sess.run(tf_ones_ex_one).

We see that it’s a 2x3x4 TensorFlow tensor full of ones that all have decimal points, so that visually designates that all these are float32 numbers.

Next, we print our second TensorFlow tensor that should be full of zeros, 2x3x4 zeros, decimal points.

```
print(sess.run(tf_zeros_ex))
```

Exactly what we want.

Then we print our third TensorFlow tensor full of ones.

```
print(sess.run(tf_ones_ex_two))
```

Again, tf_ones_ex_two, tf_zeros_ex, and tf_ones_ex_one.

Those are our three tensors.

So this one is all ones, decimal points, exactly what we want.

Before we use the tf.stack to stack the tensors, let’s check the rank of the tensors to make sure they are all the same rank.

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

So we’re going to print a session run, and we use tf.rank, and we pass in our Python variable tf_ones_ex_one, and we see that the rank is three.

That is, it has three dimensions.

That’s what we expect because it is 2x3x4 so it has three numbers that define the tensor.

So because we created all the tensors to be 2x3x4, we know that all of them will have the rank of three.

So we can stack them up.

So when we go to stack them on top of each other, what we’ll be doing is we’ll be creating a tensor of rank four.

That is, three tensors of shape 2x3x4 stacked on top of each other.

The way we’re going to do that is we’re going to use tf.stack, then we’re going to pass in a Python list of our tensor variables, so tf_ones_ex_one, tf_zeros_ex, and tf_ones_ex_two.

```
tf_tensor_stack_ex = tf.stack([tf_ones_ex_one, tf_zeros_ex, tf_ones_ex_two])
```

That’s going to be evaluated and we’re going to assign that to the Python variable tf_tensor_stack_ex.

Let’s now print the result in a TensorFlow session.

```
print(sess.run(tf_tensor_stack_ex))
```

So we’re going to do a print sess.run and then our tf_tensor_stack_ex.

What we see if we scroll up a little bit is we see our first 2x3x4 tensor, then our second 2x3x4 tensor, and then our third 2x3x4 tensor.

Great.

So we have the four tensors stacked up on top of each other.

To make sure, let’s check the shape of this newly created stack tensor.

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

So we’re going to do a print sess.run, then we use tf.shape, and we pass in our tf_tensor_stack_ex Python variable, and we see that it is 3 2 3 4.

So we have three tensors that are 2x3x4 which is what we expect.

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

Because of that, we know that if we do a print sess.run, we use the tf.rank, pass in the tf_tensor_stack_ex Python variable, we see that the rank is four.

That is, it has four numbers or four dimensions.

Perfect - we are able to stack our list of three TensorFlow tensors.

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

```
sess.close()
```

That is how you stack a list of TensorFlow tensors of the same rank into one tensor by using tf.stack.