Monday, September 23, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.regularizers.l2() Function

Tensorflow.js tf.regularizers.l2() Function

The Regularisers in Tensorflow.js are attached with various components of models which work with the score function to help drive trainable values, large values. The method tf.regularizers.l2 () is inherited from regularizers class. The tf.regularizers.l2() methods apply l2 regularization in penalty case of model training. This method adds a term to the loss to perform penalty for large weights.It adds Loss+=sum(l2 * x^2) loss. So in this article, we are going to see how tf.regularizers.l2() function works.

Syntax:

tf.regularizers.l2 (args);

Parameters:

  • l2: The number represents the regularization rate by default it is 0.01.

Return: Regularizer

 

Example 1: In this example, we are going to see the standalone use of l2  Regularizer applied to the kernel weights matrix.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Add layer to it
model.add(tf.layers.dense({
    units: 32, batchInputShape:[null,50],
    kernelRegularizer:tf.regularizers.l2()
}));
  
// Model summary
model.summary();


Output:

Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense1 (Dense)         [null,32]                 1632      
=================================================================
Total params: 1632
Trainable params: 1632
Non-trainable params: 0

Example 2: In this example, we are going to see the standalone use of l2  Regularizer applied to the bias vector.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Add layer to it
model.add(tf.layers.dense({
    units: 32, batchInputShape:[null,50],
    biasRegularizer:tf.regularizers.l2()
}));
  
// Model summary
model.summary();


Output:

Layer (type)                 Output shape              Param #    
=================================================================
dense_Dense2 (Dense)         [null,32]                 1632      
=================================================================
Total params: 1632
Trainable params: 1632
Non-trainable params: 0

References:https://js.tensorflow.org/api/latest/#regularizers.l2

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