📜  Tensorflow.js tf.zeros()函数

📅  最后修改于: 2022-05-13 01:56:45.187000             🧑  作者: Mango

Tensorflow.js tf.zeros()函数

Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。

tf.zeros()函数用于创建一个所有元素都设置为零的新张量。

句法:

tf.zeros(shape, dataType)

参数:

  • shape:它采用我们要生成的张量的形状。
  • dataType:它是结果元素中张量的类型。它可以是“float32”、“int32”或“bool”。它默认为“float32”值。它是一个可选参数。

返回值:它返回给定形状的张量,所有元素都设置为零。

示例 1:在此示例中,我们使用 tf.zeros() 创建一个形状为 1*1 的张量。

Javascript
// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a shape variable
// which stores the shape
var shape = [1,1]
 
// Creating the tensor
var val = tf.zeros(shape)
 
// Printing the tensor
val.print()


Javascript
// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a shape variable
// which stores the shape
var shape = [2, 2]
 
// Creating a d_Type variable
// which stores the data-type
var d_Type = 'bool'
 
// Creating the tensor
var val = tf.zeros(shape, d_Type)
 
// Printing the tensor
val.print()


Javascript
// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a shape variable
// which stores the shape
var shape = [3,3]
 
// Creating the tensor
var val = tf.zeros(shape)
 
// Printing the tensor
val.print()


输出:

Tensor
     [[0],]

示例 2:在此示例中,我们使用 tf.zeros() 和 dataType 参数创建一个形状为 2*2 的张量。

Javascript

// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a shape variable
// which stores the shape
var shape = [2, 2]
 
// Creating a d_Type variable
// which stores the data-type
var d_Type = 'bool'
 
// Creating the tensor
var val = tf.zeros(shape, d_Type)
 
// Printing the tensor
val.print()


输出:

Tensor
    [[false, false],
     [false, false]]

示例 3:在此示例中,我们使用 tf.zeros() 创建一个形状为 1*1 的张量。

Javascript

// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating a shape variable
// which stores the shape
var shape = [3,3]
 
// Creating the tensor
var val = tf.zeros(shape)
 
// Printing the tensor
val.print()


输出:

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

参考: https://js.tensorflow.org/api/latest/#zeros