Created
August 23, 2017 00:13
-
-
Save unixpickle/8b576891b0ee25c1732671610dcd41f1 to your computer and use it in GitHub Desktop.
Tree gradient, numerical "proof"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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