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


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

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

tf.layers.depthwiseConv2d() 函數用於對數據應用深度可分離的 2D 卷積運算。

用法:

tf.layers.depthwiseConv2d(args)

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

  • args:它是一個接受以下屬性的對象。
    • 內核大小(數字|數字[]):卷積窗口的尺寸。如果 kernelSize 是數字,則卷積窗口將為正方形。
    • 深度乘數(數字):對於每個輸入通道,深度卷積輸出通道的數量。 FiltersIn * 深度乘數將等於深度卷積輸出通道的總數。 1 是默認值。
    • depthwiseInitializer:深度內核矩陣的初始值設定項。 GlorotNormal 是默認值。
    • depthwiseConstraint:深度核矩陣的約束。
    • depthwiseRegularizer:深度核矩陣的正則化函數。
    • 步幅(數字|數字[]):每個維度的卷積步長。如果步幅是數字,則兩個維度的步幅相等。
    • padding:填充模式。
    • dataFormat:數據格式。這指定了輸入中維度的排序順序。 ChannelLast 是默認值。
    • dilationRate:在每個維度中,用於擴張卷積的擴張率。它應該是一個整數或一個二元或three-int 數組。
    • activation:該層的激活函數。
    • useBias(布爾值):該層是否有偏差向量。 True 是默認值。
    • kernelInitializer:卷積核權重矩陣的初始值設定項。
    • biasInitializer:偏置向量的初始值設定項。
    • kernelConstraint:卷積核權重的約束。
    • biasConstraint:偏置向量的約束。
    • kernelRegularizer:應用於核權重矩陣的正則化函數。
    • biasRegularizer:應用於偏置向量的正則化函數。
    • activityRegularizer: 應用於激活的正則化函數。
    • inputShape: 如果設置了此屬性,它將用於構造一個輸入層,該輸入層將插入到該層之前。
    • batchInputShape:如果設置了此屬性,將創建一個輸入層並將其插入到該層之前。
    • batchSize:如果未提供batchInputShape而提供了inputShape,則使用batchSize來構建batchInputShape。
    • dtype:這是該層的數據類型。 float32 是默認值。該參數僅適用於輸入層。
    • name:這是圖層的名稱,是字符串類型。
    • trainable:如果該層的權重可以通過擬合來改變。 True 是默認值。
    • weights:圖層的初始權重值。
    • inputDType:它用於舊版支持。

返回:它返回一個對象(DepthwiseConv2D)。

示例 1:

Javascript


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

輸出:

Tensor
   [[[[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304]],
     [[0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],
      [0.25322, -0.140555, 0.4617286, -0.447692, -0.5744522, -0.0203538, 0.3361098, -0.7265304],

示例 2:

Javascript


import * as tf from "@tensorflow/tfjs";
const input = tf.input({ shape: [4, 4, 1] });
const depthwiseConv2DLayer = tf.layers.depthwiseConv2d({ 
    kernelSize: 3 
});
const output = depthwiseConv2DLayer.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
   [[[[2.8932226],
      [2.8629632]],
     [[2.7721865],
      [2.7419279]]]]

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



相關用法


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