Tensorflow.js tf.zerosLike()函数
Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。 tf.zeroslIke()用于创建一个tf.tensor ,通过传递参数值,所有元素都设置为 '0',形状与给定张量相同。
句法:
tf.zerosLike(value)
参数:它接受上面提到的单个参数,如下所述:
- value:张量的值,可以是简单的或嵌套的 Array 或 TypedArray 的数字。我们在这里传递所需形状的张量。
返回值:它返回所需形状的张量。
注意:上述函数不会改变原始张量。
示例 1:在此示例中,我们使用 tf.tensor 使用 tf.zeroslike() 方法。
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)
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)
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)
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)
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, 0, 0, 0]
Original tensor:
Tensor
[1, 2, 3, 4, 5, 6, 7]
示例 2:在此示例中,我们使用 tf.tensor1d() 方法创建张量并应用 tf.zerosLike 方法。
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]
示例 3:在此示例中,我们使用 tf.tensfor2d() 方法创建张量并应用 tf.zerosLike 方法。
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]]
示例 4:在此示例中,我们将使用 tensor3d() 方法创建张量并应用 tf.zerosLike() 方法。
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]]]
示例 5:在此示例中,我们使用 tensor4d() 方法创建张量并应用 tf.zerosLike() 方法。
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]]]]
参考: https://js.tensorflow.org/api/latest/#zerosLike