Implementation:Tensorflow Tfjs Layer Variables
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The variables.ts module defines LayerVariable, the mutable tensor wrapper used for layer weights in TensorFlow.js. Unlike immutable tf.Tensor objects, a LayerVariable has a mutable value that can be read and updated during training. It supports dtype, shape, naming (with unique suffix generation), trainability control, and optional constraints applied after weight updates.
The module also provides factory functions for creating variables initialized with zeros, ones, random values, identity matrices, and truncated normals. Batch operations (batchGetValue, batchSetValue) allow efficient bulk reads and updates. The gradients() function computes gradients with respect to LayerVariable instances.
Code Reference
Source Location
tfjs-layers/src/variables.ts (View on GitHub)
Key Signatures
export class LayerVariable {
readonly dtype: DataType;
readonly shape: Shape;
readonly id: number;
readonly name: string;
readonly originalName: string;
readonly constraint: Constraint;
constructor(val: Tensor, dtype?: DataType, name?: string,
trainable?: boolean, constraint?: Constraint);
read(): Tensor;
write(newVal: Tensor): LayerVariable;
dispose(): void;
get trainable(): boolean;
set trainable(trainable: boolean);
}
// Factory functions
export function variable(x: Tensor, dtype?: DataType, name?: string,
constraint?: Constraint): LayerVariable;
export function zerosVariable(shape: Shape, dtype?: DataType, name?: string): LayerVariable;
export function onesVariable(shape: Shape, dtype?: DataType, name?: string): LayerVariable;
export function zerosLike(x: Tensor, dtype?: DataType, name?: string): LayerVariable;
export function onesLike(x: Tensor, dtype?: DataType, name?: string): LayerVariable;
export function eyeVariable(size: number, dtype?: DataType, name?: string): LayerVariable;
export function randomUniformVariable(shape: Shape, minval: number, maxval: number,
dtype?: DataType, seed?: number, name?: string): LayerVariable;
export function truncatedNormalVariable(shape: Shape, mean?: number, stddev?: number,
dtype?: DataType, seed?: number, name?: string): LayerVariable;
export function randomNormalVariable(shape: Shape, mean?: number, stddev?: number,
dtype?: DataType, seed?: number, name?: string): LayerVariable;
// Update functions
export function update(x: LayerVariable, xNew: Tensor): LayerVariable;
export function updateAdd(x: LayerVariable, increment: Tensor): LayerVariable;
export function updateSub(x: LayerVariable, decrement: Tensor): LayerVariable;
// Batch operations
export function batchGetValue(xs: LayerVariable[]): Tensor[];
export function batchSetValue(variablesAndValues: Array<[LayerVariable, Tensor]>): void;
// Gradients
export function gradients(lossFn: () => tfc.Scalar, variables: LayerVariable[]): Tensor[];
Import
import { LayerVariable, variable, batchGetValue, batchSetValue } from './variables';
I/O Contract
LayerVariable Constructor
- Input: An initial
Tensorvalue, optional dtype (default'float32'), optional name (default'Variable'), trainable flag (defaulttrue), and optionalConstraint. - Output: A
LayerVariableinstance with a unique scoped name and an underlyingtfc.Variable. - Constraints: The constraint is applied automatically after each
write()call.
read() / write()
- read(): Returns the current
tf.Tensorvalue. Throws if the variable is disposed. - write(newVal): Updates the value in-place via
tfc.Variable.assign(). The new value must match the existing shape. Applies the constraint if one is set. Returns the variable for chaining.
Batch Operations
- batchGetValue: Takes an array of
LayerVariables, returns their values asTensor[]. - batchSetValue: Takes an array of
[LayerVariable, Tensor]pairs and writes each value.
gradients()
- Input: A loss function (returning a scalar) and an array of
LayerVariables. - Output: An array of gradient
Tensors, one per variable.
Usage Examples
import { LayerVariable, variable, zerosVariable, batchGetValue } from './variables';
import * as tf from '@tensorflow/tfjs-core';
// Create a trainable variable
const weights = variable(tf.randomNormal([3, 4]), 'float32', 'dense/kernel');
console.log(weights.name); // 'dense/kernel' or 'dense/kernel_1' if collision
console.log(weights.shape); // [3, 4]
// Read and update
const currentValue = weights.read();
weights.write(tf.ones([3, 4]));
// Create zero-initialized variable
const bias = zerosVariable([4], 'float32', 'dense/bias');
// Batch read
const values = batchGetValue([weights, bias]);
// Compute gradients
const grads = gradients(
() => tf.sum(tf.matMul(input, weights.read())),
[weights]
);
Related Pages
- Tensorflow_Tfjs_Backend_Operations - Backend tensor operations that manipulate the underlying tensor values
- Tensorflow_Tfjs_Graph_Executor - Executor that uses layer weights during inference
- Tensorflow_Tfjs_Recurrent_Layers - RNN layers that use
batchGetValueandbatchSetValuefor state management - Tensorflow_Tfjs_Normalization_Layers - Normalization layers that create
LayerVariables for gamma, beta, moving mean, and moving variance