tf.concat - Use tf.concat, TensorFlow's concatenation operation, to concatenate TensorFlow tensors along a given dimension

tf.concat - Use tf.concat, TensorFlow's concatenation operation, to concatenate TensorFlow tensors along a given dimension

We import TensorFlow as tf

```
import tensorflow as tf
```

And then we print the TensorFlow version.

```
tf.__version__
```

We are using TensorFlow 1.0.1.

Now, we’re going to create two TensorFlow variables that will hold random numbers.

```
random_tensor_var_one = tf.Variable(tf.random_uniform([2, 3, 4], minval=0, maxval=10, dtype=tf.int32, seed=None, name=None))
```

We use TensorFlow Variable so that they maintain the same state across multiple calls of the session run.

We’re creating this TensorFlow Variable using the tf.random_uniform functionality and we’re going to generate a tensor that is 2x3x4, has a minimum value of 0, a max value of 10, and the data type is tf.int32.

Int32 is a 32-bit signed integer.

So that is random_tensor_var_one.

We define random_tensor_var_two the same way.

```
random_tensor_var_two = tf.Variable(tf.random_uniform([2, 3, 4], minval=0, maxval=10, dtype=tf.int32, seed=None, name=None))
```

We use the tf.random_uniform. We have a tensor that’s 2x3x4, min value of 0, max value of 10, and the data type is also int32.

Next, we use tf.global_variables_initializer so that when we run the session, we can initialize all the variables.

```
init_var = tf.global_variables_initializer()
```

Then we define our session variable

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

And initialize all our variables.

```
sess.run(init_var)
```

So here, we’re going to have two variables.

Remember, random_tensor_var_one and random_tensor_var_two.

First, let’s print our random_tensor_var_one to see what it looks like.

```
print(sess.run(random_tensor_var_one))
```

We see that it is integers and it is 2x3x4.

Next, we print our second tensor and we see that it is also 2x3x4, it’s all integers, and it is a completely different tensor from the first one.

```
print(sess.run(random_tensor_var_two))
```

To concatenate tensors, we’re going to use tf.concat.

```
concat_tensor_dim_zero = tf.concat([random_tensor_var_one, random_tensor_var_two], 0)
```

What we do is we pass a list of tensors and then we specify the dimension we want to concatenate across.

We are going to concatenate across the 0th dimension.

Remember, Python is a zero-based index. And we’re going to assign this concatenation to Python variable concat_tensor_dim_zero.

Now, we’re going to print the result.

```
print(sess.run(concat_tensor_dim_zero))
```

We evaluate the variable inside of the session and we print the result.

We see that it has the four matrices, so 6, 5, 4; 1, 2, 3; 5, 8, 3; 4, 9, 0.

They are all there.

And we can run tf.shape on our concat_tensor_dim_zero variable, run it on the session, and print the result.

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

And we see that it is 4x3x4.

This is expected.

We’re concatenating the two tensors across the 0th dimension, so that would be the first dimension.

Since each TensorFlow tensor was 2x3x4, we see that it is 4x3x4.

So we have these 3x4 matrices and there are four of them on top of each other.

We’re going to do another concatenation between random_tensor_var_one and random_tensor_var_two.

This time, we’re concatenating across dimension 1. We use the tf.concat functionality and we’re going to assign it to the Python variable, concat_tensor_dim_one.

```
concat_tensor_dim_one = tf.concat([random_tensor_var_one, random_tensor_var_two], 1)
```

We evaluate the variable inside of our session and print the result

```
print(sess.run(concat_tensor_dim_one))
```

To see that we have one matrix here, one matrix here, and it is now one, two, three, four, five, six rows by four columns.

So we have two matrices, six rows, four columns.

So when we use the tf.shape to get the dimensions

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

We can see that it is 2x6x4, which is what we expect because each tensor originally was 2x3x4.

So when we concatenated it across dimension 1, we would expect the shape to be 2x6x4.

The last concatenation we do is across the second dimension.

```
concat_tensor_dim_two = tf.concat([random_tensor_var_one, random_tensor_var_two], 2)
```

Remember, this tensor is 2x3x4 so it has three dimensions.

Python is a zero-based index so this 2 will be the last one.

We assign it to the Python variable, concat_tensor_dim_two

And we print the result.

```
print(sess.run(concat_tensor_dim_two))
```

We see that we have two matrices with one, two, three rows and one, two, three, four, five, six, seven, eight columns.

When we print out the shape using the tf.shape functionality

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

we can see that it is indeed 2x3x8 which is expected.

Each original tensor was 2x3x4 so when we concatenated across the second dimension, we would expect this to be 2x3x4 plus 4 which is exactly what we got.

The last thing we do is we close the TensorFlow session to release the TensorFlow resources we used in this session as we no longer require them.

```
sess.close()
```

Receive the Data Science Weekly Newsletter every Thursday

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