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:
- layers: It is the instance of RNN layer which is to be wrapped.
- mergeMode: It should be ‘sum’ or ‘mul’ or ‘concat’ or ‘ave’. It is the mode at which the output of forward and backward or RNNs are combined.
- inputShape:It should be an array of numbers. This field is used to create the input layer which is used to be inserted before this layer.
- batchInputShape: It should be an array of numbers. This field will be used if the input shape and this field are provided as a parameter for creating the input layer which is used to insert before this layer.
- batchSize: It should be a number. In the absence of batchInputShape, this field is used to create batchInputShape with input shape. batchInputShape : [ batchSize ,  …inputShape].
- dtype: If this layer is used as the input layer, then this field is used as the data type for this layer.
- name: It should be a string type. this field defines the name for this layer.
- trainable: It should be boolean. This field defines whether the weights of this layer are trainable with fit or not.
- weights: This should be a tensor that defines the initial weight value for this layer.
Returns: It returns Bidirectional.
Example 1:
Javascript
import * as tf from '@tensorflow/tfjs'Â
// Bidirectional layerconst k = tf.layers.bidirectional({   layer: tf.layers.dense({units: 4}),   batchInputShape: [32, 10, 16],});Â
// Creating input layerconst input = tf.input({shape: [10,16,32]});const output = k.apply(input);Â
// Printing the Input Shapeconsole.log(JSON.stringify(output.shape)); |
Output:
[null,10,16,8]
Example 2:
Javascript
import * as tf from '@tensorflow/tfjs'Â
// Instance of RNN layerconst RNN = tf.layers.dense({units: 3});// Creating Bidirectional layerconst k = tf.layers.bidirectional({   layer: RNN,   mergeMode: 'sum',   batchInputShape: [8, 4, 2],});Â
// Create a 3d tensorconst x = tf.tensor([1, 2, 3, 4], [4, 1]);Â
// Apply Bidirectional layer to xconst output = k.apply(x);Â
// Print outputoutput.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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



