Open In App

Tensorflow.js tf.layers.timeDistributed() Function

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.

The tf.layers.timeDistributed() function is used to apply the wrap of a layer to every temporal slice of a specified input. The given input must be at least 3D, and the index 1 for the dimension will be considered as the temporal dimension.



Syntax:

tf.layers.timeDistributed(args)

Parameters: This function accepts a single parameter, args, which can be used to specify the following properties:



Return Value: It returns a TimeDistributed object.

Example 1: 




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a model
const model = tf.sequential();
  
// Calling the tf.layers.timeDistributed() function
const a = model.add(tf.layers.timeDistributed({
   layer: tf.layers.dense({units: 8}),
    
   // Considering a sequence of 5 vectors
   // of 10 dimensions
   inputShape: [5, 10],
}));
  
// Getting the model.outputShape
console.log(JSON.stringify(model.outputs[0].shape));

Output: 

[null,5,8]

Example 2: 




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a model
const model = tf.sequential();
  
// Calling the tf.layers.timeDistributed() function
const a = model.add(tf.layers.timeDistributed({
    
   // Initializing the first layer with inputShape
   layer: tf.layers.dense({units: 12}),
    
   // Considering a sequence of 5 vectors
   // of 10 dimensions
   inputShape: [5, 10],
}));
  
// In the second layer, there is no 
// need for `inputShape`
model.add(tf.layers.timeDistributed(
  {layer: tf.layers.dense({units: 32})}
));
  
// Getting the model.outputShape
console.log(JSON.stringify(model.outputs[0].shape));

Output:

[null,5,32]

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


Article Tags :