JavaScript

The Tf.Concat() Function in TensorFlow.Js

In the Tensorflow.js library, the tf.concat() function is used to join two or more tensors.

Concatenating 1D Tensors

In this case, the tensors are appended at the end of the first tensor.

Syntax:

tf.concat([tensor1,tensor2,.............])

Parameter:

It takes two or more tensors as a parameter that can be one-dimensional.

Example 1

Create two 1D tensors with integers and concatenate two tensors.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//tensor1

let tensor1 = tf.tensor1d([10,20,30,30]);

//tensor2

let tensor2 = tf.tensor1d([40,50,60,60]);

 

document.write("<b>Tensor-1:</b> ",tensor1);

document.write("<br>");

document.write("<b>Tensor-2:</b> ",tensor2);

document.write("<br>");

document.write("<b>Combined Tensors:</b> ",tf.concat([tensor1,tensor2]));

 

</script>

</body>

</html>

Output:

Values in Tensor-2 are appended to Tensor-1.

Example 2

Create four 1D tensors with integers and concatenate four tensors.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//tensor1

let tensor1 = tf.tensor1d([10,20,30,30]);

//tensor2

let tensor2 = tf.tensor1d([40,50,60,60]);

//tensor3

let tensor3 = tf.tensor1d([70,80]);

//tensor4

let tensor4 = tf.tensor1d([90,100,110]);

 

document.write("<b>Tensor-1:</b> ",tensor1);

document.write("<br>");

document.write("<b>Tensor-2:</b> ",tensor2);

document.write("<br>");

document.write("<b>Tensor-3:</b> ",tensor3);

document.write("<br>");

document.write("<b>Tensor-4:</b> ",tensor4);

document.write("<br>");

document.write("<b>Combined Tensors:</b> ",tf.concat([tensor1,tensor2,tensor3,tensor4]));

 

</script>

</body>

</html>

Output:

Values in Tensor-2, Tensor-3, and Tensor-4 are appended to Tensor-1.

Concatenating 2D Tensors

In this case, we will concatenate tensors that have two dimensions. The tf.concat() function takes one optional axis parameter and the IE integer values.

If we specify 0, then all the rows in remaining tensors are appended one after another to the first tensor.

If we specify 1, then row by row in all tensors are concatenated one after another.

By default, it is 0.

Syntax:

tf.concat([tensor1,tensor2,......],axis)

Parameter:

It takes two or more tensors as a parameter that can be two-dimensional.

Example 1

Create two 2D tensors with 2 rows and 2 columns and concatenate two tensors by specifying axis as 0.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//tensor1

let tensor1 = tf.tensor2d([10,20,30,30],[2,2]);

//tensor2

let tensor2 = tf.tensor2d([40,50,60,60],[2,2]);

 

document.write("<b>Tensor-1:</b> ",tensor1);

document.write("<br>");

document.write("<b>Tensor-2:</b> ",tensor2);

document.write("<br>");

//concatenate tensors

document.write("<b>Combined Tensors:</b> ",tf.concat([tensor1,tensor2],0));

 

</script>

</body>

</html>

Output:

Values in Tensor-2 are appended to Tensor-1.

Example 2

Create two 2D tensors with two rows and two columns and concatenate two tensors by specifying axis as 1.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//tensor1

let tensor1 = tf.tensor2d([10,20,30,30],[2,2]);

//tensor2

let tensor2 = tf.tensor2d([40,50,60,60],[2,2]);

 

document.write("<b>Tensor-1:</b> ",tensor1);

document.write("<br>");

document.write("<b>Tensor-2:</b> ",tensor2);

document.write("<br>");

//concatenate tensors

document.write("<b>Combined Tensors:</b> ",tf.concat([tensor1,tensor2],1));

 

</script>

</body>

</html>

Output:

The first row values in Tensor-2 are appended to first row values in Tensor-1. Similarly, second row values in Tensor-2 are appended to second row values in Tensor-1.

Example 3

Create three 2D tensors with two rows and two columns and concatenate three tensors.

<html>

<!--   CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

 

 

<body>

<script>

 

//tensor1

let tensor1 = tf.tensor2d([10,20,30,30],[2,2]);

//tensor2

let tensor2 = tf.tensor2d([40,50,60,60],[2,2]);

//tensor3

let tensor3 = tf.tensor2d([90,78,89,87],[2,2]);

document.write("<b>Tensor-1:</b> ",tensor1);

document.write("<br>");

document.write("<b>Tensor-2:</b> ",tensor2);

document.write("<br>");

document.write("<b>Tensor-3:</b> ",tensor3);

document.write("<br>");

//concatenate tensors at a time

document.write("<b>Combined Tensors at a time:</b> ",tf.concat([tensor1,tensor2,tensor3],1));

document.write("<br>");

 

//concatenate tensors row by row

document.write("<b>Combined Tensors row by row:</b> ",tf.concat([tensor1,tensor2,tensor3],1));

</script>

</body>

</html>

Output:

In the first output, three tensors are concatenated.

In the second output, row by row in all the tensors are concatenated.

Conclusion

In this Tensorflow.js article, we saw how to concatenate two or more tensors using the tf.concat() function. Make sure that you understand all the examples discussed and get the difference between axis-0/1 parameters. Based on your requirement, you can set the parameter to 0 or 1.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain