MCPcopy Create free account
hub / github.com/apache/tvm / lrn_python

Function lrn_python

python/tvm/topi/testing/lrn_python.py:26–75  ·  view source on GitHub ↗

Local response normalization operator in NCHW layout. Parameters ---------- a_np : numpy.ndarray 4-D with shape [batch, in_channel, in_height, in_width] size : int normalization window size axis : int input data layout channel axis bias : float

(a_np, size, axis, bias, alpha, beta)

Source from the content-addressed store, hash-verified

24
25
26def lrn_python(a_np, size, axis, bias, alpha, beta):
27 """Local response normalization operator in NCHW layout.
28
29 Parameters
30 ----------
31 a_np : numpy.ndarray
32 4-D with shape [batch, in_channel, in_height, in_width]
33
34 size : int
35 normalization window size
36
37 axis : int
38 input data layout channel axis
39
40 bias : float
41 offset to avoid dividing by 0. constant value
42
43 alpha : float
44 constant value
45
46 beta : float
47 exponent constant value
48
49 Returns
50 -------
51 lrn_out : np.ndarray
52 4-D with shape [batch, out_channel, out_height, out_width]
53 """
54 radius = size // 2
55 sqr_sum = np.zeros(shape=a_np.shape).astype(a_np.dtype)
56 for i, j, k, l in product(*[range(_axis) for _axis in a_np.shape]):
57 axis_size = a_np.shape[axis]
58 if axis == 1:
59 # NCHW layout
60 sum_start = j - radius if j - radius >= 0 else 0
61 sum_end = j + radius + 1 if j + radius + 1 < axis_size else axis_size
62 sqr_sum[i, j, k, l] = sum(
63 a_np[i, sum_start:sum_end, k, l] * a_np[i, sum_start:sum_end, k, l]
64 )
65 elif axis == 3:
66 # NHWC layout
67 sum_start = l - radius if l - radius >= 0 else 0
68 sum_end = l + radius + 1 if l + radius + 1 < axis_size else axis_size
69 sqr_sum[i, j, k, l] = sum(
70 a_np[i, j, k, sum_start:sum_end] * a_np[i, j, k, sum_start:sum_end]
71 )
72
73 sqr_sum_up = np.power((bias + (alpha * sqr_sum / size)), beta)
74 lrn_out = np.divide(a_np, sqr_sum_up)
75 return lrn_out

Callers

nothing calls this directly

Calls 5

powerMethod · 0.80
sumFunction · 0.50
astypeMethod · 0.45
zerosMethod · 0.45
divideMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…