Implementation:Tensorflow Tfjs Constraints
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API, Constraints |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This module implements weight constraint classes for TensorFlow.js Layers, ported from Keras constraints.py. Constraints are applied to weight tensors during training to enforce properties such as maximum norm, unit norm, non-negativity, or bounded norms. The module provides the abstract Constraint base class and four concrete implementations: MaxNorm, UnitNorm, NonNeg, and MinMaxNorm. It also includes serialization/deserialization utilities and a registry for resolving constraint identifiers.
Code Reference
Source Location
tfjs-layers/src/constraints.ts (GitHub)
Key Imports
import * as tfc from '@tensorflow/tfjs-core';
import {serialization, Tensor, tidy} from '@tensorflow/tfjs-core';
import {epsilon} from './backend/common';
import {deserializeKerasObject, serializeKerasObject} from './utils/generic_utils';
Classes
Constraint (Abstract Base)
export abstract class Constraint extends serialization.Serializable {
abstract apply(w: Tensor): Tensor;
getConfig(): serialization.ConfigDict;
}
MaxNorm
Constrains weights so that the L2 norm of each weight vector does not exceed maxValue along the specified axis.
export class MaxNorm extends Constraint {
static readonly className = 'MaxNorm';
constructor(args: MaxNormArgs); // maxValue?: number (default 2), axis?: number (default 0)
apply(w: Tensor): Tensor;
}
UnitNorm
Constrains weights to have unit L2 norm along the specified axis.
export class UnitNorm extends Constraint {
static readonly className = 'UnitNorm';
constructor(args: UnitNormArgs); // axis?: number (default 0)
apply(w: Tensor): Tensor;
}
NonNeg
Constrains weights to be non-negative by applying ReLU.
export class NonNeg extends Constraint {
static readonly className = 'NonNeg';
apply(w: Tensor): Tensor; // returns tfc.relu(w)
}
MinMaxNorm
Constrains weights so that the L2 norm falls between minValue and maxValue, with a configurable enforcement rate.
export class MinMaxNorm extends Constraint {
static readonly className = 'MinMaxNorm';
constructor(args: MinMaxNormArgs);
// minValue?: number (default 0), maxValue?: number (default 1),
// rate?: number (default 1), axis?: number (default 0)
apply(w: Tensor): Tensor;
}
Utility Functions
export function serializeConstraint(constraint: Constraint): serialization.ConfigDictValue;
export function deserializeConstraint(config: serialization.ConfigDict,
customObjects?: serialization.ConfigDict): Constraint;
export function getConstraint(identifier: ConstraintIdentifier |
serialization.ConfigDict | Constraint): Constraint;
The getConstraint function resolves string identifiers ('maxNorm', 'minMaxNorm', 'nonNeg', 'unitNorm'), config dicts, or existing Constraint instances.
I/O Contract
| Method | Input | Output |
|---|---|---|
Constraint.apply |
Weight Tensor |
Constrained weight Tensor (same shape)
|
getConstraint |
String identifier, config dict, or Constraint |
Constraint instance
|
Usage Example
import * as tf from '@tensorflow/tfjs';
const model = tf.sequential();
model.add(tf.layers.dense({
units: 64,
kernelConstraint: tf.constraints.maxNorm({maxValue: 2, axis: 0}),
inputShape: [128]
}));
Related Pages
- Tensorflow_Tfjs_Exports_Constraints - Public API factory functions for constraints
- Tensorflow_Tfjs_Regularizers - Weight regularizers (complementary to constraints)
- Tensorflow_Tfjs_Exports_Initializers - Weight initializer factory functions