func OptimizeWeights(init_weights []float64, all_labels [][]float64) []float64 { log.Printf("optimize ...\n") var solver optimization.Solver if *solver_name == "gradient" { solver = &optimization.GradientDescentSolver{} } if *solver_name == "conjugate" { solver = &optimization.GradientDescentSolver{} } if solver == nil { solver = &optimization.LmBFGSSolver{} } solver.Init(map[string]interface{}{ "MaxIter": *max_iter, "LogFunc": func(level int, message string) { log.Printf("solver[level=%v]:%v", level, message) }, }) problem := &optimization.Problem{ ValueAndGradientFunc: func(p optimization.Point) (float64, optimization.Point) { return opt_func_grad(p, all_labels) }, } m, v := solver.Solve(problem, optimization.VectorDensePoint(init_weights)) log.Printf("solver min value %v #f=%v #g=%v at %v\n", v, problem.NumValue, problem.NumGradient, m.String()) weights := optimization.VectorToDense(m) normalize(weights) return weights }
func test_solver(p optimization.Point, name string, solver optimization.Solver) { log.Printf("solver %s ...\n", name) solver.Init(map[string]interface{}{ "MaxIter": 48, "LogFunc": func(level int, message string) { log.Print(message) }, }) problem := &optimization.Problem{ ValueFunc: opt_func, GradientFunc: opt_grad, } m, y := solver.Solve(problem, p) log.Printf("solver %s min value %v at %v #f=%v #g=%v\n", name, y, m.String(), problem.NumValue, problem.NumGradient) }
func test_solver(A, b []float64, p optimization.Point, name string, solver optimization.Solver) { log.Printf("solver %s ...\n", name) solver.Init(map[string]interface{}{ "MaxIter": *max_iter, "LogFunc": func(level int, message string) { log.Printf(message) }, }) problem := &optimization.Problem{ ValueFunc: func(p optimization.Point) float64 { return opt_func(A, b, p) }, GradientFunc: func(p optimization.Point) optimization.Point { return opt_grad(A, b, p) }, } m, v := solver.Solve(problem, p) log.Printf("solver %s min value %v #f=%v #g=%v at %v\n", name, v, problem.NumValue, problem.NumGradient, m.String()) }