Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.regularizers.l1() Function

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)).

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

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