#189 · Deep Learning · Medium
⊣ Solve on deep-ml.comImplement Local Response Normalization (LRN) as described in the AlexNet paper. Given a 4D input tensor (batch, channels, height, width) and parameters n, alpha, beta, and k, normalize each value using the activities of its neighboring channels.
import numpy as np
def local_response_normalization(x: np.ndarray, n: int = 5,
alpha: float = 1e-4,
beta: float = 0.75,
k: float = 2.0) -> np.ndarray:
# x shape: (batch, channels, height, width)
batch, channels, height, width = x.shape
output = np.zeros_like(x)
half_n = n // 2
for b in range(batch):
for c in range(channels):
c_start = max(0, c - half_n)
c_end = min(channels, c + half_n + 1)
neighborhood = x[b, c_start:c_end, :, :]
sum_sq = np.sum(neighborhood ** 2, axis=0)
denom = (k + alpha * sum_sq) ** beta
output[b, c, :, :] = x[b, c, :, :] / denom
return outputn neighboring channels centered at c.sum_sq = sum(x[b, j, h, w]^2) for j in the neighborhood.output = x[b, c, h, w] / (k + alpha * sum_sq)^beta.