Open In App

Tensorflow.js tf.layers.convLstm2dCell() Function

Last Updated : 26 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The tf.layers.convLstm2dCell() is used to create ConvLSTM2DCell which is different from the ConvRNN2D subclass ConvLSTM2D and the call method takes the input data of only a single time step and return the cell’s output at the time step. convLstm2dCell is the Cell class for ConvLSTM2D.

Syntax:

tf.layers.convLstmd2dCell( args )

Parameters: 

  • args: It takes an object with the following parameters:
    • activation: It is the activation function that is to be used. Default is a hyperbolic tangent. If null is passed then the ‘linear’ activation function will be used.
    • useBias: It specifies whether to use a bias vector or not.
    • kernelInitializer: It is the Initializer of the kernel weight matrix, which is used for linear transformation of the inputs.
    • recurrentInitializer: It is the Initializer of the recurrent_Kernel weight matrix, which is used for linear transformation of the recurrent state.
    • biasInitializer: It is the initializer for the bias vector.
    • kernelRegularizer: It is a regularizer function applied to the kernel weights matrix. 
    • recurrentRegularizer: It is a regularizer function applied to the recurrentKernel weights matrix.
    • biasRegularizer:  It is a regularizer function applied to the bias vector.
    • kernelConstraint: It is a constraint function applied to the kernel weights matrix.
    • recurrentConstraint:  It is a constraint function applied to the recurrentKernel weights matrix.
    • biasConstraint: It is a constraint function applied to the bias vector.
    • dropout: It is a number between 0 to 1 which indicate the unit to drop for the linear transformation of the input.
    • recurrentDropout: It is the fraction number that drops the linear transformation of the recurrent state.
    • dropoutFunc: This is used for the test DI. 
    • inputShape: It should be an array of numbers. This field is used to create an input layer which is used to be inserted before this layer.
    • batchInputShape: It should be an array of numbers. This field will be used if inputShape and this field are provided as a parameter for creating the input layer which is used to insert before this layer.
    • batchSize: It should be a number. In the absence of batchInputShape this field is used to create batchInputShape with inputShape. batchInputShape : [ batchSize ,  …inputShape].
    • dtype: If this layer is used as the input layer, then this field is used as data-type for this layer.
    • name: It should be a string type. this field defines the name for this layer.
    • trainable: It should be boolean. This field defines whether the weights of this layer are trainable with fit or not.
    • weights: This should be a tensor that defines the initial weight value for this layer.
    • inputDType: This is a data type that is used for Legacy support.
    • recurrentActivation: It specifies the activation function which will be used for the recurrent step. The default value of this parameter is hard sigmoid.
    • unitForgetBias: It should be boolean. It is used to add 1 to the bias of the forget gate at initialization. 
    • implementation: It specifies the implementation mode. It can be either 1 or 2. For superior performance, implementation is recommended.
    • filters: It is a number of filters in convolution.
    • kernelSize: It is a dimension that the convolution window will have.
    • strides: It is strides of convolution layer in each dimension. 
    • padding: It should be ‘valid’, ‘same’, and ‘causal’. It defines the padding mode. 
    • dataFormat: It defines the format of data, which tells the ordering of the dimensions in the inputs.
    • dilationRate: The dilation rate to which the convolution layer should dilate in each dimension.

Parameters: It returns ConvLSTMCell

Example 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();


Output:

__________________________________________________________________________________________
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
__________________________________________________________________________________________

Example 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();


Output:

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],]]]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads