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.l1() Function

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

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.l1() function is used for L1 regularization. Moreover, it appends a name to the loss in order to rebuke enormous weights: loss += sum(l1 * abs(x)).

Syntax:

tf.regularizers.l1(config?)

Parameters:

  • config: It is an object which is optional. And under it comes l1.
  • l1: It is the stated L1 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 l1 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.l1() method
model.add(tf.layers.dense({
    units: 37, batchInputShape:[null, 40],
    kernelRegularizer:tf.regularizers.l1()
}));
  
// Calling summary() method and 
// Printing output
model.summary();


Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense52 (Dense)        [null,37]                 1517      
=================================================================
Total params: 1517
Trainable params: 1517
Non-trainable params: 0
_________________________________________________________________

Example 2: In this example, we are going to see the standalone use of l1 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.l1() method
model.add(tf.layers.dense({
    units: 2, batchInputShape:[null, 13],
    biasRegularizer:tf.regularizers.l1()
}));
  
// Calling summary() method and 
// Printing output
model.summary();


Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense54 (Dense)        [null,2]                  28        
=================================================================
Total params: 28
Trainable params: 28
Non-trainable params: 0
_________________________________________________________________

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


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