cnn.hpp (1649B)
1 #ifndef CNN_H 2 #define CNN_H 3 4 #include <fstream> 5 6 class ConvLayer 7 { 8 public: 9 int stride_len; 10 int padding; 11 Eigen::MatrixXf* input; 12 Eigen::MatrixXf* kernel; 13 Eigen::MatrixXf* output; 14 Eigen::MatrixXf* dZ; 15 std::function<float(float)> activation; 16 std::function<float(float)> activation_deriv; 17 float bias; 18 19 ConvLayer(int x, int y, int stride, int kern_x, int kern_y, int pad, std::function<float(float)> activ, std::function<float(float)> activ_deriv); 20 void convolute(); 21 void set_input(Eigen::MatrixXf* matrix); 22 }; 23 24 class PoolingLayer 25 { 26 public: 27 int stride_len; 28 int padding; 29 Eigen::MatrixXf* input; 30 Eigen::MatrixXf* kernel; 31 Eigen::MatrixXf* output; 32 33 void pool(); 34 PoolingLayer(int x, int y, int stride, int kern_x, int kern_y, int pad); 35 }; 36 37 class ConvNet : public Network 38 { 39 public: 40 int preprocess_length; 41 std::vector<std::vector<double>> data; 42 unsigned char* data_labels; 43 44 std::vector<ConvLayer> conv_layers; 45 std::vector<PoolingLayer> pool_layers; 46 47 ConvNet(const char* path, float learn_rate, float bias_rate, Regularization reg, float l, float ratio); 48 void list_net(); 49 void process(); // Runs the convolutional and pooling layers. 50 void next_batch(); 51 void backpropagate(); 52 void train(); 53 void add_conv_layer(int x, int y, int stride, int kern_x, int kern_y, int pad, std::function<float(float)> activ, std::function<float(float)> activ_deriv); 54 void add_pool_layer(int x, int y, int stride, int kern_x, int kern_y, int pad); 55 void set_label(Eigen::MatrixXf newlabels); 56 void initialize(); 57 }; 58 #endif /* MODULE_H */