A singular Riemannian geometry approach to Deep Neural Networks.
layer.h
1 /******************************************************
2 *
3 * GENERIC LAYER CLASS HEADER
4 *
5 ******************************************************/
6 
12 #ifndef LAYER_H
13 #define LAYER_H
14 
15 //STD libs
16 #include<vector>
17 
18 //OpenMP libs
19 #include<omp.h>
20 
21 //Eigen
22 #include"Eigen/Dense"
23 
24 //NeuranNet headers
25 #include"activation_functions.h"
26 
27 /******************************************************
28 * Generic layer class
29 ******************************************************/
30 class layer
31 {
32  public:
33  //Ctors
35  layer(){ };
37  layer(const std::vector<std::vector<double>> &weights_,const std::vector<double> &biases_);
39  layer(const std::vector<std::vector<double>> &weights_);
41  layer(const Eigen::MatrixXd &weights_);
43  layer(const Eigen::MatrixXd &weights_,const std::vector<double> &biases_);
45  layer(const Eigen::MatrixXd &weights_,const Eigen::VectorXd &biases_);
47  layer(const Eigen::MatrixXd &weights_,const Eigen::VectorXd &biases_, int in_dim, int out_dim);
48 
49  //Dtor
51  virtual ~layer(){ };
52 
53  //Compute the output of the layer given an input
55  virtual Eigen::VectorXd predict(const Eigen::VectorXd & input) {return input;};
57  virtual std::vector<Eigen::VectorXd> predict_batch(const std::vector<Eigen::VectorXd> & input) {return input;};
58 
59  //Functions to get biases and weights from the network
61  const Eigen::VectorXd get_biases();
63  const Eigen::MatrixXd get_weights();
65  const Eigen::MatrixXd get_weights_biases_as_mat();
68  const Eigen::MatrixXd get_weights_biases_as_vec_col_maj();
71  const Eigen::MatrixXd get_weights_biases_as_vec_row_maj();
73  double get_weight(int i, int j);
75  double get_bias(int i);
76 
77  //Set biases and weights (supposing they are alread
79  void set_biases(const Eigen::VectorXd & biases_);
81  void set_biases(const std::vector<double> & biases_);
83  void set_weights(const Eigen::MatrixXd & weights_);
85  void set_weight(int i, int j, double weight_);
87  void set_bias(int i,double bias_);
89  void set_weights_biases_compact(Eigen::MatrixXd &source);
92  void set_weights_biases(Eigen::MatrixXd &source);
95  void set_weights_biases_row_maj(Eigen::MatrixXd &source);
96 
97  //Get info about the layer
99  int get_num_nodes();
101  int get_input_size();
103  int get_weights_rows();
105  int get_weights_cols();
107  std::string get_type();
108 
109  //Compute Jacobians w.r.t. weights/biases or inputs
111  virtual Eigen::MatrixXd compute_partial_derivatives_wrt_inputs(Eigen::VectorXd &input) = 0;
113  virtual Eigen::MatrixXd compute_partial_derivatives_wrt_weights_biases(Eigen::VectorXd &input) = 0;
114 
115  //Utils
117  void transpose_weights();
118 
119  //Set the parameters to approximate x -> exp(-x) and x-> ln(1+exp(x)) for large values of the argument
120  //See the description of the variables exp_max_cut and exp_zero_approx
125  void set_exp_max_cut(double exp_max_cut_);
130  void set_exp_zero_approx(double exp_zero_approx_);
131 
132  //Get the parameters to approximate x -> exp(-x) and x->ln(1+exp(x)) for large arguments
134  double get_exp_max_cut();
136  double get_exp_zero_approx();
137 
138  protected:
139  //Biases and weights of the neural network
140  Eigen::VectorXd biases;
141  Eigen::MatrixXd weights;
142  //Dimensions of the input/output spaces and of the weights matrix
143  int weights_rows = 0;
144  int input_dim = 0;
145  int output_dim = 0;
146  int weights_cols = 0;
147  //Type of layer
148  std::string type;
149  //Parameters to approximate x -> exp(-x) and x-> ln(1+exp(x)) large arguments
150  double exp_max_cut = 50.;
151  double exp_zero_approx = 1.92874984e-22;
152 
153 };
154 
155 #endif /* layer_H */
156 
layer::get_weights_biases_as_vec_col_maj
const Eigen::MatrixXd get_weights_biases_as_vec_col_maj()
Definition: layer.cpp:122
layer::layer
layer()
Definition: layer.h:41
layer::get_bias
double get_bias(int i)
Definition: layer.cpp:99
layer::get_weights
const Eigen::MatrixXd get_weights()
Definition: layer.cpp:89
layer::get_input_size
int get_input_size()
Definition: layer.cpp:238
layer::set_bias
void set_bias(int i, double bias_)
Definition: layer.cpp:184
layer::predict_batch
virtual std::vector< Eigen::VectorXd > predict_batch(const std::vector< Eigen::VectorXd > &input)
Definition: layer.h:63
layer::set_weights_biases
void set_weights_biases(Eigen::MatrixXd &source)
Definition: layer.cpp:189
layer::compute_partial_derivatives_wrt_weights_biases
virtual Eigen::MatrixXd compute_partial_derivatives_wrt_weights_biases(Eigen::VectorXd &input)=0
layer::predict
virtual Eigen::VectorXd predict(const Eigen::VectorXd &input)
Definition: layer.h:61
layer::get_weights_biases_as_mat
const Eigen::MatrixXd get_weights_biases_as_mat()
Definition: layer.cpp:104
layer::set_weights_biases_row_maj
void set_weights_biases_row_maj(Eigen::MatrixXd &source)
Definition: layer.cpp:204
layer::compute_partial_derivatives_wrt_inputs
virtual Eigen::MatrixXd compute_partial_derivatives_wrt_inputs(Eigen::VectorXd &input)=0
layer::set_weights_biases_compact
void set_weights_biases_compact(Eigen::MatrixXd &source)
Definition: layer.cpp:219
layer::get_weights_cols
int get_weights_cols()
Definition: layer.cpp:248
layer::set_exp_max_cut
void set_exp_max_cut(double exp_max_cut_)
Definition: layer.cpp:275
layer
Generic layer class.
Definition: layer.h:30
layer::get_weights_rows
int get_weights_rows()
Definition: layer.cpp:243
layer::get_num_nodes
int get_num_nodes()
Definition: layer.cpp:233
layer::get_weights_biases_as_vec_row_maj
const Eigen::MatrixXd get_weights_biases_as_vec_row_maj()
Definition: layer.cpp:140
layer::get_biases
const Eigen::VectorXd get_biases()
Definition: layer.cpp:84
layer::get_exp_max_cut
double get_exp_max_cut()
Definition: layer.cpp:285
layer::set_weight
void set_weight(int i, int j, double weight_)
Definition: layer.cpp:179
layer::get_weight
double get_weight(int i, int j)
Definition: layer.cpp:94
layer::set_exp_zero_approx
void set_exp_zero_approx(double exp_zero_approx_)
Definition: layer.cpp:280
layer::transpose_weights
void transpose_weights()
Definition: layer.cpp:258
layer::~layer
virtual ~layer()
Definition: layer.h:57
layer::get_exp_zero_approx
double get_exp_zero_approx()
Definition: layer.cpp:290
layer::get_type
std::string get_type()
Definition: layer.cpp:253
layer::set_biases
void set_biases(const Eigen::VectorXd &biases_)
Definition: layer.cpp:169
layer::set_weights
void set_weights(const Eigen::MatrixXd &weights_)
Definition: layer.cpp:174