Open In App

Tensorflow.js tf.layers.lstmCell() Function

Last Updated : 30 Jun, 2021
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 layers.lstmCell() function is used for Cell class for LSTM. It is separate from the RNN subclass.

Syntax:

tf.layers.lstmCell(args)

Parameters: The function contains an args object that contains the following parameters:

  • recurrentactivation: It is used for the activation of recurrent steps.
  • unitForgetBias: It is a boolean used for forget gate at initialization.
  • implementation: It is an integer either that specifies the modes of implementation. MODE 1 is used to structure its operations as a larger number of smaller dot products and additions. MODE 2 is used to batch them into fewer, larger operations.
  • units: It is a number whether it is an integer number or dimensionality of the output space.
  • activation: It is used for the function to use.
  • useBias: It is a boolean whether the layer uses a bias vector.
  • kernelInitializer: It is used for the linear transformation of the inputs.
  • recurrentInitializer: It is used for linear transformation of the recurrent state.
  • biasInitializer: It is used for the bias vector.
  • kernelRegularizer: It is a string used for the Regularizer function applied to the kernel weights matrix.
  • recurrentRegularizer: It is a string used for the Regularizer function applied to the recurrent_kernel weights matrix.
  • biasRegularizer: It is a string used for the Regularizer function applied to the bias vector.
  • kernelConstraint: It is a string used for the Constraint function applied to the kernel weights matrix.
  • recurrentConstraint: It is a string used for the Constraint function applied to the recurrentKernel weights matrix.
  • iasConstraint: It is a string used for the Constraint function applied to the bias vector.
  • dropout: It is a number between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs.
  • recurrentDropout: It is a number between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state.
  • inputShape: It is a number used to create an input layer to insert before this layer.
  • batchInputShape: It is a number used to create an input layer to insert before this layer.
  • batchSize: It is a number used to construct the batchInputShape.
  • dtype: This argument is only applicable to input layers.
  • name: It is a string that is for the layer.
  • trainable: It is a boolean which is used Whether the weights of this layer are updatable by fit.
  • weights: It is the initial weight values of the layer.
  • inputDType: It is used for Legacy support. It is not useable for new codes.

Return value: It returns LSTMCell.

 

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling the layers.lstmCell 
// function and printing the output
const cell = tf.layers.lstmCell({units: 3});
const input = tf.input({shape: [120]});
const output = cell.apply(input);
  
console.log(JSON.stringify(output.shape));


Output:

[null, 120]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
const cells = [
   tf.layers.lstmCell({units: 6}),
   tf.layers.lstmCell({units: 10}),
];
const rnn = tf.layers.rnn(
  {cell: cells, returnSequences: true}
);
  
// Create an input with 10 time steps 
// and a length-20 vector at each step
const input = tf.input({shape: [40, 60]});
const output = rnn.apply(input);
  
console.log(JSON.stringify(output.shape));


Output:

[null, 30, 8]

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



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

Similar Reads