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


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

Tensorflow.js 是Google開發的開源工具包,用於在瀏覽器或節點平台上執行機器學習模型和深度學習神經網絡。它還使開發人員能夠在 JavaScript 中創建機器學習模型,並直接在瀏覽器中或通過 Node.js 使用它們。

tf.layers.maxPooling2d() 函數用於對空間數據應用最大池化操作。

用法:

tf.layers.maxPooling2d (args)

Parameters: 它接受 args 對象,該對象可以具有以下屬性:

  • poolSize: 它用於縮小每個維度(即[垂直、水平])的因子。它是一個整數或一個 two-int 數組。
  • strides:在池化窗口的每個維度中,步幅大小。它是一個整數或需要two-int數組。
  • padding: 對於池化層,要使用的填充類型。
  • dataFormat:對於池化層,要使用的數據格式。
  • inputShape:如果設置了此屬性,它將用於構造一個輸入層,該輸入層將插入到該層之前。
  • batchInputShape:如果設置了此屬性,將創建一個輸入層並將其插入到該層之前。
  • batchSize:如果未提供batchInputShape而提供了inputShape,則使用batchSize來構建batchInputShape。
  • dtype: 這是該層的數據類型。 float32 是默認值。該參數僅適用於輸入層。
  • name: 這是圖層的名稱,是字符串類型。
  • trainable: 如果該層的權重可以通過擬合來改變。 True 是默認值。
  • weights: 圖層的初始權重值。

返回:它返回對象 (MaxPooling2D)。

示例 1:

Javascript


import * as tf from "@tensorflow/tfjs"; 
  
const input = tf.input({ shape: [4, 4, 4] }); 
const maxPoolingLayer = tf.layers.maxPooling2d({ poolSize: [2, 2] }); 
const output = maxPoolingLayer.apply(input); 
  
const model = tf.model({ inputs: input, outputs: output }); 
model.predict(tf.ones([1, 4, 4, 4])).print();

輸出:

Tensor
   [[[[1, 1, 1, 1],
      [1, 1, 1, 1]],
     [[1, 1, 1, 1],
      [1, 1, 1, 1]]]]

示例 2:

Javascript


import * as tf from "@tensorflow/tfjs"; 
  
const input = tf.input({ shape: [4, 4, 1] }); 
  
const maxPoolingLayer =  
    tf.layers.maxPooling2d({ poolSize: [3, 3] }); 
      
const output = maxPoolingLayer.apply(input); 
  
const model = tf.model({ inputs: input, outputs: output }); 
  
const x = tf.tensor4d([1, 2, 3, 4, 5, 6, 7,  
    8, 9, 10, 11, 12, 13, 14, 15, 16], [1, 4, 4, 1]); 
  
model.predict(x).print();

輸出:

Tensor
    [ [ [[11],]]]

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



相關用法


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