jacobian

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

commit 127b1d82c9bdc3792ada3a77df60963f4a1458ad
parent a932a0d864b832d5cfc92de680346f05adcfed7c
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Sun, 21 Jun 2020 13:02:04 -0700

Trains in parallel (with bugs)

Diffstat:
Mbpnn.cpp | 9++-------
Mkerasdemo.py | 8++++----
Mmr_bpnn_2.cpp | 55++++++++++++++++++++++++++++---------------------------
3 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/bpnn.cpp b/bpnn.cpp @@ -134,9 +134,9 @@ 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)); + // 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"); + //printf("Correct!\n"); correct += 1; } total = i; @@ -152,8 +152,6 @@ void Network::backpropagate() std::vector<Eigen::MatrixXd> gradients; std::vector<Eigen::MatrixXd> deltas; Eigen::MatrixXd error = ((*layers[length-1].contents) - (*labels)).cwiseProduct(((*layers[length-1].contents) - (*labels))); - error = error.cwiseProduct(((*layers[length-1].contents) - (*labels)).cwiseProduct(((*layers[length-1].contents) - (*labels)))); - error = error.cwiseProduct(((*layers[length-1].contents) - (*labels)).cwiseProduct(((*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; @@ -272,7 +270,6 @@ float Network::test(char* path) void demo(int total_epochs) { - auto begin = std::chrono::high_resolution_clock::now(); // std::cout << "\n\n\n"; int linecount = prep_file("./extra.txt", "./shuffled.txt"); Network net ("./shuffled.txt", 4, 1, 1, 5, 10, 1); @@ -324,6 +321,4 @@ void demo(int total_epochs) } net.list_net(); printf("Test accuracy: %f\n", net.test("./test.txt")); - auto end = std::chrono::high_resolution_clock::now(); - std::cout <<std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count() << "ns aka " << (double) std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count() / pow(10,9) << "s" << std::endl; } diff --git a/kerasdemo.py b/kerasdemo.py @@ -17,21 +17,21 @@ from keras.models import Sequential from keras.layers import Dense init = time.time() # load the dataset -dataset = loadtxt('data_banknote_authentication.txt', delimiter=',') +dataset = loadtxt('extra.txt', delimiter=',') # split into input (X) and output (y) variables X = dataset[:,0:4] y = dataset[:,4] # define the keras model model = Sequential() -model.add(Dense(4, input_dim=4, activation='sigmoid')) +model.add(Dense(4, input_dim=4, activation='linear')) model.add(Dense(5, activation='sigmoid')) model.add(Dense(5, activation='sigmoid')) -model.add(Dense(1, activation='relu')) +model.add(Dense(1, activation='sigmoid')) # compile the keras model opt = keras.optimizers.SGD(lr=1) model.compile(loss='mse', optimizer=opt, metrics=['accuracy']) # fit the keras model on the dataset -model.fit(X, y, epochs=50, batch_size=1) +model.fit(X, y, epochs=50, batch_size=10) # evaluate the keras model _, accuracy = model.evaluate(X, y) print('Accuracy: %.2f' % (accuracy*100)) diff --git a/mr_bpnn_2.cpp b/mr_bpnn_2.cpp @@ -11,52 +11,51 @@ struct pair* map (struct pair input_pair) float epoch_cost = 1000; float epoch_accuracy = -1; int epochs = 0; + int total_epochs = 50; net->batches= 0; // net.feedforward(); // net.backpropagate(); // std::cout << net.cost() << "\n"; printf("Beginning train on %i instances for %i epochs...\n", linecount, 50); - while (epochs < 1) { + 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; - // int linecount = prep_file("./data_banknote_authentication.txt", "./shuffled.txt"); 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(); - if (i < 1*net->batch_size) { - printf("Batch accuracy: %f\n", net->accuracy()); - } + // 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); } + net->batches++; + // 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); } - 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); - net->batches=1; 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(); - printf("Epoch %i/%i - time %f - cost %f - acc %f\n", epochs+1, 50, (double) std::chrono::duration_cast<std::chrono::nanoseconds>(ep_end-ep_begin).count() / pow(10,9), epoch_cost, epoch_accuracy); + 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]); + net->batches=1; epochs++; } struct pair* output = new struct pair; @@ -64,13 +63,11 @@ struct pair* map (struct pair input_pair) strcpy(key, path); output[0].key = key; output[0].value = net; - printf("%p %p VALS\n", output[0].key, output[0].value); return output; } struct pair* reduce (struct pair* input_pairs) { - printf("%p %p VALS\n", input_pairs[0].key, input_pairs[0].value); struct pair* output = new struct pair[6]; for (int i = 0; input_pairs[i].key != 0x0; i++) { Network net = *(Network*)input_pairs[i].value; @@ -78,7 +75,6 @@ struct pair* reduce (struct pair* input_pairs) *acc = net.test("./test.txt"); output[i].key = input_pairs[i].key; output[i].value = acc; - printf("%s %f\n", (char*)output[i].key, *(float*)output[i].value); } return output; } @@ -107,6 +103,11 @@ void translate(char* path) int main(int argc, char** argv) { - // begin(argv[2], map, reduce, translate, strtol(argv[1], NULL, 10), 1, argv[3], strtol(argv[4], NULL, 10)); + auto prog_begin = std::chrono::high_resolution_clock::now(); + //begin(argv[2], map, reduce, translate, strtol(argv[1], NULL, 10), 1, argv[3], strtol(argv[4], NULL, 10)); demo(50); + auto prog_end = std::chrono::high_resolution_clock::now(); + std::cout << "Time: " << std::chrono::duration_cast<std::chrono::nanoseconds>(prog_end-prog_begin).count() / pow(10,9) << "\n"; + + // demo(50); }