Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.layers.bidirectional() Function

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 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:

Javascript




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

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