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

Method train

examples/machine_learning/neural_network.cpp:136–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134}
135
136double ann::train(const array &input, const array &target, double alpha,
137 int max_epochs, int batch_size, double maxerr, bool verbose) {
138 const int num_samples = input.dims(0);
139 const int num_batches = num_samples / batch_size;
140
141 double err = 0;
142
143 // Training the entire network
144 for (int i = 0; i < max_epochs; i++) {
145 for (int j = 0; j < num_batches - 1; j++) {
146 int st = j * batch_size;
147 int en = st + batch_size - 1;
148
149 array x = input(seq(st, en), span);
150 array y = target(seq(st, en), span);
151
152 // Propagate the inputs forward
153 vector<array> signals = forward_propagate(x);
154 array out = signals[num_layers - 1];
155
156 // Propagate the error backward
157 back_propagate(signals, y, alpha);
158 }
159
160 // Validate with last batch
161 int st = (num_batches - 1) * batch_size;
162 int en = num_samples - 1;
163 array out = predict(input(seq(st, en), span));
164 err = error(out, target(seq(st, en), span));
165
166 // Check if convergence criteria has been met
167 if (err < maxerr) {
168 printf("Converged on Epoch: %4d\n", i + 1);
169 return err;
170 }
171
172 if (verbose) {
173 if ((i + 1) % 10 == 0)
174 printf("Epoch: %4d, Error: %0.4f\n", i + 1, err);
175 }
176 }
177 return err;
178}
179
180int ann_demo(bool console, int perc, const dtype dt) {
181 printf("** ArrayFire ANN Demo **\n\n");

Callers 1

ann_demoFunction · 0.45

Calls 4

seqClass · 0.85
predictFunction · 0.70
errorFunction · 0.70
dimsMethod · 0.45

Tested by

no test coverage detected