Open In App

Tensorflow.js tf.stridedSlice() Function

Last Updated : 06 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:< This function is utilized to pull out a slice of the stated length 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:  

  • x: The stated tensor in order to stride slice. It can be of type tf.Tensor, TypedArray, or Array.
  • begin: The stated coordinates from where the slice is to be to started. It is of type number[].
  • end: The stated coordinates at which the slice is to be ended. It is of type number[].
  • strides: The stated length of the slice. It is optional and is of type number[].
  • beginMask: It is an optional parameter which is of type number. In case, the ith bit of beginMask is fixed, then begin[i] is overlooked and the greatest attainable range in such dimension is utilized on the contrary.
  • endMask: It is an optional parameter which is of type number. In case, the ith bit of endMask is fixed, then end[i] is overlooked and the greatest attainable range in such dimension is utilized on the contrary.
  • ellipsisMask: It is an optional parameter of type number.
  • newAxisMask: It is an optional parameter of type number.
  • shrinkAxisMask: The stated bitmask where bit i indicates that the ith designation must shrink the capacity. The begin as well as end should indicate a slice of length one in the size. It is optional and is of type number.

Return Value: It returns tf.Tensor.

Example 1:

Javascript




// 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:

Javascript




// 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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads