Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.layers.spatialDropout1d() Function

Tensorflow.js tf.layers.spatialDropout1d() Function

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.layers.spatialDropout1d() function is used to apply the spatial 1D version of the dropout operation on data.

Syntax:

tf.layers.spatialDropout1d(args)

Input shape: Arbitrary. When utilizing this layer as the initial layer in a model, use the inputShape configuration.

Output shape: The output has the same shape as the input.

Parameters: It accepts the args object which can have the following properties:

  • rate (number): A float value between 0 and 1. The fraction of input units to drop.
  • seed (number): An integer that will be used as a random seed.
  • inputShape: If this property is set, it will be utilized to construct an input layer that will be inserted before this layer. 
  • batchInputShape: If this property is set, an input layer will be created and inserted before this layer. 
  • batchSize: If batchInputShape isn’t supplied and inputShape is, batchSize is utilized to build the batchInputShape.
  • dtype: It is the kind of data type for this layer. float32 is the default value. This parameter applies exclusively to input layers.
  • name: This is the layer’s name and is of string type.
  • trainable: If the weights of this layer may be changed by fit. True is the default value.
  • weights: The layer’s initial weight values.

Returns: It returns an object (SpatialDropout1D).

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const model = tf.sequential({
    layers: [tf.layers.dense({ 
        units: 1, inputShape: [10, 2] 
    })],
});
    
model.add(tf.layers.spatialDropout1d({ rate: 0.25 }));
      
model.compile(
    { optimizer: "sgd", loss: "meanAbsoluteError" },
);
      
const result = model.evaluate(
    tf.ones([8, 10, 2]), tf.ones([8, 10, 1]), {
    batchSize: 4,
});
     
result.print();


Output:

Tensor
   1.1040260791778564

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
    
const model = tf.sequential({
    layers: [tf.layers.dense({ 
        units: 1, inputShape: [5, 5] 
    })],
});
      
model.add(tf.layers.spatialDropout1d({ rate: 0.5, seed: 146 }));
    
model.compile({ optimizer: "adam"
    loss: "meanSquaredError" });
      
const result = model.evaluate(
    tf.ones([4, 5, 5]), tf.ones([4, 5, 1]), {
    batchSize: 2,
});
      
result.print();


Output:

Tensor
   7.158995151519775

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

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

Recent Comments