Saturday, October 25, 2025
HomeLanguagesJavascriptTensorflow.js tf.layers.timeDistributed() Function

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:

  • layer: It is the specified layer to be wrapped.
  • inputShape: It is used to create an input layer to insert before this layer. The batchInputShape will be used when both inputShape and batchInputShape are defined. This parameter is relevant only for the input layers i.e. the first layer of a model. It is an optional parameter.
  • batchInputShape: This parameter will be used when both inputShape and batchInputShape are defined. This parameter is relevant only for the input layers i.e. the first layer of a model. It is an optional parameter.
  • batchSize: It is used to create the batchInputShape when inputShape is given, but batchInputShape is not given.
  • dtype: It is the data-type for this layer and its default value is ‘float32’. This parameter is relevant only for the input layers i.e. the first layer of a model.
  • name: It is the name for this layer.
  • trainable: Its default value is true. It says whether the weights of this layer are updatable by fit or not.
  • weights: It is the first weight values of the layer.
  • inputDType: It is recommended not to use for new code. It is for legacy support.

Return Value: It returns a TimeDistributed object.

Example 1: 

Javascript




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

Javascript




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

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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS