Tensorflow.js tf.zeros() Function
Last Updated :
04 Jul, 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.zeros() function is used to create a new tensor with all elements set to zero.
Syntax:
tf.zeros(shape, dataType)
Parameters:
- shape: It takes the shape of the tensor we are going to generate.
- dataType: It is the type of tensor in the resulting element. It can be a ‘float32′, ‘int32′ or ‘bool’. It defaults to the “float32” value. It is an optional parameter.
Return Value: It returns the tensor of the given shape with all elements set to zero.
Example 1: In this example, we are creating a tensor of shape 1*1 using tf.zeros().
Javascript
import * as tf from "@tensorflow/tfjs"
var shape = [1,1]
var val = tf.zeros(shape)
val.print()
|
Output:
Tensor
[[0],]
Example 2: In this example, we are creating a tensor of shape 2*2 using tf.zeros() and with dataType parameter.
Javascript
import * as tf from "@tensorflow/tfjs"
var shape = [2, 2]
var d_Type = 'bool'
var val = tf.zeros(shape, d_Type)
val.print()
|
Output:
Tensor
[[false, false],
[false, false]]
Example 3: In this example, we are creating a tensor of shape 1*1 using tf.zeros().
Javascript
import * as tf from "@tensorflow/tfjs"
var shape = [3,3]
var val = tf.zeros(shape)
val.print()
|
Output:
Tensor
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Reference:https://js.tensorflow.org/api/latest/#zeros
Share your thoughts in the comments
Please Login to comment...