jacobian

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

commit eb0c8873135d1ce14b1096d371ff52d196188f52
parent bfbdc5aa33576145d4e7e47e154339bab4589c5f
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Sun, 11 Oct 2020 20:23:52 -0700

Tweaks

Diffstat:
Msrc/bpnn.cpp | 20++++++++------------
Msrc/bpnn.hpp | 40++++++++++++++--------------------------
2 files changed, 22 insertions(+), 38 deletions(-)

diff --git a/src/bpnn.cpp b/src/bpnn.cpp @@ -93,6 +93,11 @@ Network::Network(char* path, int batch_sz, float learn_rate, float bias_rate, Re Ensures(batch_size < instances && data > 0 && val_data > 0); } +Network::~Network() +{ + close(data); + close(val_data); +} void Network::init_decay(char* type, ...) { @@ -104,22 +109,19 @@ void Network::init_decay(char* type, ...) decay = [this, a_0, k]() -> void { learning_rate = a_0 * learning_rate/k; }; - } - else if (strcmp(type, "exp") == 0) { + } else if (strcmp(type, "exp") == 0) { float a_0 = va_arg(args, double); float k = va_arg(args, double); decay = [this, a_0, k]() -> void { learning_rate = a_0 * exp(-k * epochs); }; - } - else if (strcmp(type, "frac") == 0) { + } else if (strcmp(type, "frac") == 0) { float a_0 = va_arg(args, double); float k = va_arg(args, double); decay = [this, a_0, k]() -> void { learning_rate = a_0 / (1+(k * epochs)); }; - } - else if (strcmp(type, "linear") == 0) { + } else if (strcmp(type, "linear") == 0) { int max_ep = va_arg(args, double); decay = [this, max_ep]() -> void { learning_rate = 1 - epochs/max_ep; @@ -342,12 +344,6 @@ void Network::backpropagate() } } -void Network::update_layer(float* vals, int datalen, int index) -{ - Expects(datalen > 0); - for (int i = 0; i < datalen; i++) (*layers[index].contents)(static_cast<int>(i / layers[index].contents->cols()), i%layers[index].contents->cols()) = vals[i]; -} - #include "data.cpp" float Network::validate(char* path) diff --git a/src/bpnn.hpp b/src/bpnn.hpp @@ -44,66 +44,54 @@ public: }; class Network { -public: - int data; - int val_data; int instances; char buf[BUFFER_SIZE]; char* p; - int val_instances; - int test_instances; - Eigen::MatrixXf numerical_grad(int i, float epsilon); - void update_layer(float* vals, int datalen, int index); - - std::vector<Layer> layers; - int length = 0; - float epoch_acc; float epoch_cost; float val_acc; float val_cost; - + std::function<void(void)> decay; + std::function<void(std::vector<Eigen::MatrixXf>, int, int)> grad_calc; + std::function<void(std::vector<Eigen::MatrixXf>, int)> update; +public: + int data; + int val_data; + int val_instances; + int test_instances; + std::vector<Layer> layers; + int length = 0; float learning_rate; float bias_lr; float lambda; + bool early_stop; + float threshold; Regularization reg_type; int batch_size; - bool silenced = false; int epochs = 0; int batches = 0; Eigen::MatrixXf* labels; - - std::function<void(void)> decay; - std::function<void(std::vector<Eigen::MatrixXf>, int, int)> grad_calc; - std::function<void(std::vector<Eigen::MatrixXf>, int)> update; - + Network(char* path, int batch_sz, float learn_rate, float bias_rate, Regularization regularization, float l, float ratio, bool early_exit=true, float cutoff=0); - + ~Network(); void add_layer(int nodes, char* name, std::function<float(float)> activation, std::function<float(float)> activation_deriv); void add_prelu_layer(int nodes, float a); void init_decay(char* type, ...); void init_optimizer(char* name, ...); void initialize(); - void grad_check(); void set_activation(int index, std::function<float(float)> custom, std::function<float(float)> custom_deriv); - void feedforward(); void softmax(); void list_net(); - - bool early_stop; - float threshold; - float cost(); float accuracy(); void backpropagate(); int next_batch(int fd); float validate(char* path); void train(); - float get_acc() {return epoch_acc;} float get_val_acc() {return val_acc;} float get_cost() {return epoch_cost;}