bpnn.hpp (3458B)
1 #ifndef BPNN_H 2 #define BPNN_H 3 4 #include <Eigen/Dense> 5 6 #include <vector> 7 #include <iostream> 8 #include <string> 9 #include <cstdio> 10 #include <cmath> 11 #include <random> 12 #include <sys/types.h> 13 #include <fcntl.h> 14 #include <unistd.h> 15 16 namespace Jacobian { 17 #define BUFFER_SIZE 600*1024 18 #define LARGE_BUF 600*1024*15 19 enum class Regularization {L1, L2}; 20 21 class Layer { 22 public: 23 Eigen::MatrixXf contents; 24 Eigen::MatrixXf weights; 25 Eigen::MatrixXf bias; 26 Eigen::MatrixXf dZ; 27 Eigen::MatrixXf v; 28 Eigen::MatrixXf m; 29 std::function<float(float)> activation; 30 std::function<float(float)> activation_deriv; 31 32 Layer(int rows, int columns); 33 Layer(float* vals, int rows, int columns); 34 void operator=(const Layer& that); 35 void init_weights(Layer next); 36 37 }; 38 39 class Network { 40 char buf[BUFFER_SIZE]; 41 char* p; 42 protected: 43 int instances; 44 float epoch_acc; 45 float epoch_cost; 46 float val_acc; 47 float val_cost; 48 std::function<void(float&)> decay; 49 std::function<void(std::vector<Eigen::MatrixXf>, int, int)> grad_calc; 50 std::function<void(Layer&, Eigen::MatrixXf, float)> update; 51 void next_batch(int fd); 52 public: 53 int data; 54 int val_data; 55 int val_instances; 56 int test_instances; 57 std::vector<Layer> layers; 58 int length = 0; 59 int batch_size; 60 float learning_rate; 61 float bias_lr; 62 Regularization reg_type; 63 float lambda; 64 bool early_stop; 65 float threshold; 66 bool silenced = false; 67 int epochs = 0; 68 int batches = 0; 69 Eigen::MatrixXf* labels; 70 71 Network(const char* path, int batch_sz, float learn_rate, 72 float bias_rate, Regularization regularization, 73 float l, float ratio, bool early_exit=true, float cutoff=0); 74 ~Network(); 75 void add_layer(int nodes, std::function<float(float)> activation, std::function<float(float)> activation_deriv); 76 void initialize(); 77 void init_optimizer(std::function<void(Layer &, Eigen::MatrixXf, float)> f) 78 { 79 update = f; 80 }; 81 void init_decay(std::function<void(float&)> f); 82 void set_activation(int index, std::function<float(float)> custom, std::function<float(float)> custom_deriv); 83 void feedforward(); 84 void softmax(); 85 void list_net(); 86 void interactive_next_batch(); 87 float cost(); 88 float accuracy(); 89 Eigen::MatrixXf backpropagate(); 90 void validate(const char* path); 91 void train(); 92 float get_acc() {return epoch_acc;} 93 float get_val_acc() {return val_acc;} 94 float get_cost() {return epoch_cost;} 95 float get_val_cost() 96 { 97 return val_cost; 98 } 99 }; 100 101 int prep_file(const char *path, const char *out_path); 102 int split_file(const char *path, int lines, float ratio); 103 104 void prep(const char *rname, const char *wname); 105 void compress(const char *rname, const char *wname); 106 Eigen::MatrixXf l1_deriv(Eigen::MatrixXf m); 107 108 #define MAXLINE 1024 109 110 #if (!RECKLESS) 111 #define checknan(x, loc) \ 112 if (x == INFINITY || x == NAN || x == -INFINITY) \ 113 throw ValueError("Detected NaN in operation", loc) 114 #define Expects(cond) assert(cond); 115 #define Ensures(cond) assert(cond); 116 #else 117 #define checknan(x, loc) 118 #define Expects(cond) 119 #define Ensures(cond) 120 #endif 121 122 #define SHUFFLED_PATH "./shuffled.txt" 123 #define VAL_PATH "./test.txt" 124 #define TRAIN_PATH "./train.txt" 125 #define VAL_BIN_PATH "./test.bin" 126 #define TRAIN_BIN_PATH "./train.bin" 127 128 } 129 #endif /* MODULE_H */