Exemple #1
0
// TODO: A lot to optimize in this method.
func NumericalGradient(f func(linalg.VectorStructure) float64, x linalg.VectorStructure) linalg.VectorStructure {
	gradientValues := make([]float64, 0)
	for i, value := range x {
		tempEpsilon := x.Remove(i)
		plusEpsilon := tempEpsilon.Insert(i, value+GRADIENT_EPSILON)
		minusEpsilon := tempEpsilon.Insert(i, value-GRADIENT_EPSILON)
		gradientValues = append(gradientValues, (f(plusEpsilon)-f(minusEpsilon))/(2*GRADIENT_EPSILON))
	}
	return linalg.NewVector(gradientValues)
}
Exemple #2
0
func TestNumericalHessian(t *testing.T) {
	hessian := NumericalHessian(parabola, linalg.NewVector([]float64{10, 10}))
	fmt.Println(hessian)
}
Exemple #3
0
func TestGradientDescent(t *testing.T) {
	optimal, iter := GradientDescent(parabola, linalg.NewVector([]float64{-10, -10}), 0.01)
	fmt.Println(optimal, iter)
}