Open In App

Tensorflow.js tf.layers.lstm() Function

Last Updated : 12 Dec, 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.

tf.layers.lstm() function is used for creating an RNN layer consisting of one LSTMCell and the apply method of LSTM operates on a sequence of inputs. The shape of the input needs to be at least 2D with the first dimension being time steps. lstm is the Long-Short Term Memory layer. 

Syntax: 

  tf.layers.lstm( args ); 

Parameters: 

  • args: It specifies the given config object:
    • 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.
    • units: It is a number that indicates the dimensionality of the output shape. 
    • activation: It is a tensor input that is an activation function to use and defaults to a hyperbolic tangent. If you pass null, linear activation will be applied. 
    • useBias: It is a boolean which indicates 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 recurrentInitializer 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 weight matrix. 
    • recurrentRegularizer: It is a regularizer function applied to the recurrentKernel weight matrix.
    • biasRegularizer:  It is a regularizer function applied to the bias vector.
    • kernelConstraint: It is a constraint function applied to the kernel weight matrix.
    • recurrentConstraint:  It is a constraint function applied to the recurrentKernel weight matrix.
    • biasConstraint: It is a constraint function applied to a bias vector.
    • dropout: It is a number between 0 to 1. which indicates the unit to drop for the linear transformation of the input.
    • recurrentDropout: It is a number between 0 to 1. which indicates the unit to drop for the linear transformation of the recurrent state.
    • dropoutFunc: This is a function that is added for test DI purposes.
    • cell: It should be an instance of an RNN cell or an array of instances of RNN cells.
    • returnSequences:  It should be boolean. It defines which both of these weather last output or full sequence return in the output sequence.
    • returnState: It should be boolean. It defines the return last state in output.
    • goBackwards: It should be boolean. It defines whether to process the input in reverse and reverse sequence or not.
    • stateful: It should be boolean. It defines whether to use the last state of the batch as the initial state for the current batch or not.
    • unroll: It should be boolean. It tells whether to use unrolled network else, use a symbolic loop.
    • inputDim: It should be a number. This is used when this layer is used as the first layer of the model. It defines the dimension of input in the layer.
    • inputLength: It should be a number. This field should be when the sequence in input data is constant.
    • inputShape: It should be an array of numbers. This field is used to create the 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 the input shape 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 input shape. batchInputShape : [ batchSize ,  …inputShape].
    • dtype: If this layer is used as the input layer, then this field is used as the 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 the data- type which is used for Legacy support.

Return Value: It returns LSTM.

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs"
 
// Long short term memory layer
const LSTM = tf.layers.lstm({units: 4, returnSequences: true});
 
// Create an input with 10 time steps.
const input = tf.input({shape: [10, 40]});
const output = LSTM.apply(input);
 
// Here we get three value 1st value is unknown batch size;
// 2nd value is the sequence length of `input`,
// 3rd value is the `LSTMCell`'s number of units
console.log(JSON.stringify(output.shape));


Output:

[null,10,4]

Example 2:

Javascript




// Importing the tensorflow.js library
//import * as tf from "@tensorflow/tfjs"
 
// Create a new model with lstm Layer
const LSTM = tf.layers.lstm({units: 4, returnSequences: true});
 
// Create a 3d tensor
const x = tf.tensor([1, 2, 3, 4], [2, 2,1]);
 
// Apply lstm layer to x
const output = LSTM.apply(x);
 
// Print output
output.print()


Output:

Tensor
    [[[0.0829882, -0.0169818, -0.1132356, 0.0627953],
      [0.1211651, -0.0538836, -0.2851864, 0.1661802]],

     [[0.0739072, -0.0640151, -0.296207 , 0.1846195],
      [0.0578168, -0.1612808, -0.5005066, 0.3471084]]]

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads