Skip to content
fn

local_response_norm

→Tensor
local_response_norm(input: Tensor, size: int, alpha: float = 0.0001, beta: float = 0.75, k: float = 1.0)

Local response normalization (Krizhevsky, Sutskever & Hinton, 2012).

Implements lateral inhibition: each activation is divided by a function of the squared activations of its channel neighbours. Introduced with AlexNet and largely superseded by batch / layer normalisation, but still used in some classic architectures.

Parameters

inputTensor
Input of shape (N, C, *spatial) (any number of spatial dims). Inputs with fewer than 2 dimensions are returned unchanged.
sizeint
Number of neighbouring channels (the size of the local window) used in the normalisation.
alphafloat= 0.0001
Multiplicative scaling factor on the squared-activation sum. Default 1e-4.
betafloat= 0.75
Exponent applied to the normalising factor. Default 0.75.
kfloat= 1.0
Additive constant guarding the division. Default 1.0.

Returns

Tensor

Same shape as input.

Notes

The scale is the mean of the squared activations over a window of n=sizen = \text{size} channels, so α\alpha is divided by nn:

yi,c,…=xi,c,…(k+αn∑c′=c−⌊n/2⌋c+⌊(n−1)/2⌋xi,c′,…2)βy_{i,c,\ldots} = \frac{x_{i,c,\ldots}}{\left(k + \frac{\alpha}{n} \sum_{c'=c-\lfloor n/2 \rfloor}^{c+\lfloor (n-1)/2 \rfloor} x_{i,c',\ldots}^2 \right)^\beta}

Channels outside [0, C) count as zero and stay in the denominator nn. The window reaches size // 2 channels below cc and (size - 1) // 2 above it, so for an even size it leans towards the lower channels. This is the reference framework's definition (an average pool of x2x^2 along the channel axis); the AlexNet paper writes the same rule with the 1/n1/n folded into α\alpha. LRN predates BatchNorm and provides only lateral inhibition (no learnt affine), which is why modern architectures usually do not include it.

Examples

>>> import lucid
>>> from lucid.nn.functional import local_response_norm
>>> x = lucid.randn(1, 96, 27, 27)   # AlexNet conv1 output shape
>>> y = local_response_norm(x, size=5, alpha=1e-4, beta=0.75, k=2.0)
>>> y.shape
(1, 96, 27, 27)

Used by 2