1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
r"""
Apply local response normalization to the input tensor.
Args:
kernel_size: the size of the kernel to apply LRN on.
k: hyperparameter k. The default vaule is 2.0.
alpha: hyperparameter alpha. The default value is 1e-4.
beta: hyperparameter beta. The default value is 0.75.
Example:
.. testcode::
from megengine import tensor
import megengine.module as M
import numpy as np
inp = tensor(np.arange(25, dtype=np.float32).reshape(1,1,5,5))
GT = np.array([[[[ 0., 0.999925, 1.9994003, 2.9979765, 3.9952066],
[ 4.9906454, 5.983851, 6.974385, 7.961814, 8.945709 ],
[ 9.925651, 10.90122, 11.872011, 12.837625, 13.7976675],
[14.751757, 15.699524, 16.640602, 17.574642, 18.501305 ],
[19.420258, 20.331186, 21.233786, 22.127764, 23.012836 ]]]])
op = M.LocalResponseNorm(kernel_size=3, k=1.0, alpha=1e-4, beta=0.75)
out = op(inp)
np.testing.assert_allclose(GT, out.numpy(), rtol=1e-6, atol=1e-6)
print('pass')
Outputs:
.. testoutput::
pass
"""
=
=
=
=
return