LocalResponseNorm
ModuleLocalResponseNorm(size: int, alpha: float = 0.0001, beta: float = 0.75, k: float = 1.0)Local Response Normalization (LRN) across adjacent channels.
Normalises each activation by a sum of squared activations in a
local neighbourhood of size channels centred on the same
spatial position:
where is the total number of channels, and the window is clipped at the channel boundaries.
LRN was introduced in the AlexNet paper as a biologically-inspired lateral inhibition mechanism: strongly-activated neurons suppress their neighbors, encouraging competition across feature detectors at the same spatial location. It has largely been superseded by Batch Normalization in modern architectures, but remains available for reproducing historical results.
Parameters
sizeintalphafloat= 0.00011e-4.betafloat= 0.750.75.kfloat= 1.01.0.Notes
- Input: where denotes zero or more spatial dimensions. The channel axis must be axis 1.
- Output: same shape as the input.
- The implementation uses a sliding-window sum over the squared
input, applied via
unfold_dimon the padded channel axis. This avoids an explicit Python loop over channels and is fully differentiable through the Lucid engine. - Inputs with fewer than 2 dimensions are returned unchanged.
- The original AlexNet used
size=5,alpha=1e-4,beta=0.75,k=2.0— adjustingkis important to control how strongly unsuppressed channels are penalized.
Examples
Reproduce the AlexNet LRN configuration:
>>> import lucid
>>> import lucid.nn as nn
>>> lrn = nn.LocalResponseNorm(size=5, alpha=1e-4, beta=0.75, k=2.0)
>>> x = lucid.randn(2, 96, 27, 27) # e.g. after first conv in AlexNet
>>> out = lrn(x)
>>> out.shape
(2, 96, 27, 27)
Minimal example with a 1-D channel tensor:
>>> lrn_small = nn.LocalResponseNorm(size=3)
>>> x2 = lucid.randn(4, 32, 64) # (N, C, L)
>>> out2 = lrn_small(x2)
>>> out2.shape
(4, 32, 64)Methods (3)
__init__
→None__init__(size: int, alpha: float = 0.0001, beta: float = 0.75, k: float = 1.0)Initialise the LocalResponseNorm module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Apply normalisation to the input tensor.
Parameters
inputTensorReturns
TensorNormalised tensor of the same shape as input.
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.