當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.metrics.meanAbsoluteError()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

tf.metrics.meanAbsoluteError()用於計算平均絕對誤差。平均絕對誤差定義為兩個張量的絕對差的平均值。其中,均值應用於特征尺寸。它需要兩個張量作為參數。

mean(abs(Prediction - True))

用法:

tf.metrics.meanAbsoluteError(Tensor1, Tensor2);

Parameters: 

  • Tensor1:這是真張量。
  • Tensor2:它是預測張量。

返回值:它返回平均絕對誤差的張量。



範例1:在此示例中,我們給出兩個一維張量作為參數,而metrics.meanAbsoluteError函數將計算平均絕對誤差並返回一個張量。

Javascript


// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensors
const True = tf.tensor([1,2,3]);
const Prediction = tf.tensor([3,2,1]);
  
// Calculating mean absolute error
const error = tf.metrics.meanAbsoluteError(True, Prediction);
  
// Printing the tensor
error.print();

輸出:

Tensor
    1.3333333730697632

範例2:在此示例中,我們給出兩個2d張量作為參數,metrics.meanAbsoluteError函數將計算平均絕對誤差並返回張量。

Javascript


// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
  
// Defining the value of the tensors
const True = tf.tensor([[1,2,3],[2,4,1]]);
const Prediction = tf.tensor([[3,2,1],[5,2,1]]);
  
// Calculating mean absolute error
const error = tf.metrics.meanAbsoluteError(True, Prediction);
  
// Printing the tensor
error.print();

輸出:

Tensor
    [1.3333334, 1.6666667]

參考:https://js.tensorflow.org/api/latest/#metrics.meanAbsoluteError

相關用法


注:本文由純淨天空篩選整理自nikhilchhipa9大神的英文原創作品 Tensorflow.js tf.metrics.meanAbsoluteError() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。