📜  Tensorflow.js tf.metrics.meanSquaredError()函数(1)

📅  最后修改于: 2023-12-03 15:20:35.319000             🧑  作者: Mango

Tensorflow.js tf.metrics.meanSquaredError()函数介绍

简介

Tensorflow.js中的tf.metrics.meanSquaredError()函数是一个用于计算均方误差(Mean Squared Error)的度量函数。均方误差(MSE)是一种衡量预测值与真实值差距的方法,常用于回归任务中。

使用方法

tf.metrics.meanSquaredError()函数接受两个张量作为输入,分别为预测值和真实值,返回一个表示均方误差的标量张量。

const yTrue = tf.tensor2d([[1, 2], [3, 4]]);
const yPred = tf.tensor2d([[4, 3], [2, 1]]);

const mse = tf.metrics.meanSquaredError(yTrue, yPred);
console.log('Mean Squared Error:', mse.dataSync()); 
// Output: Mean Squared Error: 3.5
参数说明
  • yTrue: tf.Tensor 预测值的张量,可为任意形状和类型的张量,但必须和yPred张量的形状一致。
  • yPred: tf.Tensor 真实值的张量,可为任意形状和类型的张量,但必须和yTrue张量的形状一致。
返回值说明

tf.metrics.meanSquaredError()函数返回一个标量张量,表示均方误差的值。

示例说明

下面是一些使用tf.metrics.meanSquaredError()函数的示例:

const yTrue = tf.tensor1d([1, 2, 3, 4]);
const yPred = tf.tensor1d([4, 3, 2, 1]);
const mse = tf.metrics.meanSquaredError(yTrue, yPred);

console.log('Mean Squared Error:', mse.dataSync()); 
// Output: Mean Squared Error: 5

const yTrue2 = tf.tensor2d([[1, 2], [3, 4]]);
const yPred2 = tf.tensor2d([[4, 3], [2, 1]]);
const mse2 = tf.metrics.meanSquaredError(yTrue2, yPred2);

console.log('Mean Squared Error:', mse2.dataSync()); 
// Output: Mean Squared Error: 3.5
总结

tf.metrics.meanSquaredError()函数是一个用于计算均方误差的度量函数,常用于回归任务中。传入的参数是预测值和真实值两个张量,返回一个标量张量,表示均方误差的值。