Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created August 23, 2017 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixpickle/8b576891b0ee25c1732671610dcd41f1 to your computer and use it in GitHub Desktop.
Save unixpickle/8b576891b0ee25c1732671610dcd41f1 to your computer and use it in GitHub Desktop.
Tree gradient, numerical "proof"
// Numerically verify that you should average gradients at
// the leaves in a gradient boosted tree.
package main
import (
"fmt"
"github.com/unixpickle/anydiff"
"github.com/unixpickle/anyvec/anyvec64"
)
func main() {
x := anydiff.NewConst(anyvec64.MakeVectorData([]float64{3, 1.5, 2}))
a := anydiff.NewVar(anyvec64.MakeVectorData([]float64{2.25}))
b := anydiff.NewVar(anyvec64.MakeVectorData([]float64{2}))
grad := anydiff.NewGrad(a, b)
computeLoss(x, a, b).Propagate(anyvec64.MakeVectorData([]float64{1}), grad)
fmt.Println(grad[a].Data(), grad[b].Data())
}
func computeLoss(x, a, b anydiff.Res) anydiff.Res {
vec := anydiff.Concat(a, a, b)
return anydiff.Mul(
anydiff.Dot(vec, x),
anydiff.Pow(anydiff.Dot(vec, vec), -0.5),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment