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


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

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

tf.argMax()函數用於沿軸返回指定張量的最大值的索引。

輸出結果具有與輸入相同的形狀,但沿軸的尺寸已刪除。

用法:

tf.argMax (x, axis)

參數:此函數接受兩個參數,如下所示:



  • x:輸入張量。
  • axis:指定要減小的尺寸。它是一個可選參數,默認值為0。

返回值:它返回沿軸的最大值的索引的張量。

範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a some tensors 
const a = tf.tensor1d([1, 0]);
const b = tf.tensor1d([3, 5]);
const c = tf.tensor1d([6, 3, 5, 12]);
  
// Calling the .argMax() function over 
// the above tensors
a.argMax().print();
b.argMax().print();
c.argMax().print();

輸出:

Tensor
   0
Tensor
   1
Tensor
   3

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a some tensors 
const a = tf.tensor1d([0, 1]);
const b = tf.tensor2d([9, 5, 2, 8], [2, 2]);
const c = tf.tensor1d([6, 4, 7]);
  
// Initializing a axis parameters
const axis1 = -1;
const axis2 = -2;
const axis3 = 0;
  
// Calling the .argMax() function over 
// the above tensors
a.argMax(axis1).print();
b.argMax(axis2).print();
c.argMax(axis3).print();

輸出:

Tensor
   1
Tensor
   [0, 1]
Tensor
   2

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

相關用法


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