jacobian

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

commit 3dd39f3e4573c243d6328fe263fef5c1c1a05038
parent 24918d89d814946c10ce8578adc3dc0388a85a76
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Thu, 11 Jun 2020 22:55:46 -0700

Full init, broken feedforward

Diffstat:
Mtest.cpp | 65+++++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 51 insertions(+), 14 deletions(-)

diff --git a/test.cpp b/test.cpp @@ -3,7 +3,6 @@ #include <vector> #include <array> #include <iostream> -#include <cstdlib> #include <cstdio> class Node; @@ -45,7 +44,8 @@ public: Eigen::MatrixXd* weights; Layer(float* vals, int rows, int columns); - void initWeights(Layer next, int batch_sz); + Layer(int rows, int columns); + void initWeights(Layer next); }; Layer::Layer(float* vals, int batch_sz, int nodes) @@ -53,17 +53,30 @@ Layer::Layer(float* vals, int batch_sz, int nodes) contents = new Eigen::MatrixXd (batch_sz, nodes); int datalen = batch_sz*nodes; for (int i = 0; i < datalen; i++) { - (*contents)((int)i / 4,i%nodes) = vals[i]; + (*contents)((int)i / nodes,i%nodes) = vals[i]; } - std::cout << *contents << "\n"; + std::cout << *contents << "\n\n"; } -void Layer::initWeights(Layer next, int batch_sz) +Layer::Layer(int batch_sz, int nodes) { - weights = new Eigen::MatrixXd (contents->rows(), next.contents->cols()); + contents = new Eigen::MatrixXd (batch_sz, nodes); + int datalen = batch_sz*nodes; + for (int i = 0; i < datalen; i++) { + (*contents)((int)i / nodes,i%nodes) = 0; + } + std::cout << *contents << "\n\n"; +} + +void Layer::initWeights(Layer next) +{ + weights = new Eigen::MatrixXd (contents->cols(), next.contents->cols()); + printf("%i x %i\n", weights->rows(), weights->cols()); + int nodes = weights->cols(); for (int i = 0; i < (weights->rows()*weights->cols()); i++) { - *weights << rand(); + (*weights)((int)i / nodes, i%nodes) = rand() / double(RAND_MAX); } + std::cout << *weights << "\n\n"; } class Network { @@ -72,10 +85,13 @@ public: int length; Network(char* path, int inputs, int hidden, int outputs, int neurons, int batch_sz); + void feedforward(); + void activate(Eigen::MatrixXd matrix); }; Network::Network(char* path, int inputs, int hidden, int outputs, int neurons, int batch_sz) { + length = hidden + 2; FILE* fptr = fopen(path, "r"); int datalen = batch_sz*inputs; float batch[datalen]; @@ -85,18 +101,39 @@ Network::Network(char* path, int inputs, int hidden, int outputs, int neurons, i sscanf(line, "%f,%f,%f,%f,*f", &batch[0+(i*inputs)], &batch[1+(i*inputs)], &batch[2+(i*inputs)], &batch[3+(i*inputs)]); } float* batchptr = batch; - // for (int i = 0; i < datalen; i++) { - // printf("%f (vs %f) at %x\n", batchptr[i], batch[i], batchptr); - // } layers.emplace_back(batchptr, batch_sz, inputs); - // for (int i = 0; i < hidden; i++) { - // layers.emplace_back(neurons); - // } - // layers.emplace_back(outputs); + for (int i = 0; i < hidden; i++) { + layers.emplace_back(batch_sz, neurons); + } + layers.emplace_back(batch_sz, outputs); + for (int i = 0; i < hidden+1; i++) { + layers[i].initWeights(layers[i+1]); + } +} + +void Network::activate(Eigen::MatrixXd matrix) +{ + int nodes = matrix.cols(); + for (int i = 0; i < (matrix.rows()*matrix.cols()); i++) { + // (matrix)((int)i / nodes, i%nodes) = + } +} + +void Network::feedforward() +{ + for (int i = 0; i < length-1; i++) { + std::cout << "-----------\nRUN\n\n" << *layers[i].contents << "\n-\n\n"; + Eigen::MatrixXd product = (*layers[i].contents) * (*layers[i].weights); + activate(product); + std::cout << "NEXT\n\n\n" << *layers[i+1].contents << "\n-\n\n"; + layers[i+1].contents = &product; + std::cout << "UPDATE\n\n\n" << *layers[i+1].contents << "\n\n"; + } } int main() { Network net ("./data_banknote_authentication.txt", 4, 2, 2, 5, 10); + net.feedforward(); }