Skip to content
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

Local Response Normalization[W.I.P] #312

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Flux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Juno, Requires, Reexport
using MacroTools: @forward

export Chain, Dense, RNN, LSTM, GRU, Conv,
Dropout, LayerNorm, BatchNorm,
Dropout, LayerNorm, BatchNorm, LRNorm,
params, mapleaves, cpu, gpu

@reexport using NNlib
Expand Down
48 changes: 48 additions & 0 deletions src/layers/normalise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,51 @@ function Base.show(io::IO, l::BatchNorm)
(l.λ == identity) || print(io, ", λ = $(l.λ)")
print(io, ")")
end

"""
LRNorm(k, n::Integer, α, β)

Local Response Normalization layer. The `n` input should be the number of
adjacent kernel maps to sum over.

See [ImageNet Classification with Deep Convolutional
Neural Networks](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf).

Example:

```julia
m = LRNorm(0.2, 5, 0.5, 2)
m(ip) #ip is the 4-D input
"""
mutable struct LRNorm{F,I}
k
n::I
α::F
β
end

function (LRN::LRNorm)(x)
w,h,C,N = size(x)
temp = zeros(size(x))
for z_=1:N
Copy link
Member

Choose a reason for hiding this comment

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

Slightly cleaner to do for z_=1:N, x_=1:w, ...; end here.

for x_=1:w
for y_=1:h
for i=1:C
constant = LRN.k
for j=max(1,i-div(LRN.n,2)): min(C, i+div(LRN.n,2))
constant += LRN.α * (x[x_,y_,j,z_]^2)
end
constant = constant^LRN.β
temp[x_,y_,i,z_] = x[x_,y_,i,z_] / constant
end
end
end
end
return temp
end

children(LRN::LRNorm) =
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to use treelike here?

(LRN.k, LRN,n, LRN.α, LRN.β)

mapchildren(f, LRN::LRNorm) =
LRNorm(f(LRN.k), LRN.n, f(LRN.α), f(LRN.β))