Open In App

Tensorflow.js tf.layers.gruCell() Function

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.gruCell( ) function is used to create a cell class for GRU.



Syntax:

tf.layers.gruCell (args)

Parameters:



Return Value: It returns GRUCell.

Example 1:In this example, GRUCell is distinct from the RNN subclass GRU in that its apply method takes the input data of only a single time step and returns the cell’s output at the time step, while GRU takes the input data over a number of time steps.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining input elements
const cell = tf.layers.gruCell({units: 3});
const input = tf.input({shape: [11]});
const output = cell.apply(input);
  
console.log(JSON.stringify(output.shape));

Output:

[null,11]

Example 2:In this example, Instance(s) of GRUCell can be used to construct RNN layers. The most typical use of this workflow is to combine a number of cells into a stacked RNN cell (i.e., StackedRNNCell internally) and use it to create an RNN.




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

Output:

[null,20,16]

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


Article Tags :