jacobian

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

commit b07e463e3f022e29f28f867c794bcd2b39dd64bd
parent e2e47b932dbe0e1450a56a0bd1c6fe77d674e2a6
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Sun, 12 Jul 2020 11:20:40 -0700

Backprop doesn't segfault

Diffstat:
Mexample.cpp | 17++++++++++-------
Msrc/bpnn.cpp | 24++++++------------------
2 files changed, 16 insertions(+), 25 deletions(-)

diff --git a/example.cpp b/example.cpp @@ -13,14 +13,17 @@ double bench(int batch_sz) net.init_decay("exp", 1, 10); net.initialize(); // checks(net); - net.next_batch(); - net.feedforward(); - std::cout << net.cost() << " " << net.accuracy() << "\n"; - //for (int i = 0; i < 50; i++) { - // net.train(); - //} + for (int i = 0; i < 100; i++) { + net.next_batch(); + net.feedforward(); + net.backpropagate(); + std::cout << net.cost() << " " << net.accuracy() << "\n"; + } + // for (int i = 0; i < 1; i++) { + // net.train(); + // } auto end = std::chrono::high_resolution_clock::now(); - net.list_net(); + //net.list_net(); return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() / pow(10,9); } diff --git a/src/bpnn.cpp b/src/bpnn.cpp @@ -222,29 +222,17 @@ float Network::accuracy() return (1.0/batch_size) * correct; } -#define DELTA 1 void Network::backpropagate() { std::vector<Eigen::MatrixXf> gradients; std::vector<Eigen::MatrixXf> deltas; Eigen::MatrixXf error (layers[length-1].contents->rows(), layers[length-1].contents->cols()); - for (int i = 0; i < layers[length-1].contents->rows(); i++) { - for (int j = 0; j < layers[length-1].contents->cols(); j++) { - if (j == (*labels)(i,0)) { - float sum = 0; - for (int k = 0; k < layers[length-1].contents->cols(); k++) { - if (k == j) continue; - float intermediate = (*layers[length-1].contents)(i,j) - (*labels)(i,0); - if (intermediate > 0) sum+=1; - } - if (sum == 0) error(i,j) = 0; // IEEE floats are weird - else error(i,j) = -sum; - } - else { - float classloss = (*layers[length-1].contents)(i,j) - (*labels)(i,0) + DELTA; - if (classloss > 0) error(i, j) = 1; - else error(i, j) = 0; - } + for (int i = 0; i < error.rows(); i++) { + for (int j = 0; j < error.cols(); j++) { + float truth; + if (j==(*labels)(i,0)) truth = 1; + else truth = 0; + error(i,j) = truth - (*layers[length-1].contents)(i,j); } } // std::cout << error << "\n\n";