Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Tensorflow.js tf.regularizers.l2() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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


My Personal Notes arrow_drop_up
Last Updated : 13 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials