Tensorflow.js tf.zerosLike() Function
Last Updated :
28 Apr, 2021
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
import * as tf from "@tensorflow/tfjs"
var val = tf.tensor([1, 2, 3, 4, 5, 6, 7]);
tf.zerosLike(val).print()
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
import * as tf from "@tensorflow/tfjs"
var val = tf.tensor1d([1, 2, 3]);
tf.zerosLike(val).print()
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
import * as tf from "@tensorflow/tfjs"
var val = tf.tensor2d([[1, 2], [3, 4]]);
tf.zerosLike(val).print()
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
import * as tf from "@tensorflow/tfjs"
var val = tf.tensor3d([[[1], [2]], [[3], [4]]]);
tf.zerosLike(val).print()
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
import * as tf from "@tensorflow/tfjs"
var val = tf.tensor4d([[[[1], [2]], [[3], [4]]]])
tf.zerosLike(val).print()
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