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

NewRandnInit didn't works as expected when mean=0.0 #48

Closed
FelixHo opened this issue Jul 20, 2021 · 1 comment
Closed

NewRandnInit didn't works as expected when mean=0.0 #48

FelixHo opened this issue Jul 20, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@FelixHo
Copy link

FelixHo commented Jul 20, 2021

package main

import(
    "fmt"
    "github.com/sugarme/gotch/nn"
)

func main() {
	mat_3x5 := nn.NewRandnInit(0.0, 1.0).InitTensor([]int64{3, 5}, gotch.CPU)
	fmt.Printf("%v", mat_3x5)
}

output

   1.000     1.000     1.000     1.000     1.000
   1.000     1.000     1.000     1.000     1.000
   1.000     1.000     1.000     1.000     1.000

According to this line of code, when mean=0, all elements will be initialized with stdev

data[i] = float32(rand.NormFloat64()*r.mean + r.stdev)

This is different from PyTorch's randn function

""" Returns a tensor filled with random numbers from a normal distribution
with mean `0` and variance `1`"""

torch.randn(3, 5)

############### output ###########################
tensor([[ 0.1769, -2.0933, -0.8882,  0.0051,  0.9833],
        [-0.6342,  0.4093,  0.6266,  0.3935,  0.2045],
        [ 0.3055, -0.4522, -1.7044,  1.8426,  0.4553]])

I know MustRandn can solve this problem, but NewRandnInit as the default initialization method of Embedding will directly affect the initialization weights.

Is this a bug or is it designed this way for some reason?

sugarme added a commit that referenced this issue Jul 20, 2021
sugarme added a commit that referenced this issue Jul 20, 2021
@sugarme sugarme added the bug Something isn't working label Jul 20, 2021
@sugarme
Copy link
Owner

sugarme commented Jul 20, 2021

@FelixHo ,

Hopefully it's fixed. Thanks for pointing out.

@sugarme sugarme closed this as completed Jul 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants