jacobian

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 5ba6866477b4f7b6212a05c27c7ebb25629a8476
parent b1c3f4e5486a75d0aa136d2b12ea9baa47f980f5
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Fri, 19 Jun 2020 10:28:45 -0700

Working towards fixing mr_bpnn_1 and writing mr_bpnn_2

Diffstat:
Mbpnn.cpp | 20++++++++++++--------
Mbpnn.hpp | 2+-
Mmr_bpnn_1.cpp | 22++++++++++------------
3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/bpnn.cpp b/bpnn.cpp @@ -112,7 +112,7 @@ float Network::cost() { float sum = 0; for (int i = 0; i < layers[length-1].contents->rows(); i++) { - sum += pow((*labels)(i, 0)*100 - (*layers[length-1].contents)(i, 0)*100,2); + sum += pow((*labels)(i, 0) - (*layers[length-1].contents)(i, 0),2); } return (1.0/batch_size) * sum; } @@ -122,7 +122,7 @@ void Network::backpropagate() // std::cout << "\nROUND\n\n\n\n\n\n"; std::vector<Eigen::MatrixXd> gradients; std::vector<Eigen::MatrixXd> deltas; - gradients.push_back(((*layers[length-1].contents) - (*labels)).cwiseProduct(*layers[length-1].dZ)); + gradients.push_back((((*layers[length-1].contents) - (*labels)).cwiseProduct(((*layers[length-1].contents) - (*labels)))).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--) { @@ -160,7 +160,7 @@ int Network::next_batch() if (i >= batches) { for (int j = 0; j < batch_size; j++) { fgets(line, 1024, fptr); - sscanf(line, "%f,%f,%f,%f,%i", &batch[0 + (j * inputs)], + sscanf(line, "%f,%f,%f,%f,%lf", &batch[0 + (j * inputs)], &batch[1 + (j * inputs)], &batch[2 + (j * inputs)], &batch[3 + (j * inputs)], &label); (*labels)(j, 0) = label; @@ -194,7 +194,7 @@ int prep_file(char* path) return count; } -void Network::test(char* path) +float Network::test(char* path) { int rounds = 1; int exit = 0; @@ -213,7 +213,7 @@ void Network::test(char* path) if (i >= rounds) { for (int j = 0; j < batch_size; j++) { fgets(line, 1024, fptr); - sscanf(line, "%f,%f,%f,%f,*i", &batch[0 + (j * inputs)], &batch[1 + (j * inputs)], &batch[2 + (j * inputs)], &batch[3 + (j * inputs)]); + sscanf(line, "%f,%f,%f,%f,%lf", &batch[0 + (j * inputs)], &batch[1 + (j * inputs)], &batch[2 + (j * inputs)], &batch[3 + (j * inputs)], &(*labels)(j)); } } } @@ -222,11 +222,13 @@ void Network::test(char* path) fclose(fptr); feedforward(); list_net(); + // next_batch(); // std::cout << *layers[length-1].contents << "\n\nvs\n\n" << *labels << "\n\n"; totalcost += cost(); rounds++; } - std::cout << "TEST COST: " << 1.0/((float) linecount) * totalcost << "\n"; + // std::cout << "TEST COST: " << 1.0/((float) linecount) * totalcost << "\n"; + return 1.0/((float) linecount / (float) batches) * totalcost; } void demo() @@ -241,8 +243,8 @@ void demo() // net.backpropagate(); // std::cout << net.cost() << "\n"; - while (epochs < 500) { - int linecount = prep_file("./data_banknote_authentication.txt"); + while (epochs < 10) { + // int linecount = prep_file("./data_banknote_authentication.txt"); float cost_sum = 0; for (int i = 0; i < linecount-net.batch_size; i+=net.batch_size) { net.feedforward(); @@ -260,4 +262,6 @@ void demo() printf("EPOCH %i: Cost is %f for %i instances.\n", epochs, epoch_cost, linecount); epochs++; } + net.test("./test.txt"); + net.list_net(); } diff --git a/bpnn.hpp b/bpnn.hpp @@ -48,7 +48,7 @@ public: float cost(); void backpropagate(); int next_batch(); - void test(char* path); + int test(char* path); }; void demo(); diff --git a/mr_bpnn_1.cpp b/mr_bpnn_1.cpp @@ -22,7 +22,6 @@ struct pair* map (struct pair input_pair) break; } } - net.batches=1; epoch_cost = 1.0/((float) linecount) * cost_sum; printf("EPOCH %i: Cost is %f for %i instances.\n", epochs, epoch_cost, linecount); epochs++; @@ -37,7 +36,7 @@ struct pair* map (struct pair input_pair) int inputs = net.layers[0].contents->cols(); int datalen = net.batch_size * inputs; float batch[datalen]; - for (int i = 0; i < net.batch_size*rounds + 1; i++) { + for (int i = 0; i < + 1; i+= net.batch_size) { if (fgets(line, 1024, fptr)==NULL) { exit = -1; } @@ -51,16 +50,15 @@ struct pair* map (struct pair input_pair) float *batchptr = batch; net.update_layer(batchptr, datalen, 0); net.feedforward(); - for (int i = 0; i < round(linecount / (float) net.batch_size); i++) { - char* key = new char[1024]; - sprintf(key, "%i", net.batch_size); - output_pairs[i].key = (void*) key; - float* cost = new float; - *cost = net.cost(); - // std::cout << *cost << " for " << i << "\n"; - output_pairs[i].value = cost; - } + char* key = new char[1024]; + sprintf(key, "%i", net.batch_size); + output_pairs[rounds-1].key = (void*) key; + float* cost = new float; + *cost = net.cost(); + // std::cout << *cost << " for " << i << "\n"; + output_pairs[rounds-1].value = cost; totalcost += net.cost(); + net.next_batch(); rounds++; } return output_pairs; @@ -99,7 +97,6 @@ void translate(char* path) sscanf(line, "%p %p", &addr1, &addr2); sprintf(newline, "%s %f", (char*)addr1, *(float*)addr2); int batch_num = strtol((char*)addr1, NULL, 10); - printf("fspeijfofselk %i\n", batch_num); fprintf(wptr, "%s %f (avg %f)\n",(char*)addr1, *(float*)addr2, ((*(float*)addr2/(float)batch_num))); } fclose(rptr); @@ -111,4 +108,5 @@ void translate(char* path) int main(int argc, char** argv) { begin(argv[2], map, reduce, translate, strtol(argv[1], NULL, 10), 6, argv[3], strtol(argv[4], NULL, 10)); + // demo(); }