Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
Created April 27, 2023 03:44
Show Gist options
  • Save eileen-code4fun/2a4db9c5980e3a9279bb99b09a97799a to your computer and use it in GitHub Desktop.
Save eileen-code4fun/2a4db9c5980e3a9279bb99b09a97799a to your computer and use it in GitHub Desktop.
Autograd Incorrect add and mul in Golang
func (v *Value) add(v1 *Value) *Value {
nv := newValue(v.data+v1.data, nil /* incorrect, should be []*Value{v, v1} */)
nv.backward = func() {
v.grad = /* incorrect, should be += */ nv.grad
v1.grad = /* incorrect, should be += */ nv.grad
}
return nv
}
func (v *Value) mul(v1 *Value) *Value {
nv := newValue(v.data*v1.data, nil /* incorrect, should be []*Value{v, v1} */)
nv.backward = func() {
v.grad = /* incorrect, should be += */ v1.data * nv.grad
v1.grad = /* incorrect, should be += */ v.data * nv.grad
}
return nv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment