JavaScript

The Tf.Stack() Function in TensorFlow.Js

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

Syntax:

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

Parameters:

  1. It takes two or more tensors as a parameter that can be one-dimensional as the first parameter.
  2. The axis takes two possible values (0 and 1).

If specified 0, tf.stack() joins tensors one after another by returning a new tensor.

If specified 1, tf.stack() joins element by element in a row by returning a new tensor.

Example 1

Create two 1D tensors with integers and stack two tensors by setting axis-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.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>");

//stack the tensors at a time

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

document.write("<br>");

</script>

</body>

</html>

Output:

Tensor1 and Tensor2 are stacked one after another.

Example 2

Create two 1D tensors with integers and stack two tensors by setting axis-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.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>");

//stack the tensors element by element

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

document.write("<br>");

</script>

</body>

</html>

Output:

Tensor1 and Tensor2 are stacked element by element in both tensors, and returned tensor has stacked elements placed in separate rows.

Example 3

Create four 1D tensors with integers and stack them by setting axis-1 and axis-0 separately.

<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([12,10,45,67]);

//tensor4

let tensor4 = tf.tensor1d([40,12,34,56]);

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>");

//stack the tensors element by element

document.write("<b>Stacked Tensors element by element: </b> ",tf.stack([tensor1,tensor2,tensor3,tensor4],1));

document.write("<br>");

//stack the tensors at a time

document.write("<b>Stacked Tensors at a time: </b> ",tf.stack([tensor1,tensor2,tensor3,tensor4],0));

document.write("<br>");

</script>

</body>

</html>

Output:

In the first output, four tensors are stacked element by element, and in the second output, tensors are stacked at a time.

Conclusion

In this TensorFlow.js tutorial, we saw how to stack two or more tensors using the tf.stack() function. If the axis is specified as 0, tf.stack() joins tensors one after another by returning a new tensor. If specified as 1, tf.stack() joins element by element in a row by returning a new tensor. Ensure you understand all the examples discussed and get the difference between axis-0/1 parameters.

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