MCPcopy Create free account
hub / github.com/bwaldvogel/liblinear-java / newton

Method newton

src/main/java/de/bwaldvogel/liblinear/Newton.java:24–95  ·  view source on GitHub ↗
(double[] w)

Source from the content-addressed store, hash-verified

22 }
23
24 void newton(double[] w) {
25 int n = fun_obj.get_nr_variable();
26 int i, cg_iter;
27 double step_size;
28 double f, fold, actred;
29 double init_step_size = 1;
30 boolean search = true;
31 int iter = 1;
32 MutableInt inc = new MutableInt(1);
33 double[] s = new double[n];
34 double[] r = new double[n];
35 double[] g = new double[n];
36
37 final double alpha_pcg = 0.01;
38 double[] M = new double[n];
39
40 // calculate gradient norm at w=0 for stopping condition.
41 double[] w0 = new double[n];
42 for (i = 0; i < n; i++)
43 w0[i] = 0;
44 fun_obj.fun(w0);
45 fun_obj.grad(w0, g);
46
47 double gnorm0 = Blas.dnrm2_(n, g, inc);
48
49 f = fun_obj.fun(w);
50 fun_obj.grad(w, g);
51 double gnorm = Blas.dnrm2_(n, g, inc);
52 info("init f %5.3e |g| %5.3e%n", f, gnorm);
53
54 if (gnorm <= eps * gnorm0)
55 search = false;
56
57 while (iter <= max_iter && search) {
58 fun_obj.get_diag_preconditioner(M);
59 for (i = 0; i < n; i++)
60 M[i] = (1 - alpha_pcg) + alpha_pcg * M[i];
61 cg_iter = pcg(g, M, s, r);
62
63 fold = f;
64 MutableDouble fReference = new MutableDouble(f);
65 step_size = fun_obj.linesearch_and_update(w, s, fReference, g, init_step_size);
66 f = fReference.get();
67
68 if (step_size == 0) {
69 info("WARNING: line search fails%n");
70 break;
71 }
72
73 fun_obj.grad(w, g);
74 gnorm = Blas.dnrm2_(n, g, inc);
75
76 info("iter %2d f %5.3e |g| %5.3e CG %3d step_size %4.2e%n", iter, f, gnorm, cg_iter, step_size);
77
78 if (gnorm <= eps * gnorm0)
79 break;
80 if (f < -1.0e+32) {
81 info("WARNING: f < -1.0e+32%n");

Callers 1

train_oneMethod · 0.95

Calls 9

dnrm2_Method · 0.95
pcgMethod · 0.95
getMethod · 0.95
infoMethod · 0.80
get_nr_variableMethod · 0.65
funMethod · 0.65
gradMethod · 0.65
linesearch_and_updateMethod · 0.65

Tested by

no test coverage detected