Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs Regularizers

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, Layers_API, Regularization
Last Updated 2026-02-10 06:00 GMT

Overview

This module implements weight regularizer classes for TensorFlow.js Layers, ported from Keras regularizers.py. Regularizers add penalty terms to the loss function based on weight values, discouraging large weights to reduce overfitting. The module provides the abstract Regularizer base class, the L1L2 concrete class supporting combined L1 and L2 penalties, convenience functions l1() and l2(), and serialization/deserialization utilities.

Code Reference

Source Location

tfjs-layers/src/regularizers.ts (GitHub)

Key Imports

import * as tfc from '@tensorflow/tfjs-core';
import {abs, add, Scalar, serialization, sum, Tensor, tidy, zeros} from '@tensorflow/tfjs-core';
import * as K from './backend/tfjs_backend';
import {deserializeKerasObject, serializeKerasObject} from './utils/generic_utils';

Classes

Regularizer (Abstract Base)

export abstract class Regularizer extends serialization.Serializable {
  abstract apply(x: Tensor): Scalar;
}

L1L2

Combined L1 and L2 regularizer. The penalty is: l1 * sum(abs(x)) + l2 * sum(x^2).

export class L1L2 extends Regularizer {
  static className = 'L1L2';
  constructor(args?: L1L2Args);  // l1?: number (default 0.01), l2?: number (default 0.01)
  apply(x: Tensor): Scalar;
  getConfig(): serialization.ConfigDict;
  static override fromConfig(cls, config): T;
}

Interfaces

export interface L1L2Args { l1?: number; l2?: number; }
export interface L1Args  { l1: number; }
export interface L2Args  { l2: number; }

Convenience Functions

export function l1(args?: L1Args): L1L2   // creates L1L2 with l2=0
export function l2(args: L2Args): L1L2    // creates L1L2 with l1=0

Utility Functions

export function serializeRegularizer(constraint: Regularizer): serialization.ConfigDictValue;
export function deserializeRegularizer(config: serialization.ConfigDict,
    customObjects?: serialization.ConfigDict): Regularizer;
export function getRegularizer(identifier: RegularizerIdentifier |
    serialization.ConfigDict | Regularizer): Regularizer;

The getRegularizer function resolves string identifiers (e.g., 'l1l2'), config dicts, or existing Regularizer instances.

I/O Contract

Method Input Output
Regularizer.apply Weight Tensor Scalar penalty value
getRegularizer String, config dict, or Regularizer Regularizer instance

Usage Example

import * as tf from '@tensorflow/tfjs';

const model = tf.sequential();
model.add(tf.layers.dense({
  units: 64,
  kernelRegularizer: tf.regularizers.l2({l2: 0.001}),
  inputShape: [128]
}));

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment