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.
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
const tf = require( "@tensorflow/tfjs" );
const model = tf.sequential({
layers: [tf.layers.dense({
units: 1, inputShape: [10]
})],
});
model.add(tf.layers.dropout({ rate: 0.25 }));
model.compile(
{ optimizer: "sgd" , loss: "meanAbsoluteError" },
(metrics = [ "accuracy" ])
);
const result = model.evaluate(
tf.ones([8, 10]), tf.ones([8, 1]), {
batchSize: 4,
});
result.print();
|
Output:
Tensor
1.608272910118103
Example 2:
Javascript
const tf = require( "@tensorflow/tfjs" );
const model = tf.sequential({
layers: [tf.layers.dense({
units: 1, inputShape: [10]
})],
});
model.add(tf.layers.dropout({ rate: 0.5 }));
model.compile({ optimizer: "adam" ,
loss: "meanSquaredError" });
const result = model.evaluate(
tf.ones([8, 10]), tf.ones([8, 1]), {
batchSize: 2,
});
result.print();
|
Output:
Tensor
0.9941154718399048
Reference: https://js.tensorflow.org/api/latest/#layers.dropout
Share your thoughts in the comments
Please Login to comment...