Skip to content

compute.ndarray.normalize

Summary

compute.ndarray.normalize applies explicit scaling and centering or derives its parameters from min-max or z-score statistics. Choose the axes to normalize each sample or channel independently, or supply statistics from another array. The result and any computed statistics stay in the input's residency.

Syntax

normalize<T extends CPUndarray | GPUndarray>(a: T, options: NdarrayNormalizeOptions): T
const scaled = nd.normalize(a, { method: "minmax", axes: -1 });

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live real-valued input array.
options NdarrayNormalizeOptions Yes Select an explicit affine transform or one statistics method, plus optional output settings.

Returns

T - Packed array with the same shape and residency. The output dtype defaults to the input float type, or to f32 for integer input. Destroy an allocated result when finished.

Type Details

type NdarrayParameter = number | ArrayLike<number> | CPUndarray | GPUndarray;
type NdarrayNormalizeMethod = "minmax" | "zscore";
type NdarrayNormalizeOptions = NdarrayOperationOptions & { dtype?: DType } & (
    | { method?: undefined; scale?: NdarrayParameter; offset?: NdarrayParameter; mean?: undefined; std?: undefined; axis?: number }
    | { method?: undefined; mean: NdarrayParameter; std: NdarrayParameter; scale?: undefined; offset?: undefined; axis?: number }
    | ({
        scale?: undefined;
        offset?: undefined;
        mean?: undefined;
        std?: undefined;
        axes?: number | readonly number[];
        axis?: number;
        nan?: NdarrayNaNMode;
        epsilon?: number;
    } & (
        | { method: "minmax"; range?: readonly [number, number]; ddof?: undefined }
        | { method: "zscore"; ddof?: number; range?: undefined }
    ))
);

The explicit modes accept either scale and offset for x * scale + offset (defaults 1 and 0, supply at least one), or both mean and std for (x - mean) / std. A numeric parameter is a finite scalar or a list of one value or one per element of the specified axis. A numeric standard deviation must be nonzero. Ndarray parameters broadcast by shape and must share the input's residency and device. With ndarray parameters, use shape to express the broadcast and omit axis. A zero divisor in an ndarray supplied as std can produce non-finite values.

For computed statistics, axes names dimensions to reduce. Alternatively, axis names the single axis to keep. With neither, the calculation uses the whole array. nan defaults to "propagate". Use "omit" to ignore NaNs in the statistics while keeping their output positions NaN for floating outputs.

  • method: "minmax" maps the minimum and maximum of each slice to range, a finite, increasing pair defaulting to [0, 1].
  • method: "zscore" subtracts the mean and divides by the standard deviation. ddof is finite and non-negative and defaults to zero. A sample count at most ddof produces NaN statistics.
  • epsilon defaults to zero and is a finite non-negative threshold. A finite denominator with absolute value at most epsilon is replaced by one. Constant finite slices produce the lower range endpoint for min-max and zero for z-score when their sample count exceeds ddof.

GPU output options and encoder behavior follow compute.ndarray.contiguous. The optional dtype selects a real output type. Integer results saturate, round ties to even and map NaN to zero. The CPU computes in f64. The GPU computes in f32 and rejects f64 inputs, outputs and ndarray parameters.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2, 3], [1, 2, 3, 4, 4, 4]);
const b = nd.normalize(a, { method: "minmax", axes: 1 });
console.log(b.data()); // [0, 0.5, 1, 0, 0, 0]
b.destroy();
a.destroy();

See Also