MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / train

Function train

examples/machine_learning/logistic_regression.cpp:66–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66array train(const array &X, const array &Y, double alpha = 0.1,
67 double lambda = 1.0, double maxerr = 0.01, int maxiter = 1000,
68 bool verbose = false) {
69 // Initialize parameters to 0
70 array Weights = constant(0, X.dims(1), Y.dims(1));
71
72 array J, dJ;
73 float err = 0;
74
75 for (int i = 0; i < maxiter; i++) {
76 // Get the cost and gradient
77 cost(J, dJ, Weights, X, Y, lambda);
78
79 err = max<float>(abs(J));
80 if (err < maxerr) {
81 printf("Iteration %4d Err: %.4f\n", i + 1, err);
82 printf("Training converged\n");
83 return Weights;
84 }
85
86 if (verbose && ((i + 1) % 10 == 0)) {
87 printf("Iteration %4d Err: %.4f\n", i + 1, err);
88 }
89
90 // Update the parameters via gradient descent
91 Weights = Weights - alpha * dJ;
92 }
93
94 printf("Training stopped after %d iterations\n", maxiter);
95 return Weights;
96}
97
98void benchmark_logistic_regression(const array &train_feats,
99 const array &train_targets,

Callers 2

logit_demoFunction · 0.70

Calls 4

constantFunction · 0.85
costFunction · 0.70
absFunction · 0.50
dimsMethod · 0.45

Tested by

no test coverage detected