Open In App

Tensorflow.js tf.squeeze() 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 .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:  



Return Value: It returns the tf.Tensor object.

Example 1:  




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




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

Article Tags :