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

Function train

examples/machine_learning/softmax_regression.cpp:70–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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