Skip to content
Related Articles
Open in App
Not now

Related Articles

Tensorflow.js tf.zerosLike() Function

Improve Article
Save Article
  • Last Updated : 28 Apr, 2021
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!