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


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

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

tf.max()函數用於根據指定的張量在其整個維度上計算最大值。它沿軸的尺寸減少了給定的輸入元素。如果參數“keepDims”為true,則減小的尺寸保留為長度1,否則張量的等級減小1。如果axes參數沒有條目,則它返回具有所有減小尺寸的單個元素的張量。

用法:

tf.max (x, axis?, keepDims?)

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

  • x:正在為其計算最大值的輸入張量。
  • axis:指定要減小的尺寸。默認情況下,它會縮小所有尺寸。它是可選參數。
  • keepDims:如果此參數值為true,它將保留長度為1的縮小尺寸,否則Tensor的等級減小1。這也是可選參數。

返回值:它返回最大值的張量。



範例1:

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.tensor1d([3, 5]);
const c = tf.tensor1d([2, 4, 7]);
  
// Calling the .max() function over 
// the above tensors
a.max().print();
b.max().print();
c.max().print();

輸出:

Tensor
   1
Tensor
   5
Tensor
   7

範例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([3, 5, 2, 8], [2, 2]);
const c = tf.tensor1d([2, 4, 7]);
  
// Initializing a axis parameters
const axis1 = -1;
const axis2 = -2;
const axis3 = 0;
  
// Calling the .max() function over 
// the above tensors
a.max(axis1).print();
b.max(axis2, true).print();
c.max(axis1, false).print();
b.max(axis3, false).print();

輸出:

Tensor
   1
Tensor
    [[3, 8],]
Tensor
   7
Tensor
   [3, 8]

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

相關用法


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