Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

layers.Rescaling

A Keras preprocessing layer that converts every input value with input * scale + offset. It is commonly used to normalize image pixels, such as mapping [0, 255] to [0, 1] or [-1, 1].

How it works

Rescaling transforms every input value using the affine rule output = input × scale + offset. It accepts integer or floating-point inputs, normally produces floating-point outputs, and performs the same transformation while training and inferring.

Syntax

tf.keras.layers.Rescaling(scale, offset=0.0, **kwargs)

Parameters

NameTypeDescription
scaleFloatFloating-point multiplier applied to each input value.
offsetFloatFloating-point value added after scaling; defaults to 0.0.
**kwargsKeyword argumentsAdditional base-layer keyword arguments, including options such as name and dtype.

Returns

A tensor containing the rescaled values; outputs are floating point by default.

Key points

  • Use this Keras preprocessing layer when model inputs need deterministic range conversion.
  • The operation is elementwise: each value is multiplied by scale, then shifted by offset.
  • For standard 8-bit image normalization, set scale=1./255.
  • For symmetric image scaling from 0–255, use scale=1./127.5 and offset=-1.
  • The layer has no training-only mode; its transformation remains active in both execution phases.
  • Dataset preprocessing can call the layer directly, including when constructing a mapped input pipeline.

Common mistakes

  • Do not expect this layer to behave differently between training and inference; it always applies its configured transformation.
  • If downstream code requires a particular dtype, configure the layer's dtype explicitly or account for its default floating-point output.
  • The layer can be placed in a tf.data pipeline, so dataset mapping is a supported deployment of the preprocessing step.

Example: Scale image pixels to [0, 1]

import tensorflow as tf

layer = tf.keras.layers.Rescaling(1.0 / 255.0)
images = tf.constant([[[[0, 128, 255]]]], dtype=tf.uint8)
scaled = layer(images)
print(scaled.numpy())  # [[[ [0.0, 0.5019608, 1.0] ]]]

This runnable example converts integer pixel values from the 0–255 range to floating-point values in the 0–1 range.

When to use it

Normalize pixels to [0, 1]

Image models receive integer RGB pixels in the [0, 255] range.

Convert pixels to [-1, 1]

A model expects inputs centered in a symmetric range.

Embed deterministic scaling in a model or tf.data pipeline

Preprocessing should be part of the input pipeline rather than separate application code.

Interview questions

QWhat computation does tf.keras.layers.Rescaling perform?

Rescaling is a preprocessing layer that applies an affine transformation independently to each input value: multiply by scale, then add offset. For input x, the result is x * scale + offset.

Follow-up: How would you map [0, 255] to [-1, 1]? Use scale=1./127.5 and offset=-1, which gives 0 → -1 and 255 → 1.

QHow do you configure common image-value range conversions?

Use scale=1./255 when converting image values from [0, 255] to [0, 1]. To map the same source range to [-1, 1], use scale=1./127.5 with offset=-1.

Follow-up: Why use a layer rather than manual preprocessing? Embedding it in the model can simplify deployment, while applying it through dataset mapping is also supported.

QWhat are the constructor inputs and dtype behaviors?

The constructor accepts scale, an offset multiplier, offset, an additive shift defaulting to 0.0, and standard base-layer keyword arguments such as name and dtype. Inputs may be integer or floating point, and outputs are floating point by default.

Follow-up: What operational behavior should be tested? Verify the output dtype and confirm that the same transformation is applied during both training and inference.

  • tf.keras.layers.Layer
  • tf.keras.layers.Normalization
  • tf.data.Dataset.map

Source: tf.keras.layers.Rescaling | TensorFlow v2.16.1. Checked 2026-10-15.