-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add lrn layer #9157
Add lrn layer #9157
Changes from 6 commits
bc787af
1d36397
3bf1707
bb9c711
bb66828
308b3c2
0caf2f2
246367d
4316a67
ce8e013
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None): | ||
""" | ||
**Local Response Normalization Operator** | ||
|
||
Refer to `ImageNet Classification with Deep Convolutional Neural Networks | ||
<https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_ | ||
|
||
The formula is as follows: | ||
|
||
.. math:: | ||
|
||
Output(i, x, y) = Input(i, x, y) / \left( | ||
k + \alpha \sum\limits^{\min(C, c + n/2)}_{j = \max(0, c - n/2)} | ||
(Input(j, x, y))^2 \right)^{\beta} | ||
|
||
In the above equation: | ||
|
||
* :math:`n`: The number of channels to sum over. | ||
* :math:`k`: The offset (usually positive to avoid dividing by 0). | ||
* :math:`alpha`: The scaling parameter. | ||
* :math:`beta`: The exponent. | ||
|
||
Args: | ||
input (Variable): The input tensor of this layer. The dimension of input tensor must be 4 and it's order should be 'NCHW'. | ||
n (int, default 5): The number of channels to sum over. | ||
k (float, default 1.0): An offset (usually positive to avoid dividing by 0). | ||
alpha (float, default 1e-4): The scaling parameter. | ||
beta (float, default 0.75): The exponent. | ||
name (str, default None): A name for this operation. | ||
|
||
Raises: | ||
ValueError: If rank of the input tensor is not 4. | ||
|
||
Returns: | ||
A tensor variable storing the transformation result. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
data = fluid.layers.data(name="data", shape=[3, 112, 112], dtype="float32") | ||
lrn = fluid.layers.lrn(input=data) | ||
""" | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ | |
'smooth_l1', | ||
'one_hot', | ||
'autoincreased_step_counter', | ||
'lrn', | ||
] | ||
|
||
|
||
|
@@ -3292,3 +3293,72 @@ def autoincreased_step_counter(counter_name=None, begin=1, step=1): | |
counter.stop_gradient = True | ||
|
||
return counter | ||
|
||
|
||
def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None): | ||
""" | ||
**Local Response Normalization Operator** | ||
|
||
Refer to `ImageNet Classification with Deep Convolutional Neural Networks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
<https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_ | ||
|
||
The formula is as follows: | ||
|
||
.. math:: | ||
|
||
Output(i, x, y) = Input(i, x, y) / \left( | ||
k + \alpha \sum\limits^{\min(C, c + n/2)}_{j = \max(0, c - n/2)} | ||
(Input(j, x, y))^2 \right)^{\beta} | ||
|
||
In the above equation: | ||
|
||
* :math:`n`: The number of channels to sum over. | ||
* :math:`k`: The offset (usually positive to avoid dividing by 0). | ||
* :math:`alpha`: The scaling parameter. | ||
* :math:`beta`: The exponent parameter. | ||
|
||
Args: | ||
input (Variable): The input tensor of this layer, and the dimension of input tensor must be 4. | ||
n (int, default 5): The number of channels to sum over. | ||
k (float, default 1.0): An offset (usually positive to avoid dividing by 0). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid being divided by 0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
alpha (float, default 1e-4): The scaling parameter. | ||
beta (float, default 0.75): The exponent. | ||
name (str, default None): A name for this operation. | ||
|
||
Raises: | ||
ValueError: If rank of the input tensor is not 4. | ||
|
||
Returns: | ||
A tensor variable storing the transformation result. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
data = fluid.layers.data(name="data", shape=[3, 112, 112], dtype="float32") | ||
lrn = fluid.layers.lrn(input=data) | ||
""" | ||
helper = LayerHelper('lrn', **locals()) | ||
dtype = helper.input_dtype() | ||
input_shape = input.shape | ||
dims = len(input_shape) | ||
|
||
if dims != 4: | ||
raise ValueError( | ||
"dims of input must be 4(not %d), and it's order must be NCHW" % | ||
(dims)) | ||
|
||
mid_out = helper.create_tmp_variable(dtype=dtype, stop_gradient=True) | ||
lrn_out = helper.create_tmp_variable(dtype) | ||
helper.append_op( | ||
type="lrn", | ||
inputs={"X": input}, | ||
outputs={ | ||
"Out": lrn_out, | ||
"MidOut": mid_out, | ||
}, | ||
attrs={"n": n, | ||
"k": k, | ||
"alpha": alpha, | ||
"beta": beta}) | ||
|
||
return lrn_out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this file.