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


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

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

tf.layers.convLstm2dCell() 用於創建 ConvLSTM2DCell,它與 ConvRNN2D 子類 ConvLSTM2D 不同,調用方法僅獲取單個時間步的輸入數據並返回該時間步的單元輸出。 convLstm2dCell 是 ConvLSTM2D 的 Cell 類。

用法:

tf.layers.convLstmd2dCell( args )

參數:

  • args: 它需要一個具有以下參數的對象:
    • activation: 這是要使用的激活函數。默認為雙曲正切。如果傳遞 null,則將使用 ‘linear’ 激活函數。
    • useBias: 它指定是否使用偏差向量。
    • kernelInitializer: 它是核權重矩陣的初始化器,用於輸入的線性變換。
    • recurrentInitializer: 它是recurrent_Kernel權重矩陣的初始化器,用於循環狀態的線性變換。
    • biasInitializer: 它是偏置向量的初始值設定項。
    • kernelRegularizer: 它是應用於核權重矩陣的正則化函數。
    • recurrentRegularizer: 它是應用於 recurrentKernel 權重矩陣的正則化函數。
    • biasRegularizer:  它是應用於偏置向量的正則化函數。
    • kernelConstraint: 它是應用於核權重矩陣的約束函數。
    • recurrentConstraint:  它是應用於 recurrentKernel 權重矩陣的約束函數。
    • biasConstraint: 它是應用於偏置向量的約束函數。
    • dropout: 它是 0 到 1 之間的數字,指示輸入線性變換要丟棄的單位。
    • recurrentDropout: 它是降低循環狀態線性變換的分數。
    • dropoutFunc: 這用於測試 DI。
    • inputShape: 它應該是一個數字數組。該字段用於創建一個輸入層,該輸入層用於插入到該層之前。
    • batchInputShape: 它應該是一個數字數組。如果提供 inputShape 和此字段作為創建用於插入到該圖層之前的輸入圖層的參數,則將使用此字段。
    • batchSize: 它應該是一個數字。在沒有batchInputShape的情況下,該字段用於使用inputShape創建batchInputShape。 batchInputShape:[batchSize,...inputShape]。
    • dtype: 如果該層用作輸入層,則該字段用作該層的數據類型。
    • name: 它應該是字符串類型。該字段定義該層的名稱。
    • trainable: 它應該是布爾值。該字段定義該層的權重是否可以通過擬合進行訓練。
    • weights: 這應該是一個定義該層初始權重值的張量。
    • inputDType: 這是用於舊版支持的數據類型。
    • recurrentActivation: 它指定將用於循環步驟的激活函數。該參數的默認值為硬 sigmoid。
    • unitForgetBias: 它應該是布爾值。它用於在初始化時將遺忘門的偏差加 1。
    • implementation: 它指定了實現模式。它可以是 1 或 2。為了獲得卓越的性能,建議實施。
    • filters: 它是多個濾波器的卷積。
    • kernelSize: 它是卷積窗口將具有的維度。
    • strides: 它是每個維度上卷積層的步長。
    • padding: 應該是‘valid’, ‘same’和‘causal’。它定義了填充模式。
    • dataFormat: 它定義了數據的格式,它告訴輸入中維度的順序。
    • dilationRate: 卷積層在每個維度上應擴張到的擴張率。

Parameters: 它返回ConvLSTMCell

示例 1:

Javascript


import * as tf from "@tensorflow/tfjs";
// InputShape and Input layer for convLstm2dCell layer
const InputShape = [ 4, 2, 5, 5, 2];
const input = tf.input({ shape: InputShape });
// Creating ConvLstm2dCell 
const convLstm2dCell = tf.layers.convLstm2dCell(
    { filters:4, kernelSize:4});
const output = convLstm2dCell.apply(input);
// Printing summary of layers
const model = tf.model({ inputs: input, outputs: output });
model.summary();

輸出:

__________________________________________________________________________________________
Layer (type)                Input Shape               Output shape              Param #   
==========================================================================================
input6 (InputLayer)         [[null,4,2,5,5,2]]        [null,4,2,5,5,2]          0         
__________________________________________________________________________________________
conv_lst_m2d_cell_ConvLSTM2 [[null,4,2,5,5,2]]        [null,4,2,5,5,2]          1552      
==========================================================================================
Total params: 1552
Trainable params: 1552
Non-trainable params: 0
__________________________________________________________________________________________

示例 2:

Javascript


// Importing header file 
import * as tf from "@tensorflow/tfjs"
// Creating layer for convLstm2dCell 
const filters = 3;
const kernelSize = 3;
const inputShape = [1, 3, 3, 3];
const input = tf.ones(inputShape);
const convLstm2dCell = tf.layers
    .convLstm2dCell({filters, kernelSize});
     
convLstm2dCell.build(input.shape);
const outShape = [1, 1, 1, 3];
const c = tf.zeros(outShape);
const h = tf.zeros(outShape);
// Printing Tensor 
const [first, second, third] = 
    convLstm2dCell.call([input, c, h], {});
     
console.log("First Tensor");
first.print();
console.log("\nSecond Tensor");
second.print();
console.log("\nThird Tensor");
third.print();

輸出:

First Tensor
Tensor
     [ [ [[0.1302425, 0.0364179, 0.0439035],]]]

Second Tensor
Tensor
     [ [ [[0.1302425, 0.0364179, 0.0439035],]]]

Third Tensor
Tensor
     [ [ [[0.3106561, 0.1217758, 0.1326777],]]]

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



相關用法


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