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


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

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

.maxPoolWithArgmax() 函數用於確定圖像的 2D 最大池化以及 argmax 列表,即索引。其中,argmax 中的索引是水平的,以便位置 [b, y, x, c] 處的峰值變為壓縮索引:(y * width + x) * channels + c 以防 include_batch_in_index 為假,如果 include_batch_in_index為真,則為 ((b * height + y) * width + x) * channels +c。此外,在展平之前,返回的索引始終在 [0, height) x [0, width) 中。

用法:

tf.maxPoolWithArgmax(x, filterSize, 
    strides, pad, includeBatchInIndex?)

參數:

  • x:指定的輸入張量,其等級為 4 或等級 3,形狀為:[batch, height, width, inChannels]。此外,如果等級為 3,則假定批次大小為 1。它可以是 tf.Tensor4D、TypedArray 或 Array 類型。
  • filterSize:指定形狀的過濾器大小:[filterHeight, filterWidth]。如果過濾器大小是一個奇異數,那麽 filterHeight == filterWidth。它可以是 [number, number] 或 number 類型。
  • strides:形狀池的規定步幅:[strideHeight, strideWidth]。如果 strides 是一個單數,那麽 strideHeight == strideWidth。它可以是 [number, number] 或 number 類型。
  • pad:規定的填充算法類型。它可以是類型 valid、same 或 number。
    • 在這裏,對於相同和步長 1,輸出將具有與輸入相同的大小,而與濾波器大小無關。
    • 對於,‘valid’,在濾波器尺寸大於1*1×1的情況下,輸出應小於輸入。
  • includeBatchInIndex:它是可選的並且是布爾類型。

返回值:它返回 {[name:string]:tf.Tensor}。

範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining input tensor
const x = tf.tensor4d([1, 2, 3, 4], [2, 2, 1, 1]);
  
// Calling maxPoolWithArgmax() method
const result = tf.maxPoolWithArgmax(x, 3, 2, 'same');
  
// Printing output
console.log(result)

輸出:

{
  "result":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      2,
      1,
      1,
      1
    ],
    "dtype":"float32",
    "size":2,
    "strides":[
      1,
      1,
      1
    ],
    "dataId":{
      "id":20
    },
    "id":20,
    "rankType":"4",
    "scopeId":14
  },
  "indexes":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      2,
      1,
      1,
      1
    ],
    "dtype":"float32",
    "size":2,
    "strides":[
      1,
      1,
      1
    ],
    "dataId":{
      "id":21
    },
    "id":21,
    "rankType":"4",
    "scopeId":14
  }
}

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling maxPoolWithArgmax() method
console.log(tf.maxPoolWithArgmax(
    tf.tensor4d([1.1, 2.1, 3.1, 4.1], 
    [1, 2, 2, 1]), [1, 2], [1, 1], 
    'valid', true
));

輸出:

{
  "result":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      1,
      2,
      1,
      1
    ],
    "dtype":"float32",
    "size":2,
    "strides":[
      2,
      1,
      1
    ],
    "dataId":{
      "id":80
    },
    "id":80,
    "rankType":"4",
    "scopeId":54
  },
  "indexes":{
    "kept":false,
    "isDisposedInternal":false,
    "shape":[
      1,
      2,
      1,
      1
    ],
    "dtype":"float32",
    "size":2,
    "strides":[
      2,
      1,
      1
    ],
    "dataId":{
      "id":81
    },
    "id":81,
    "rankType":"4",
    "scopeId":54
  }
}

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


相關用法


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