commit c1621a248c76f048d48d212c6a1656c704ef12f2
parent ba7dedea1dfed183ab4bd83a6195142f64eea54a
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sat, 20 Jun 2020 19:09:14 -0700
Training batch advancement works properly
Diffstat:
| M | bpnn.cpp | | | 37 | ++++++++++++++++++------------------- |
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -192,7 +192,6 @@ int Network::next_batch(char* path)
float* batchptr = batch;
update_layer(batchptr, datalen, 0);
fclose(fptr);
- std::cout << "Next batch of:\n" << *layers[0].contents << "\nwith labels\n" << *labels << "\n";
return 0;
}
@@ -207,6 +206,7 @@ int prep_file(char* path, char* out_path)
lines.emplace_back(line);
count++;
}
+ lines[lines.size()-1] = lines[lines.size()-1] + "\n";
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(lines.begin(), lines.end(), g);
@@ -279,37 +279,36 @@ void demo(int total_epochs)
float acc_sum = 0;
double times[5] = {0};
for (int i = 0; i <= linecount-net.batch_size; i+=net.batch_size) {
- auto feed_begin = std::chrono::high_resolution_clock::now();
+ // auto feed_begin = std::chrono::high_resolution_clock::now();
net.feedforward();
- auto back_begin = std::chrono::high_resolution_clock::now();
+ // auto back_begin = std::chrono::high_resolution_clock::now();
net.backpropagate();
- auto cost_begin = std::chrono::high_resolution_clock::now();
+ // auto cost_begin = std::chrono::high_resolution_clock::now();
cost_sum += net.cost();
// std::cout << acc_sum << " "<< net.accuracy() << " " << net.batch_size << "\n";
- auto acc_begin = std::chrono::high_resolution_clock::now();
+ // auto acc_begin = std::chrono::high_resolution_clock::now();
acc_sum += net.accuracy();
// std::cout << net.cost() << " as it is " << net.labels[0] << " vs " << *net.layers[net.length-1].contents << "\n";
- auto batch_begin = std::chrono::high_resolution_clock::now();
+ // auto batch_begin = std::chrono::high_resolution_clock::now();
- int exit = net.next_batch(net.fpath);
- auto loop_end = std::chrono::high_resolution_clock::now();
- times[0] += std::chrono::duration_cast<std::chrono::nanoseconds>(back_begin - feed_begin).count() / pow(10,9);
- times[1] += std::chrono::duration_cast<std::chrono::nanoseconds>(cost_begin - back_begin).count() / pow(10,9);
- times[2] += std::chrono::duration_cast<std::chrono::nanoseconds>(acc_begin - cost_begin).count() / pow(10,9);
- times[3] += std::chrono::duration_cast<std::chrono::nanoseconds>(batch_begin - acc_begin).count() / pow(10,9);
- times[4] += std::chrono::duration_cast<std::chrono::nanoseconds>(loop_end - batch_begin).count() / pow(10,9);
- net.batches++;
- if (exit == -1) {
- break;
+ if (i != linecount-net.batch_size) { // Don't try to advance batch on final batch.
+ net.next_batch(net.fpath);
}
+ // auto loop_end = std::chrono::high_resolution_clock::now();
+ // times[0] += std::chrono::duration_cast<std::chrono::nanoseconds>(back_begin - feed_begin).count() / pow(10,9);
+ // times[1] += std::chrono::duration_cast<std::chrono::nanoseconds>(cost_begin - back_begin).count() / pow(10,9);
+ // times[2] += std::chrono::duration_cast<std::chrono::nanoseconds>(acc_begin - cost_begin).count() / pow(10,9);
+ // times[3] += std::chrono::duration_cast<std::chrono::nanoseconds>(batch_begin - acc_begin).count() / pow(10,9);
+ // times[4] += std::chrono::duration_cast<std::chrono::nanoseconds>(loop_end - batch_begin).count() / pow(10,9);
+ net.batches++;
}
epoch_accuracy = 1.0/((float) linecount/net.batch_size) * acc_sum;
epoch_cost = 1.0/((float) linecount/net.batch_size) * cost_sum;
auto ep_end = std::chrono::high_resolution_clock::now();
double epochtime = (double) std::chrono::duration_cast<std::chrono::nanoseconds>(ep_end-ep_begin).count() / pow(10,9);
- printf("───\nEpoch %i/%i - time %f - cost %f - acc %f\n", epochs+1, total_epochs, epochtime, epoch_cost, epoch_accuracy);
- printf("Avg time spent across %i batches: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch.\n", net.batches, times[0]/net.batches, times[1]/net.batches, times[2]/net.batches, times[3]/net.batches, times[4]/net.batches);
- printf("Time spent across epoch: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch, %lf other.\n", times[0], times[1], times[2], times[3], times[4], epochtime-times[0]-times[1]-times[2]-times[3]-times[4]);
+ printf("Epoch %i/%i - time %f - cost %f - acc %f\n", epochs+1, total_epochs, epochtime, epoch_cost, epoch_accuracy);
+ // printf("Avg time spent across %i batches: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch.\n", net.batches, times[0]/net.batches, times[1]/net.batches, times[2]/net.batches, times[3]/net.batches, times[4]/net.batches);
+ // printf("Time spent across epoch: %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc, %lf on next batch, %lf other.\n", times[0], times[1], times[2], times[3], times[4], epochtime-times[0]-times[1]-times[2]-times[3]-times[4]);
net.batches=1;
epochs++;