Open In App

Tensorflow.js tf.layers.bidirectional() 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 tf.layers.bidirectional function is a bidirectional wrapper for RNNs layer.



Syntax:

tf.layers.bidirectional( args )

Parameters: This function accepts objects as parameters with the following fields:



Returns: It returns Bidirectional.

Example 1:




import * as tf from '@tensorflow/tfjs'
 
// Bidirectional layer
const k = tf.layers.bidirectional({
   layer: tf.layers.dense({units: 4}),
   batchInputShape: [32, 10, 16],
});
 
// Creating input layer
const input = tf.input({shape: [10,16,32]});
const output = k.apply(input);
 
// Printing the Input Shape
console.log(JSON.stringify(output.shape));

Output:

[null,10,16,8]

Example 2:




import * as tf from '@tensorflow/tfjs'
 
// Instance of RNN layer
const RNN = tf.layers.dense({units: 3});
// Creating Bidirectional layer
const k = tf.layers.bidirectional({
   layer: RNN,
   mergeMode: 'sum',
   batchInputShape: [8, 4, 2],
});
 
// Create a 3d tensor
const x = tf.tensor([1, 2, 3, 4], [4, 1]);
 
// Apply Bidirectional layer to x
const output = k.apply(x);
 
// Print output
output.print()

Output:

Tensor
    [[-0.6737164, -1.6011676, 1.9193256],
     [-1.3474327, -3.2023351, 3.8386512],
     [-2.0211492, -4.8035026, 5.7579765],
     [-2.6948655, -6.4046702, 7.6773024]]

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


Article Tags :