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


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

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。它還可以幫助開發人員使用JavaScript語言開發ML模型,並可以直接在瀏覽器或Node.js中使用ML。

tf.metrics.categoricalCrossentropy() 函數分類地找到目標和輸出張量之間的分類交叉熵。

用法:

tf.metrics.categoricalCrossentropy(yTrue, yPred)

參數:此函數接受以下兩個參數:

  • yTrue:它是真值張量,類型為 tf.Tensor。
  • yPred:它是預測張量,它的類型是 tf.Tensor。

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



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initialising first 2d tensor.
let geek1 = tf.tensor2d([[2, 4], [1, 3]]);
  
// Initialising second 2d tensor.
let geek2 = tf.tensor2d([11, 22, 33, 44], [2, 2]);
  
// Using .categoricalCrossentropy() function
// to find categorical accuracy metric.
let result = tf.metrics.categoricalCrossentropy(geek1, geek2);
  
// Printing the result.
result.print();

輸出:

Tensor
    [3.8190854, 2.526145]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using .categoricalCrossentropy() function
// to find categorical accuracy metric
tf.metrics.categoricalCrossentropy(
  tf.tensor1d([1, 2]), 
   tf.tensor2d([50], [1, 1]))
      .print();

輸出:

Tensor
    [4e-7]

參考:https://js.tensorflow.org/api/3.6.0/#metrics.categoricalCrossentropy

相關用法


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