commit 191e835dafe04cd4e9ae022ce9660d047c8d29ae
parent f8fd68053bde09d3c129f7587de9bc055e522ae8
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 21 Jun 2020 22:22:17 -0700
Some cleaning up
Diffstat:
| M | bpnn.cpp | | | 54 | +++++++++++++++--------------------------------------- |
1 file changed, 15 insertions(+), 39 deletions(-)
diff --git a/bpnn.cpp b/bpnn.cpp
@@ -42,7 +42,6 @@ Network::Network(char* path, int inputs, int hidden, int outputs, int neurons, i
{
learning_rate = rate;
fpath = path;
- // std::cout << "Initialized path to " << fpath << "\n";
length = hidden + 2;
batch_size = batch_sz;
FILE* fptr = fopen(fpath, "r");
@@ -57,10 +56,6 @@ Network::Network(char* path, int inputs, int hidden, int outputs, int neurons, i
(*labels)(i,0) = label;
}
float* batchptr = batch;
- // std::cout << "Dumping batch contents...\n";
- // for (int i = 0; i < batch_size*4; i++) {
- // std::cout << batch[i] << "\n";
- // }
layers.emplace_back(batchptr, batch_size, inputs);
for (int i = 0; i < hidden; i++) {
layers.emplace_back(batch_size, neurons);
@@ -70,7 +65,6 @@ Network::Network(char* path, int inputs, int hidden, int outputs, int neurons, i
layers[i].initWeights(layers[i+1]);
}
batches = 1;
- // std::cout << "Initial batch of:\n" << *layers[0].contents << "\nwith labels\n" << *labels;
}
Eigen::MatrixXd Network::activate(Eigen::MatrixXd matrix)
@@ -135,32 +129,25 @@ float Network::accuracy()
float correct = 0;
int total = 0;
for (int i = 0; i < layers[length-1].contents->rows(); i++) {
- // printf("%i vs %f\n", (int)(*labels)(i, 0), (*layers[length-1].contents)(i, 0));
if ((*labels)(i, 0) == round((*layers[length-1].contents)(i, 0))) {
- //printf("Correct!\n");
correct += 1;
}
total = i;
}
- // printf("CORRECT: %f/%i\n", correct, batch_size);
- // std::cout << (1.0/batch_size) * correct << "\n";
return (1.0/batch_size) * correct;
}
void Network::backpropagate()
{
- // std::cout << "\nROUND\n\n\n\n\n\n";
std::vector<Eigen::MatrixXd> gradients;
std::vector<Eigen::MatrixXd> deltas;
- Eigen::MatrixXd error = ((*layers[length-1].contents) - (*labels));//.cwiseProduct(((*layers[length-1].contents) - (*labels)));
+ Eigen::MatrixXd error = ((*layers[length-1].contents) - (*labels));
gradients.push_back(error.cwiseProduct(*layers[length-1].dZ));
deltas.push_back((*layers[length-2].contents).transpose() * gradients[0]);
int counter = 1;
for (int i = length-2; i >= 1; i--) {
- // std::cout << gradients[counter-1] << "\n\nTHAT WAS GRADIENT\n\n" <<*layers[i].weights << "\n\nTHAT WAS WEIGHTS\n"
gradients.push_back((gradients[counter-1] * layers[i].weights->transpose()).cwiseProduct(*layers[i].dZ));
deltas.push_back((*layers[i-1].contents).transpose() * gradients[counter]);
- // std::cout << gradients[counter] << "\n\nand\n\n" << *layers[i-1].weights << "\n\nweights\n\n" << deltas[counter] << "\n\ndelta above\n\n\n\n\n";
counter++;
}
for (int i = 1; i < gradients.size(); i++) {
@@ -176,7 +163,6 @@ void Network::update_layer(float* vals, int datalen, int index)
}
}
-// LSP is cool
int Network::next_batch(char* path)
{
FILE* fptr = fopen(path, "r");
@@ -243,12 +229,10 @@ float Network::test(char* path)
int datalen = batch_size * inputs;
float batch[datalen];
int label = -1;
- printf("End line is %i\n", batch_size*((i/batch_size)+1));
for (int j = 1; j < batch_size*((i/batch_size)+1); j++) {
if (fgets(line, 1024, fptr)==NULL) {
break;
}
- printf("End line is %i\n", batch_size*((i/batch_size)+1));
if (i >= (i/batch_size)*batch_size) {
int k = i - ((i/batch_size)*batch_size);
sscanf(line, "%f,%f,%f,%f,%i", &batch[0 + (k * inputs)],
@@ -260,21 +244,16 @@ float Network::test(char* path)
float* batchptr = batch;
update_layer(batchptr, datalen, 0);
fclose(fptr);
- std::cout << "Batch is: \n"<< *layers[0].contents << "\n and labels \n" << *labels << "\n";
cost_sum += cost();
acc_sum += accuracy();
finalcount = i;
}
- // std::cout << *layers[0].contents << "\n\n and \n\n" << *labels;
- // std::cout << "TEST COST: " << 1.0/((float) linecount) * totalcost << "\n"
float chunks = ((float)finalcount/batch_size)+1;
- // std::cout << acc_sum << " " << chunks << " " << acc_sum/chunks << "\n";
return acc_sum/chunks;
}
void demo(int total_epochs)
{
- // std::cout << "\n\n\n";
int linecount = prep_file("./extra.txt", "./shuffled.txt");
Network net ("./shuffled.txt", 4, 1, 1, 5, 10, 1);
float epoch_cost = 1000;
@@ -284,32 +263,29 @@ void demo(int total_epochs)
printf("Beginning train on %i instances for %i epochs...\n", linecount, total_epochs);
while (epochs < total_epochs) {
auto ep_begin = std::chrono::high_resolution_clock::now();
- // int linecount = prep_file("./data_banknote_authentication.txt");
float cost_sum = 0;
float acc_sum = 0;
- // double times[5] = {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();
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);
+ 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;
@@ -317,8 +293,8 @@ void demo(int total_epochs)
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("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]);
+ 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++;