Friday, August 29, 2025
HomeLanguagesJavascriptTensorflow.js tf.zerosLike() Function

Tensorflow.js tf.zerosLike() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. The tf.zeroslIke() is used to create a tf.tensor with all elements set to ‘0’ with the same shape as the given tensor by passing the parameter value.

Syntax:

tf.zerosLike(value)

Parameter: It accepts a single parameter as mentioned above and described below:

  • value: It is the value of the tensor which can be a simple or nested Array or TypedArray of numbers. We pass the Tensor of the required shape here.

Return value: It returns a tensor of the required shape.

Note: The above function does not change the original tensor.

Example 1: In this example, we use the tf.zeroslike() method using tf.tensor.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor([1, 2, 3, 4, 5, 6, 7]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)


Tensor
    [0, 0, 0, 0, 0, 0, 0]
Original tensor:
Tensor
    [1, 2, 3, 4, 5, 6, 7]

Example 2: In this example, we are using the tf.tensor1d() method to create the tensor and apply the tf.zerosLike method.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor1d([1, 2, 3]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)


Tensor
    [0, 0, 0]
Original tensor:
Tensor
    [1, 2, 3]

Example 3: In this example, we are using the tf.tensfor2d() method to create the tensor and apply the tf.zerosLike method.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor2d([[1, 2], [3, 4]]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)


​Tensor
    [[0, 0],
     [0, 0]]
Original tensor:
Tensor
    [[1, 2],
     [3, 4]]

Example 4: In this example, we will use the tensor3d() method to create the tensor and apply the tf.zerosLike() method.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor3d([[[1], [2]], [[3], [4]]]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)


Tensor
    [[[0],
      [0]],

     [[0],
      [0]]]
Original tensor:
Tensor
    [[[1],
      [2]],

     [[3],
      [4]]]

Example 5: In this example, we use tensor4d() method to create the tensor and apply the tf.zerosLike() method.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor4d([[[[1], [2]], [[3], [4]]]])
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)


Tensor
    [[[[0],
       [0]],

      [[0],
       [0]]]]
Original tensor:
Tensor
    [[[[1],
       [2]],

      [[3],
       [4]]]]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS