Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add Kaiming He initialization, fixed Xavier initialization #311

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
37 changes: 31 additions & 6 deletions src/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ end

"""

xavier(a...)
xavier(a...; gain=1)

Xavier initialization. The `a` arguments are passed to `rand`. See
([Glorot and Bengio 2010](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
Xavier initialization. The `a` arguments are passed to `rand`. You can
change `gain` for different activation functions. `gain=1` at default.
See ([Glorot and Bengio 2010](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
for a description.
[Caffe](http://caffe.berkeleyvision.org/doxygen/classcaffe_1_1XavierFiller.html#details)
implements this slightly differently.
[Lasagne](http://lasagne.readthedocs.org/en/latest/modules/init.html#lasagne.init.GlorotUniform)
calls it `GlorotUniform`.

"""
function xavier(a...)
function xavier(a...; gain=1)
w = rand(a...)
if ndims(w) == 1
fanout = 1
Expand All @@ -37,8 +38,32 @@ function xavier(a...)
fanout = size(w, ndims(w))
fanin = div(length(w), fanout)
end
s = convert(eltype(w), sqrt(2 / (fanin + fanout)))
w = 2s*w-s
s = convert(eltype(w), sqrt( 2 / (fanin + fanout)))
s = sqrt(3) * gain * s
w = 2s .* w .- s
end

"""

kaiming(a...)

Kaiming He initialization. The `a` arguments are passed to `rand`. You can
change `gain` for different activation functions. `gain=sqrt(2)` at default.
See ([He et al. 2015](https://arxiv.org/abs/1502.01852)) for a description.
"""
function kaiming(a...; gain=sqrt(2))
w = rand(a...)
if ndims(w) == 1
fanin = length(w)
elseif ndims(w) == 2
fanin = size(w,2)
else
fanout = size(w, ndims(w))
fanin = div(length(w), fanout)
end
s = convert(eltype(w), sqrt(1 / fanin))
s = sqrt(3) * gain * s
w = 2s .* w .- s
end

"""
Expand Down