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

added normal Glorot initialization #109

Open
wants to merge 1 commit 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
32 changes: 32 additions & 0 deletions nn/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,35 @@ func XavierUniform_(x *ts.Tensor, gainOpt ...float64) {

src.MustDrop()
}

// XavierNormal fills the input tensor with values according to the method
// described in the paper `Understanding the difficulty of training deep feedforward neural networks`
// using a normal distribution
//
// Also known as normal Glorot initialization.
//
// Paper: https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf
// Pytorch implementation: https://github.com/pytorch/pytorch/blob/df50f91571891ec3f87977a2bdd4a2b609d70afc/torch/nn/init.py#L337
func XavierNormal_(x *ts.Tensor, gainOpt ...float64) {
Copy link
Owner

Choose a reason for hiding this comment

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

  • We try to use Go conventional naming. If it's a unexposed function we can start with lowercase xavierNormal()
  • There a placeholder for GolorotInit at
    // glorotInit :
    . It would be great if you could fill in those things.
  • Try some unit test or example (make sure accuracy and no memory leak are the most priority as the function will be used everywhere (I often test result against Python in terms of accuracy).

gain := 1.0
if len(gainOpt) > 0 {
gain = gainOpt[0]
}

size := x.MustSize()
dtype := x.DType()
device := x.MustDevice()
fanIn, fanOut, err := CalculateFans(size)
if err != nil {
panic(err)
}

std := gain * math.Sqrt(2.0/float64(fanIn+fanOut))

// calculate uniform bounds from standard deviation
uniformInit := NewUniformInit(0, std)
src := uniformInit.InitTensor(size, device, dtype)
x.Copy_(src)

src.MustDrop()
}