📜  Tensorflow.js tf.moments()函数

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

Tensorflow.js tf.moments()函数

tf.moments()用于计算在函数中作为参数传递的张量的均值和方差。通过在传入参数的轴上聚合张量的内容来计算均值和方差。

句法:

tf.moments(tensor, axis, keepdims)

参数:该方法接受以下三个参数:

  • 张量:用于表示需要计算其均值和方差的张量(向量)。
  • axis:一个整数向量,表示需要计算均值和方差的轴。
  • keepdims:保持维度是一个布尔变量,指示生成的矩是否与输入张量具有相同的维度。

返回值:它返回两个张量对象,即计算的均值和方差。

示例 1:在此示例中,我们将计算一维张量的真实均值和方差。

Javascript
// Creating 1-D Tensor
const tensor = tf.tensor1d([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  
// Calculating mean and Variance using tf.moments()
const value = tf.moments(tensor,[0]);
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Javascript
// Creating 2-D Tensor
tensor = tf.tensor2d([[1,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,axes=[0])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Javascript
// Creating 1-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]]);
  
// Calculating mean and Variance using tf.moments() across axis=[1]
value = tf.moments(tensor,axes=[1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


Javascript
// Creating 2-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,[0,1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);


输出:

Mean:  Tensor
    5 
Variance:  Tensor
    6.666666507720947

示例 2:在此示例中,我们将计算二维张量的均值和方差的浮点值。

Javascript

// Creating 2-D Tensor
tensor = tf.tensor2d([[1,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,axes=[0])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);

输出:

Mean:  Tensor
    [3.6666667, 4.666667, 3] 
Variance:  Tensor
    [6.2222228, 4.2222223, 2]

示例 3:在上面的示例中,均值和方差是跨轴 [0] 计算的,即 [(1+3+7)/3, (2+7+5)/3, (4+4+1)/3 ],在本例中,我们将参数轴设置为 [1]。

Javascript

// Creating 1-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]]);
  
// Calculating mean and Variance using tf.moments() across axis=[1]
value = tf.moments(tensor,axes=[1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);

输出:平均值计算为 [(3+2+4)/3 ,(3+7+4)/3 ,(7+5+1)/3]

Mean:  Tensor
    [3, 4.666667, 4.3333335] 
Variance:  Tensor
    [0.6666667, 2.8888888, 6.2222228]

示例 4:在此示例中,我们将通过更改 axes=[0,1] 来计算完整向量的均值和方差。

Javascript

// Creating 2-D Tensor
tensor = tf.tensor2d([[3,2,4],[3,7,4],[7,5,1]])
  
// Calculating mean and Variance using tf.moments()
value = tf.moments(tensor,[0,1])
  
// Printing mean and variance
console.log("Mean: ",value.mean,"\nVariance: ",value.variance);

输出:

Mean:  Tensor
    4 
Variance:  Tensor
    3.777777910232544

注意:计算均值和方差是张量的归一化。这 tf.moments()在 JavaScript 中可以正常工作,但如果我们在Python中导入 TensorFlow 模块,我们使用tf.nn.moments()来执行相同的操作。