Skip to content
Related Articles

Related Articles

Tensorflow.js tf.regularizers.l1l2() Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 27 Jul, 2021

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .regularizers.l1l2() function is used for L1 and L2 regularization. Moreover, it appends a name to the loss in order to rebuke enormous weights: loss += sum(l1 * abs(x)) + sum(l2 * x^2).

Syntax:

tf.regularizers.l1l2(config?)

Parameters:

  • config: It is an object which is optional. And under it comes l1 and l2.
  • l1: It is the stated L1 regularization rate, whose by default value is 0.01. It is of type number.
  • l2: It is the stated L2 regularization rate, whose by default value is 0.01. It is of type number.

Return Value: It returns Regularizer.

Example 1: In this example, we are going to see the standalone use of l1l2 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();
  
// Adding layer to it and calling 
// regularizers.l1l2() method
model.add(tf.layers.dense({
    units: 11, batchInputShape:[3, 4],
    kernelRegularizer:tf.regularizers.l1l2()
}));
  
// Calling summary() method and 
// Printing output
model.summary();

Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense57 (Dense)        [3,11]                    55        
=================================================================
Total params: 55
Trainable params: 55
Non-trainable params: 0
_________________________________________________________________

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

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
  
// Define sequential model
const model = tf.sequential();
  
// Adding layer to it and calling 
// regularizers.l1l2() method
model.add(tf.layers.dense({
    units: 12, inputShape:[null, 8, 2],
    biasRegularizer:tf.regularizers.l1l2()
}));
  
// Calling summary() method and 
// Printing output
model.summary();

Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense69 (Dense)        [null,null,8,12]          36        
=================================================================
Total params: 36
Trainable params: 36
Non-trainable params: 0
_________________________________________________________________

Reference: https://js.tensorflow.org/api/latest/#regularizers.l1l2


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!