Problems
Loading...
Step 4 of 6
Local Response Normalization
Medium

Implement AlexNet local response normalization across neighboring channels:

yb,h,w,c=xb,h,w,c(k+αn∑j=max⁡(0,c−r)min⁡(C−1,c+r)xb,h,w,j2)β,r=⌊n2⌋.y_{b,h,w,c}=\frac{x_{b,h,w,c}}{\left(k+\frac{\alpha}{n}\sum_{j=\max(0,c-r)}^{\min(C-1,c+r)}x_{b,h,w,j}^{2}\right)^{\beta}},\qquad r=\left\lfloor\frac{n}{2}\right\rfloor.

Here, B,H,W,CB,H,W,C are the batch, height, width, and channel dimensions. The positive odd integer n gives the full channel-window width away from boundaries. Return a float64 NumPy array with the same shape as x.

Loading visualization...

Examples

Input: x = [[[[1,2,3,0.5,-1]]]], k = 2, n = 5, alpha = 0.0001, beta = 0.75

Output: [[[[0.594541,1.18908,1.783607,0.29727,-0.594558]]]]

Explanation: Each channel uses the squared values inside its clipped five-channel neighborhood.

Input: x = [[[[1,-2,3]]]], k = 1, n = 1, alpha = 0.1, beta = 0.5

Output: [[[[0.953463,-1.690309,2.176429]]]]

Input: x = [[[[1,2,3]],[[3,2,1]]]], k = 2, n = 3, alpha = 0.01, beta = 0.75

Output: [[[[0.590914,1.168812,1.755363]],[[1.755363,1.168812,0.590914]]]]

Hint 1

Loop over the channel index and clip the local slice with max and min.

Hint 2

Sum x[..., start:stop] ** 2 along the last axis.

Requirements

  • Normalize each value using squared activations from its local channel window.
  • Clip the channel window at the first and last channels.
  • Preserve the input shape and return float64 values.

Constraints

  • x has shape (B,H,W,C)(B,H,W,C) and contains finite values.
  • n is a positive odd integer.
  • k>0k>0, α≥0\alpha\ge0, and β>0\beta>0.