Open In App

Tensorflow.js tf.stridedSlice() Function

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .stridedSlice() function is used to pull out a strided section of the stated input tensor.



Note:stride from the stated input tensor. Beginning at the position as given by begin the slice proceeds by appending the stride to the stated index before all the measurements are greater than end. Moreover, a stride can also be negative, which leads to a reverse slice.

Syntax:



tf.stridedSlice(x, begin, end, strides?, beginMask?, endMask?, 
ellipsisMask?, newAxisMask?, shrinkAxisMask?)

 

Parameters:  

Return Value: It returns tf.Tensor.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input
const tn = tf.tensor3d([5, 5, 5 ,7, 7, 7, 13, 
    13, 13, 14, 14, 14, 1, 1, 1, 2, 2, 2],
    [3, 3, 2]);
  
// Calling stridedSlice() method and 
// Printing output
tn.stridedSlice([5, 0, 5], [7, 5, 7], [2, 2, 2]).print();

Output:

Tensor
    [[[1],
      [2]]]

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input
const tn = tf.tensor3d([5, 5, 5 ,7, 7, 7, 13, 
    13, 13, 14, 14, 14, 1, 1, 1, 2, 2, 2],
    [3, 3, 2]);
  
// Defining all the parameters
const x = [-5, 5, 7];
const begin = [-7, 13, 14];
const end = [-7, 13, 13];
const strides = [1];
const beginMask = 2;
const endMask = 0;
const ellipsisMask = 2;
const shrinkAxisMask = 15;
  
// Calling stridedSlice() method and 
// Printing output
tn.stridedSlice(x, begin, end, strides, beginMask,
     endMask, ellipsisMask, shrinkAxisMask).print();

Output:

Tensor
    2

Reference: https://js.tensorflow.org/api/latest/#stridedSlice


Article Tags :