Open In App

Tensorflow.js tf.squeeze() Function

Last Updated : 31 May, 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 .squeeze() function is used to discard the dimensions of length one out of the shape of a stated tf.Tensor.

Syntax :  

tf.squeeze(x, axis?)

Parameters:  

  • x: It is the stated tensor input which is to be squeezed, and it can be of type tf.Tensor, TypedArray, or Array.
  • axis: It is an elective parameter that holds a list of numbers. It squeezes only the stated dimensions in case its specified. Moreover, the dimension index here begins with zero, and it’s a flaw to compress a dimension whose length is not one.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor([11, 76, -4, 6], [2, 2, 1]);
  
// Calling squeeze() method and
// Printing output
y.squeeze().print();


Output:

Tensor
    [[11, 76],
     [-4, 6 ]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling squeeze() method with
// all its parameter
var res = tf.squeeze(tf.tensor(
    [2.1, 5.6, 8.6, 7.6], 
    [4, 1]), [1]
);
  
// Printing output
res.print();


Output:

Tensor
    [2.0999999, 5.5999999, 8.6000004, 7.5999999]

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


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

Similar Reads