Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.layers.stackedRNNCells() Function

Tensorflow.js tf.layers.stackedRNNCells() Function

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. 

Syntax:

tf.layers.stackedRNNCells(arge); 

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:

Javascript




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);
 
console.log(JSON.stringify(output.shape));


Output:

[null,8]

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. 

Javascript




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


Output:

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

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments