Skip to content

Local Response Normalization#4667

Merged
soumith merged 4 commits into
pytorch:masterfrom
Kaixhin:lrn
Jan 16, 2018
Merged

Local Response Normalization#4667
soumith merged 4 commits into
pytorch:masterfrom
Kaixhin:lrn

Conversation

@Kaixhin

@Kaixhin Kaixhin commented Jan 15, 2018

Copy link
Copy Markdown
Contributor

Functional implementation + module for LRN based on @jiecaoyu's implementation. Let me know if there should be any naming changes/if we want an across/within channel flag like Caffe/what to do with CrossMapLRN2d etc.

Originally AlexNet doesn't seem to do an average (just sums), whilst Caffe does do an average... I've written the docs as a sum as in the original, so will either need to change the docs or implementation depending on which one we want (or even make it another option).

Comment thread torch/nn/functional.py Outdated


def local_response_norm(input, size, alpha=1e-4, beta=0.75, k=1):
div = avg_pool3d(input.pow(2).unsqueeze(1), (size, 1, 1),

This comment was marked as off-topic.

Comment thread torch/nn/functional.py Outdated
def local_response_norm(input, size, alpha=1e-4, beta=0.75, k=1):
div = avg_pool3d(input.pow(2).unsqueeze(1), (size, 1, 1),
stride=1, padding=(size // 2, 0, 0)).squeeze(1)
div.mul_(alpha).add_(k).pow_(beta)

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/nn/modules/normalization.py Outdated
from .. import functional as F


class LocalResponseNorm2d(Module):

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/nn/functional.py
)


def local_response_norm(input, size, alpha=1e-4, beta=0.75, k=1):

This comment was marked as off-topic.

@apaszke apaszke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk what the formula for LRN is, so I haven't checked that, but the code looks good in general

Comment thread torch/nn/functional.py Outdated
div = div.view(sizes[0], 1, sizes[1], sizes[2], sizes[3] * sizes[4])
div = pad(div, (0, 0, 0, 0, size // 2, (size - 1) // 2))
div = avg_pool3d(div, (size, 1, 1), stride=1).squeeze(1)
div = div.view(sizes[0], sizes[1], sizes[2], sizes[3], sizes[4])

This comment was marked as off-topic.

This comment was marked as off-topic.

@Kaixhin

Kaixhin commented Jan 15, 2018

Copy link
Copy Markdown
Contributor Author

Pinging @jiecaoyu @fmassa for an additional review (to check the formula/address any points in my initial comment) before merging.

@vadimkantorov

Copy link
Copy Markdown
Contributor

+1 for clearing up the difference with CrossMapLRN2d (is it across-channels / within-channel thing?)

Specify 'across-channels'
@Kaixhin

Kaixhin commented Jan 15, 2018

Copy link
Copy Markdown
Contributor Author

@vadimkantorov this is across-channels, same as CrossMapLRN2d (which should be deprecated). The within-channel version seems to be far less common, so I'd go for merging this across-channel version in (which actually now supports 1D, 2D, 3D, 4D etc.), and then adding that as another option with an extra optional flag if someone wants it.

@apaszke The canonical reference is AlexNet, section 3.3, which sums the normalisation bit across channels. However, Torch documents SpatialCrossMapLRN as being a mean over channels, as does Caffe, so I'm changing the implementation and docs back to using the mean to be consistent with most implementations.

@soumith soumith merged commit 2260649 into pytorch:master Jan 16, 2018
@Kaixhin Kaixhin deleted the lrn branch January 16, 2018 03:34
@hdmetor hdmetor mentioned this pull request Jan 16, 2018
@vadimkantorov

vadimkantorov commented Jan 16, 2018

Copy link
Copy Markdown
Contributor

@Kaixhin Python disallows starting a variable name with a digit (minor docs thing):

>>> 2d_signal = autograd.Variable(torch.randn(32, 5, 24, 24))
>>> 4d_signal = autograd.Variable(torch.randn(16, 5, 7, 7, 7, 7))
>>> 2d_output = lrn(2d_signal)
>>> 4d_output = lrn(4d_signal)

@Kaixhin

Kaixhin commented Jan 16, 2018

Copy link
Copy Markdown
Contributor Author

@vadimkantorov thanks for noting - have adjusted in #4681 since I forgot to add LRN to the autogenerated docs.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
* Local Response Normalization

* Add 1D and 3D LRN

* Generalise LRN to higher dims

* Use mean instead of sum

Specify 'across-channels'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants