JavaScript

Tensorflow.js – tf.slice()

We already know how to create a tensor in the tensorflow.js library and display all the values from it. Now, the task is to return only some portion/range of elements from a tensor.

How do you do that?

The answer is quite simple. Tensorflow.js library supports the tf.slice() function which returns the elements based on the index. The index starts with 0.

Let’s see how to get the elements from a tensor.

Tensorflow.js – tf.slice()

The tf.slice() function is used to return elements from a tensor within the range and return those range of elements in a new tensor. It takes three parameters.

Syntax:

tf.slice(tensor.start,size)

 
Parameters:

    1. Tensor can be single or two-dimensional.
    2. Start specifies the index position in which the starting range is specified.
    3. Size takes an integer that returns the elements from the starting range.

Example 1:

Create a one-dimensional tensor with 10 integer values and get the following range of values:

    1. From index-0 to index-6 (start should be 0 and size is 7)
    2. From index-0 to index-8 (start should be 0 and size is 9)
    3. From index-3 to index-8 (start should be 3 and size is 6)
    4. From index-4 to index-9 (start should be 4 and size is 6)
<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h2>Tensorflow.js - tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor1d([1,2,3,4,5,6,7,8,9,10]);
//actual tensor
document.write("<b>Actual Scalar: </b>",values);
 
document.write("<br>");
 
// index-0 to index-6
document.write("<b>Elements from index-0 to index-6: </b> "+tf.slice(values,[0],7));
document.write("<br>");

// index-0 to index-8
document.write("<b>Elements from index-0 to index-8: </b> "+tf.slice(values,[0],9));
document.write("<br>");

// index-3 to index-8
document.write("<b>Elements from index-3 to index-8: </b> "+tf.slice(values,[3],6));
document.write("<br>");

// index-4 to index-9
document.write("<b>Elements from index-4 to index-9: </b> "+tf.slice(values,[4],6));
document.write("<br>");
</script>
 
</body>
</html>

 
Output:


We got the elements from index-0 to index-6. The total number of elements is 7.

Hence, we specified the size as 7.

Similarly:

    1. From index-0 to index-8, the size is 9.
    2. From index-3 to index-8, the size is 6.
    3. From index-4 to index-9, the size is 6.

Example 2:

Create a one-dimensional tensor with 5 integer values and get the following range of values:

    1. From index-0 to index-3 (start should be 0 and size is 4)
    2. From index-3 to index-4 (start should be 3 and size is 1)
<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h2>Tensorflow.js - tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor1d([1,2,3,4,5]);
//actual tensor
document.write("<b>Actual Scalar: </b>",values);
 
document.write("<br>");
 
// index-0 to index-3
document.write("<b>Elements from index-0 to index-3: </b> "+tf.slice(values,[0],4));
document.write("<br>");

// index-3 to index-4
document.write("<b>Elements from index-3 to index-4: </b> "+tf.slice(values,[3],1));

</script>
 
</body>
</html>

 
Output:

Example 3:

Create a two-dimensional tensor with 5 rows and 4 columns (20 elements) and get the range of values from the row-index2 to row-index3.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
 
<body>
<center><h2>Tensorflow.js - tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor2d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],[5,4]);
//actual tensor
document.write("<b>Actual Scalar: </b>",values);
 
document.write("<br>");
 
// row index-2 to row index-3
document.write("<b>Elements from row index-2 to row index-3: </b> "+tf.slice(values,[2],2));
document.write("<br>");


</script>
 
</body>
</html>

 
Output:


Row index-2 => [9, 10, 11, 12] and Row index-3 => [13, 14, 15, 16].

Conclusion

At the end of this article, we learned that using the tf.slice()can be possible to get a range of elements from a tensor. We specified the three different examples to understand this concept better.

In the Deep Learning using the Tensorflow.js library, we will use this technique to get the image pixels from a particular position.

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