Implementation:Tensorflow Tfjs Rescaling Layer
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API, Preprocessing |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The Rescaling layer is an image preprocessing layer that applies a linear transformation to pixel values: output = input * scale + offset. It is commonly used to normalize pixel values from [0, 255] to [0, 1] or [-1, 1] as the first step in an image processing pipeline. The layer automatically casts inputs to float32 if needed.
Code Reference
Source Location
tfjs-layers/src/layers/preprocessing/image_preprocessing.ts (GitHub)
Key Imports
import {LayerArgs, Layer} from '../../engine/topology';
import {serialization, Tensor, mul, add, tidy} from '@tensorflow/tfjs-core';
import {getExactlyOneTensor} from '../../utils/types_utils';
import * as K from '../../backend/tfjs_backend';
Layer Class
export class Rescaling extends Layer {
static className = 'Rescaling';
constructor(args: RescalingArgs);
override getConfig(): serialization.ConfigDict;
override call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor[] | Tensor;
}
RescalingArgs
export interface RescalingArgs extends LayerArgs {
scale: number; // multiplicative factor
offset?: number; // additive offset (default: 0)
}
I/O Contract
| Method | Input | Output |
|---|---|---|
call |
Tensor of any shape (cast to float32) | input * scale + offset (same shape, float32)
|
Usage Example
import * as tf from '@tensorflow/tfjs';
// Normalize pixel values from [0, 255] to [0, 1]
const model = tf.sequential();
model.add(tf.layers.rescaling({scale: 1.0 / 255, inputShape: [224, 224, 3]}));
// Normalize to [-1, 1]
const model2 = tf.sequential();
model2.add(tf.layers.rescaling({
scale: 1.0 / 127.5,
offset: -1,
inputShape: [224, 224, 3]
}));
Related Pages
- Tensorflow_Tfjs_Resizing_Layer - Image resizing preprocessing layer
- Tensorflow_Tfjs_CenterCrop_Layer - Center crop preprocessing layer
- Tensorflow_Tfjs_CategoryEncoding_Layer - Categorical feature preprocessing
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment