tf.moments() 用于计算作为函数参数传递的张量的均值和方差。均值和方差是通过在参数中传递的轴上聚合张量的内容来计算的。
用法:
tf.moments(tensor, axis, keepdims)
参数:该方法接受以下三个参数:
- tensor:它用于表示需要计算均值和方差的张量(向量)。
- axis:表示需要计算均值和方差的轴的整数向量。
- keepdims:保持维度是一个布尔变量,表示产生的矩是否与输入张量具有相同的维度。
返回值:它返回两个 Tensor 对象,即计算的均值和方差。
范例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);
输出:
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], In在这个例子中,我们将参数轴设置为 [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() acorss 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:在此示例中,我们将通过更改轴 = [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() 来执行相同的操作。
相关用法
- PHP imagecreatetruecolor()用法及代码示例
- p5.js year()用法及代码示例
- d3.js d3.utcTuesdays()用法及代码示例
- PHP ImagickDraw getTextAlignment()用法及代码示例
- PHP Ds\Sequence last()用法及代码示例
- PHP Imagick floodFillPaintImage()用法及代码示例
- PHP geoip_continent_code_by_name()用法及代码示例
- d3.js d3.map.set()用法及代码示例
- PHP GmagickPixel setcolor()用法及代码示例
- Tensorflow.js tf.layers.embedding()用法及代码示例
- PHP opendir()用法及代码示例
- PHP cal_to_jd()用法及代码示例
- d3.js d3.bisectLeft()用法及代码示例
- PHP stream_get_transports()用法及代码示例
注:本文由纯净天空筛选整理自harshshukla5大神的英文原创作品 Tensorflow.js tf.moments() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。