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


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

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

.metrics.sparseCategoricalAccuracy() 函數是稀疏分類精度度量函數,它使用索引和對數來返回 tf.Tensor 對象。

用法:

tf.metrics.sparseCategoricalAccuracy(yTrue, yPred) 

Parameters: 

  • yTrue:它是規定的真實標簽,即索引,它可以是 tf.Tensor 類型。
  • yPred:它是預測的期望或對數,它可以是 tf.Tensor 類型。

返回值:它返回tf.Tensor對象。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining indices and logits
const y = tf.tensor1d([1, 2, 1, 7]);
const z = tf.tensor2d([[1, 1, 9], [0.2, 0, 1], [0.1], [1.8]]);
  
// Calling metrics.sparseCategoricalAccuracy() 
// method
const sparseCategoricalAccuracy = 
    tf.metrics.sparseCategoricalAccuracy(y, z);
  
// Printing output
sparseCategoricalAccuracy.print();

輸出:

Tensor
    [0, 1, 1, 0]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling metrics.sparseCategoricalAccuracy()
// method and printing output
tf.metrics.sparseCategoricalAccuracy(
    tf.tensor1d([2, 3, null, 'a']), 
    tf.tensor2d([[0, 0, 0], [0, 0, 1], 
    [2, 2, 2], [6, 7, 8]])
).print();

輸出:

Tensor
    [0, 0, 1, 0]

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

相關用法


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