-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add lrn layer #9157
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
Merged
qingqing01
merged 10 commits into
PaddlePaddle:develop
from
dragonwarrior:add-lrn-layer
Mar 26, 2018
Merged
Add lrn layer #9157
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bc787af
add LRN layer for fluid
walloollaw 1d36397
add LRN layer for fluid
walloollaw 3bf1707
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
walloollaw bb9c711
add documentation for LRN layer
walloollaw bb66828
add paper reference for LRN layer
walloollaw 308b3c2
add seperate documentation for LRN layer
walloollaw 0caf2f2
rm lrn.py in doc/fluid/dev/src
walloollaw 246367d
merge with lod_reset branch
walloollaw 4316a67
change code style in lrn
walloollaw ce8e013
fix style of comments in lrn
walloollaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,7 @@ | |
| 'one_hot', | ||
| 'autoincreased_step_counter', | ||
| 'lod_reset', | ||
| 'lrn', | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -3388,3 +3389,73 @@ def lod_reset(x, y=None, target_lod=None): | |
| raise ValueError("y and target_lod should not be both None.") | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None): | ||
| """ | ||
| Local Response Normalization Layer. This layer performs a kind of | ||
| "lateral inhibition" by normalizing over local input regions. | ||
|
|
||
| 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. | ||
|
|
||
| Refer to `ImageNet Classification with Deep Convolutional Neural Networks | ||
| <https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_ | ||
|
|
||
| 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). | ||
|
Contributor
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
Contributor
Author
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
kind -> type
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.
done