jacobian

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

commit ea7d9e1c72c35308586bf7cf473d09412d02a2ec
parent 761debbb9ef1e9cc9c2c5f86b07a4f90ed9b204d
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Sun, 28 Mar 2021 20:28:58 -0700

Network interactivity aids and visualization in demo

Diffstat:
Msrc/bpnn.cpp | 12++++++++++++
Msrc/bpnn.hpp | 10++++++++++
Msrc/pybind.cpp | 17++++++++++++++---
3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/bpnn.cpp b/src/bpnn.cpp @@ -25,6 +25,7 @@ Layer::Layer(int batch_sz, int nodes) } } + void Layer::init_weights(Layer next) { v = new Eigen::MatrixXf (contents->cols(), next.contents->cols()); @@ -294,6 +295,17 @@ void Network::validate(const char* path) #include "optimizers.cpp" +void Network::interactive_next_batch() +{ + if (batches < instances/batch_size-batch_size) next_batch(data); + else { + batches = 0; + data = open(TRAIN_BIN_PATH, O_RDONLY | O_NONBLOCK); + decay(learning_rate); + } + batches++; +} + void Network::train() { float cost_sum = 0; diff --git a/src/bpnn.hpp b/src/bpnn.hpp @@ -15,6 +15,7 @@ #define BUFFER_SIZE 600*1024 #define LARGE_BUF 600*1024*15 +#define MATRIX_NULL Eigen::MatrixXf::Constant(0,1,1) enum Regularization {L1, L2}; class Layer { @@ -32,6 +33,14 @@ public: Layer(float* vals, int rows, int columns); void operator=(const Layer& that); void init_weights(Layer next); + + Eigen::MatrixXf get_contents() {return *contents;} + Eigen::MatrixXf get_weights() {if (weights == nullptr) return MATRIX_NULL; else return *weights;} + Eigen::MatrixXf get_bias() {return *bias;} + Eigen::MatrixXf get_dZ() {return *v;} + Eigen::MatrixXf get_v() {if (weights == nullptr) return MATRIX_NULL; else return *v;} + Eigen::MatrixXf get_m() {if (weights == nullptr) return MATRIX_NULL; else return *m;} + }; class Network { @@ -78,6 +87,7 @@ public: void feedforward(); void softmax(); void list_net(); + void interactive_next_batch(); float cost(); float accuracy(); Eigen::MatrixXf backpropagate(); diff --git a/src/pybind.cpp b/src/pybind.cpp @@ -6,6 +6,7 @@ // #include <pybind11/pybind11.h> +#include <pybind11/stl.h> #include <pybind11/functional.h> #include <pybind11/eigen.h> @@ -21,7 +22,15 @@ PYBIND11_MODULE(_jacobian, m) .value("L2", Regularization::L2) .export_values(); py::class_<Layer>(m, "Layer") - .def(py::init<int, int>()); + .def(py::init<int, int>()) + .def("get_contents", &Layer::get_contents) + .def("get_weights", &Layer::get_weights) + .def("get_bias", &Layer::get_bias) + .def("get_v", &Layer::get_v) + .def("get_m", &Layer::get_m) + .def("get_dZ", &Layer::get_dZ) + .def_readonly("activation", &Layer::activation) + .def_readonly("activation_deriv", &Layer::activation); py::class_<Network>(m, "Network") .def(py::init<char *, int, float, float, Regularization, float, float, bool, float>(), @@ -40,13 +49,15 @@ PYBIND11_MODULE(_jacobian, m) .def("feedforward", &Network::feedforward) .def("backpropagate", &Network::backpropagate) .def("list_net", &Network::list_net) + .def("next_batch", &Network::interactive_next_batch) .def("cost", &Network::cost) .def("accuracy", &Network::accuracy) .def("train", &Network::train) - .def("get_acc", &Network::get_acc) .def("get_cost", &Network::get_cost) + .def("get_acc", &Network::get_acc) + .def("get_val_cost", &Network::get_val_cost) .def("get_val_acc", &Network::get_val_acc) - .def("get_val_cost", &Network::get_val_cost); + .def_readonly("layers", &Network::layers); m.def("linear", &linear, py::arg("x")); m.def("linear_deriv", &linear_deriv, py::arg("x")); m.def("sigmoid", &sigmoid, py::arg("x"));