Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.layers.dropout() Function

  • Last Updated : 04 Jul, 2021

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.dropout() function is an inbuilt function of Tensorflow.js library. This function is used to prevent overfitting in a model by randomly setting a fraction rate of input units to 0 at each update during training time.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

tf.layers.dropout( {rate} )

Parameters:



  • args: The given object as a parameter.
  • rate: Specifies the fraction of input units to drop. Its value ranges between 0 and 1.
  • noiseShape: List of integers that represents the shape of the dropout that will be multiplied with the input. It is an optional parameter.
  • seed: Specifies random seed. It is an optional parameter.
  • inputShape: If this parameter is defined, it will create another input layer to insert before this layer.
  • batchInputShape: If this parameter is defined, it will create another input layer to insert before this layer.
  • batchSize: Used to construct batchInputShape, if not already specified.
  • dtype: Specifies the data type for this layer. The default value of this parameter is ‘float32’.
  • name: Specifies name for this layer.
  • trainable: Specifies whether the weights of this layer are updated by fit.
  • weights: Specifies the initial weight values of the layer.
  • inputDType: It is used to denote the inputDType and its value can be ‘float32’ or ‘int32’ or ‘bool’ or ‘complex64’ or ‘string’.

Return value: It returns the Dropout.

Example 1: We will create a new model and add dropout layer to it.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define the model
const model = tf.sequential({
    layers: [tf.layers.dense({ 
        units: 1, inputShape: [10] 
    })],
});
  
// Add dropout to model
model.add(tf.layers.dropout({ rate: 0.25 }));
    
// Compile the model
model.compile(
    { optimizer: "sgd", loss: "meanAbsoluteError" },
    (metrics = ["accuracy"])
);
    
// Evaluate the model
const result = model.evaluate(
    tf.ones([8, 10]), tf.ones([8, 1]), {
    batchSize: 4,
});
   
// Print the resulting tensor
result.print();

Output:

Tensor
    1.608272910118103

Example 2:

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define the model
const model = tf.sequential({
    layers: [tf.layers.dense({ 
        units: 1, inputShape: [10] 
    })],
});
    
// Add dropout to model
model.add(tf.layers.dropout({ rate: 0.5 }));
  
// Compile the model
model.compile({ optimizer: "adam"
    loss: "meanSquaredError" });
    
// Evaluate the model
const result = model.evaluate(
    tf.ones([8, 10]), tf.ones([8, 1]), {
    batchSize: 2,
});
    
// Print the result
result.print();

Output:

Tensor
    0.9941154718399048

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :