Tensorflow.js tf.dropout() 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.dropout() function is used to compute the dropout. You can read more about dropout from https://www.w3wiki.net/dropout-in-neural-networks/.

Syntax:

tf.dropout (x, rate, noiseShape?, seed?)

Parameters:

  • x:  a tensor with floating input values.
  • rate (number): the probability that each element of x is discarded. Takes in values in the range 0 to 1.
  • noiseShape (number[]): array representing the shape for randomly generated keep/drop flags. It is optional to prove this parameter. Type: int32.
  • seed (number or string): it is used to create random seeds. It is optional to provide this parameter.

Return Value: It returns tf.Tensor[].

Example 1:

Javascript




const tf = require("@tensorflow/tfjs")
 
// creating a tensor
const x = tf.tensor1d([1, 2, 2, 1]);
const rate = 0.6;
 
// calculating dropout
const output = tf.dropout(x, rate);
output.print();


Output:

Tensor
   [0, 5, 5, 0]

Example 2:

In this example we’ll pass the noiseShape as [4, 1] to create a new dimension size.

Javascript




const tf = require("@tensorflow/tfjs")
 
// creating a tensor
const x = tf.tensor1d([1, 2, 3, 1]);
const rate = 0.6;
 
// calculating dropout
const output = tf.dropout(x, rate, [4,1]);
output.print();


Output:

Tensor
  [[0  , 0, 0  , 0  ],
   [0  , 0, 0  , 0  ],
   [2.5, 5, 7.5, 2.5],
   [2.5, 5, 7.5, 2.5]]

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