jacobian

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

commit cf18167c4d30a20ce0d546125803d63615c32529
parent a595e677d4953c0492bc8547e2e6e1ac6992d017
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Sat, 27 Jun 2020 18:35:28 -0700

Working on speeding up file i/o

Diffstat:
Mbpnn.cpp | 11+++++------
Mbpnn.hpp | 2+-
Mexample.cpp | 20++++++++++----------
Mutils.cpp | 2+-
4 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/bpnn.cpp b/bpnn.cpp @@ -42,7 +42,7 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate) instances = prep_file(path, "./shuffled.txt"); length = 0; batch_size = batch_sz; - data = fopen("./shuffled.txt", "r"); + data = new std::ifstream("./shuffled.txt"); batches = 0; } @@ -200,16 +200,14 @@ void Network::update_layer(float* vals, int datalen, int index) int Network::next_batch() { auto init_begin = std::chrono::high_resolution_clock::now(); - char line[1024] = {' '}; + char line[1024]; int inputs = layers[0].contents->cols(); int datalen = batch_size * inputs; float batch[datalen]; int label = -1; auto get_begin = std::chrono::high_resolution_clock::now(); for (int i = 0; i < batch_size; i++) { - if (fgets(line, 1024, data)==NULL) { - break; - } + data->getline(line, 1024); sscanf(line, "%i,%f,%f,%f,%f", &label, &batch[0 + (i * inputs)], &batch[1 + (i * inputs)], &batch[2 + (i * inputs)], &batch[3 + (i * inputs)]); @@ -328,6 +326,7 @@ void Network::train(int total_epochs) printf("Time spent across epoch: %lf on next batch, %lf on feedforward, %lf on backprop, %lf on cost, %lf on acc.\n\n", times[0], times[1], times[2], times[3], times[4], epochtime-times[0]-times[1]-times[2]-times[3]-times[4]); batches=1; epochs++; - rewind(data); + data->clear(); + data->seekg(0); } } diff --git a/bpnn.hpp b/bpnn.hpp @@ -31,7 +31,7 @@ public: class Network { public: - FILE* data; + std::ifstream* data; int instances; std::vector<Layer> layers; diff --git a/example.cpp b/example.cpp @@ -3,14 +3,14 @@ int main() { - // Network net ("./data_banknote_authentication.txt", 10, 0.01, 0.001); - // net.add_layer(4, "linear"); - // net.add_layer(5, "sigmoid"); - // net.set_activation(1, lecun_tanh, lecun_tanh_deriv); - // net.add_layer(1, "resig"); - // net.initialize(); - // // net.list_net(); - // net.train(50); - // // net.list_net(); - printf("%i\n", wc("./data_banknote_authentication.txt")); + Network net ("./data_banknote_authentication.txt", 10, 0.01, 0.001); + net.add_layer(4, "linear"); + net.add_layer(5, "lecun_tanh"); + net.set_activation(1, lecun_tanh, lecun_tanh_deriv); + net.add_layer(1, "resig"); + net.initialize(); + // net.list_net(); + net.train(50); + net.list_net(); + //printf("%i\n", wc("./data_banknote_authentication.txt")); } diff --git a/utils.cpp b/utils.cpp @@ -58,7 +58,7 @@ std::function<double(double)> rectifier(double (*activation)(double)) uintmax_t wc(char const *fname) { - static const auto BUFFER_SIZE = 16*1024; + static const auto BUFFER_SIZE = 1024; int fd = open(fname, O_RDONLY); if(fd == -1) exit(1);