Thursday, February 12, 2026
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

1 COMMENT

Most Popular

Dominic
32497 POSTS0 COMMENTS
Milvus
128 POSTS0 COMMENTS
Nango Kala
6872 POSTS0 COMMENTS
Nicole Veronica
11996 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12090 POSTS0 COMMENTS
Shaida Kate Naidoo
7009 POSTS0 COMMENTS
Ted Musemwa
7246 POSTS0 COMMENTS
Thapelo Manthata
6959 POSTS0 COMMENTS
Umr Jansen
6950 POSTS0 COMMENTS