Tensorflow.js tf.regularizers.l1() Function
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)).
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!
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 libraryconst tf = require("@tensorflow/tfjs"); // Define sequential modelconst model = tf.sequential(); // Adding layer to it and calling // regularizers.l1() methodmodel.add(tf.layers.dense({ units: 37, batchInputShape:[null, 40], kernelRegularizer:tf.regularizers.l1()})); // Calling summary() method and // Printing outputmodel.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 libraryconst tf = require("@tensorflow/tfjs"); // Define sequential modelconst model = tf.sequential(); // Adding layer to it and calling // regularizers.l1() methodmodel.add(tf.layers.dense({ units: 2, batchInputShape:[null, 13], biasRegularizer:tf.regularizers.l1()})); // Calling summary() method and // Printing outputmodel.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

