Skip to content
Related Articles
Open in App
Not now

Related Articles

Tensorflow.js tf.layers.stackedRNNCells() Function

Improve Article
Save Article
  • Last Updated : 12 Dec, 2022
Improve Article
Save Article

Introduction: Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. 

Tensorflow.js tf.layers.stackedRNNCells() function is used to stack the RNN cell and make them to behave as a single cell. 



Parameters: Above method accepts the following parameter:

  • args: This is an object type. It has the following fields:
    • cells: It is an Array of instance RNNCell that should be stacked together.
    • InputShape: It should be null or array of numbers. It is used to create the input layer which is inserted before this layer. It is used to input layer only.
    • batchinputShape: It should be null of an array of numbers. It is used to create the input layer which is inserted before this layer. It has more priority than inputShape, so if batchinputShape is defined it is used for creating the input layer.
    • batchSize: It should be a number. In case of absence of batchinputShape, It is used to create batchinputShape with InputShape Which will be [ batchSize, …inputSize ].
    • dtype: It is the datatype for the input layer. Default data – type for this input layer is float32.
    • name: It should be a string. It defines the name of the input layer.
    • weights: It should be tensor. Which defines the initial weight value of the input layer.
    • inputDtype: It should be data-type. It is used to support Legacy.

Returns: It returns an object (StackedRNNCells).

Example 1: In this example, we will see how simple RNNCells are stacked with tf.layers.stackedRNNCells() and work as single RNNCells:


import * as tf from "@tensorflow/tfjs"
// Creating RNNcells for stack
const cell1 = tf.layers.simpleRNNCell({units: 2});
const cell2 = tf.layers.simpleRNNCell({units: 4});
// Stack all the RNNCells
const cell = tf.layers.stackedRNNCells({ cells: [cell1, cell2]});
const input = tf.input({shape: [8]});
const output = cell.apply(input);



Example 2: In this example, we will combine a number of cells into a stacked RNN cell with the help of stackedRNNCells and used to create RNN. 


import * as tf from "@tensorflow/tfjs";
// Creating simple RNNCell for stacking together
const cell1 = tf.layers.simpleRNNCell({ units: 4 });
const cell2 = tf.layers.simpleRNNCell({ units: 8 });
const cell3 = tf.layers.simpleRNNCell({ units: 12 });
const cell4 = tf.layers.simpleRNNCell({ units: 16 });
const stacked_cell = tf.layers.stackedRNNCells({
    cells: [cell1, cell2, cell3, cell4],
    name: "Stacked_RNN",
    dtype: "int32",
const rnn = tf.layers.rnn({ cell: stacked_cell, returnSequences: true });
// Create input with 10 steps and 20 length vector at each step.
const input = tf.input({ shape: [8, 32] });
const output = rnn.apply(input);
console.log("Shape of output should be in : ", JSON.stringify(output.shape));


Shape of output should be in :  [null,8,16]


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!