Skip to content
Related Articles

Related Articles

Tensorflow.js tf.basicLSTMCell() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 14 May, 2022

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.basicLSTMCell() function computes the next state and output of a BasicLSTMCell.

Syntax:

tf.basicLSTMCell (forgetBias, lstmKernel, lstmBias, data, c, h)

Parameters:

  • forgetBias: The forget bias for the cell.
  • lstmKernel: The weights for the cell.
  • lstmBias: The bias for the cell.
  • data: The input to the cell.
  • c: Array of previous cell states.
  • h: Array of previous cell outputs.

Returns: [tf.Tensor2D, tf.Tensor2D]

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const data = tf.tensor2d([7, 51, 50, 54, 24, 1, 48, 75], [4, 2]);
const kernel = tf.tensor2d([49, 62, 47, 93, 12, 80, 
    24, 89, 34, 8, 96, 74, 56, 42, 32, 53, 7, 87, 35, 54], [5, 4]);
const state = tf.tensor2d([97, 56, 32, 29, 57, 6, 8, 75, 26, 20, 1, 17], [4, 3]);
const output = tf.tensor2d([27, 77, 90, 72, 9, 8, 94, 41, 89, 51, 18, 60], [4, 3]);
const basicLSTMCell = tf.basicLSTMCell(0.8, kernel, 2.2, data, state, output);
  
console.log(basicLSTMCella)


Output:

[
 Tensor {
   kept: false,
   isDisposedInternal: false,
   shape: [ 4, 3 ],
   dtype: 'float32',
   size: 12,
   strides: [ 3 ],
   dataId: { id: 19 },
   id: 19,
   rankType: '2',
   scopeId: 0
 },
 Tensor {
   kept: false,
   isDisposedInternal: false,
   shape: [ 4, 3 ],
   dtype: 'float32',
   size: 12,
   strides: [ 3 ],
   dataId: { id: 22 },
   id: 22,
   rankType: '2',
   scopeId: 0
 }
]

Example 2: 

Javascript




import * as tf from "@tensorflow/tfjs";
  
const data = tf.tensor2d([70, 10, 62, 
    55, 74, 85, 66, 9], [4, 2]);
  
const kernel = tf.tensor2d([10, 82, 93, 83, 
    49, 73, 45, 77, 56, 29, 32, 2, 24, 
    39, 34, 91, 95, 61, 76, 69], [5, 4]);
  
const state = tf.tensor2d([29, 40, 79, 61, 
    5, 34, 78, 47, 86, 74, 46, 28], [4, 3]);
  
const output = tf.tensor2d([25, 55, 33, 85, 
    82, 65, 20, 75, 54, 59, 50, 3], [4, 3]);
  
const basicLSTMCell = tf.basicLSTMCell(1.0, 
    kernel, 2.0, data, state, output);
  
const input = tf.input({ shape: [4, 2] });
const simpleRNNLayer = tf.layers.simpleRNN({
    units: 4,
    returnSequences: true,
    returnState: true,
    cell: basicLSTMCell
});
  
let outputs, finalState;
  
[outputs, finalState] = simpleRNNLayer.apply(input);
  
const model = tf.model({
    inputs: input,
    outputs: outputs
});
  
const x = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8], [1, 4, 2]);
  
model.predict(x).print();


Output:

Tensor
   [[[0.8135326, -0.8665518, 0.946215 , 0.8714994],
     [0.9547493, -0.9747651, 0.9873405, 0.9995403],
     [0.9983249, -0.9986398, 0.9996439, 0.9999973],
     [0.9999447, -0.9999344, 0.9999925, 1        ]]]

Reference: https://js.tensorflow.org/api/latest/#basicLSTMCell


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!