30 Dense(
int n_input,
int n_output,
40 std::random_device rd;
41 std::mt19937 gen(rd());
42 std::uniform_real_distribution<double> dis(-1, 1);
44 _weights = Eigen::MatrixXd::Zero(n_input, n_output).unaryExpr([&](
double)
45 {
return .1 * dis(gen); });
46 _biases = Eigen::MatrixXd::Zero(1, n_output);
48 _weights_optimizer = Eigen::MatrixXd::Zero(n_input, n_output);
50 _biases_optimizer = Eigen::MatrixXd::Zero(1, n_output);
52 _weights_optimizer_additional = Eigen::MatrixXd::Zero(n_input, n_output);
54 _biases_optimizer_additional = Eigen::MatrixXd::Zero(1, n_output);
63 void forward(Eigen::MatrixXd &out,
const Eigen::MatrixXd &x)
67 for (
int row = 0; row < out.rows(); ++row)
69 out.row(row).array() += _biases.array();
79 void backward(Eigen::MatrixXd &out,
const Eigen::MatrixXd &dx)
81 _dweights = _forward_input.transpose() * dx;
82 _dbiases = dx.colwise().sum().array();
86 if (_l1_weights_regularizer > 0)
88 Eigen::MatrixXd dL1 = Eigen::MatrixXd::Ones(_weights.rows(), _weights.cols());
89 dL1 = (_weights.array() < 0).select(-1, dL1);
90 _dweights += _l1_weights_regularizer * dL1;
93 if (_l2_weights_regularizer > 0)
95 _dweights += 2 * _l2_weights_regularizer * _weights;
98 if (_l1_biases_regularizer > 0)
100 Eigen::MatrixXd dL1 = Eigen::MatrixXd::Ones(_biases.rows(), _biases.cols());
101 dL1 = (_biases.array() < 0).select(-1, dL1);
102 _dbiases += _l1_biases_regularizer * dL1;
105 if (_l2_biases_regularizer > 0)
107 _dbiases += 2 * _l2_biases_regularizer * _biases;
110 out = dx * _weights.transpose();
164 if (_weights.rows() !=
weights.rows() || _weights.cols() !=
weights.cols())
166 LOG_ERROR(
"Shape of new matrix does not match to initial's matrix shape.");
167 throw std::invalid_argument(
"Shape of new matrix does not match to initial's matrix shape.");
184 if (_biases.rows() !=
biases.rows() || _biases.cols() !=
biases.cols())
186 LOG_ERROR(
"Shape of new matrix does not match to initial's matrix shape.");
187 throw std::invalid_argument(
"Shape of new matrix does not match to initial's matrix shape.");
199 _weights_optimizer = woptimizer;
209 _biases_optimizer = boptimizer;
219 return _weights_optimizer;
229 return _biases_optimizer;
239 _weights_optimizer_additional = woptimizer;
249 _biases_optimizer_additional = boptimizer;
259 return _weights_optimizer_additional;
269 return _biases_optimizer_additional;
279 return _l1_weights_regularizer;
289 return _l2_weights_regularizer;
299 return _l1_biases_regularizer;
309 return _l2_biases_regularizer;
319 return _n_input * _n_output + _n_output;
328 void shape(
int &n_input,
int &n_output)
const
331 n_output = _n_output;
338 Eigen::MatrixXd _weights;
339 Eigen::MatrixXd _biases;
341 Eigen::MatrixXd _dweights;
342 Eigen::MatrixXd _dbiases;
344 Eigen::MatrixXd _weights_optimizer;
345 Eigen::MatrixXd _biases_optimizer;
347 Eigen::MatrixXd _weights_optimizer_additional;
348 Eigen::MatrixXd _biases_optimizer_additional;
350 double _l1_weights_regularizer;
351 double _l1_biases_regularizer;
352 double _l2_weights_regularizer;
353 double _l2_biases_regularizer;
355 Eigen::MatrixXd _forward_input;
Dense layer.
Definition Dense.hpp:16
void biases(Eigen::MatrixXd &biases)
Set's the biases of the dense layer.
Definition Dense.hpp:182
const Eigen::MatrixXd & weights_optimizer_additional() const
Get additional weights optimizer matrix.
Definition Dense.hpp:257
const Eigen::MatrixXd & weights() const
Get weights.
Definition Dense.hpp:118
const Eigen::MatrixXd & biases_optimizer() const
Get biases optimizer matrix.
Definition Dense.hpp:227
const Eigen::MatrixXd & dweights() const
Get weights gradients.
Definition Dense.hpp:138
const double & l1_weights_regularizer() const
Get L1 weights regularizer.
Definition Dense.hpp:277
const Eigen::MatrixXd & dbiases() const
Get biases gradients.
Definition Dense.hpp:148
const double & l2_biases_regularizer() const
Get L2 biases regularizer.
Definition Dense.hpp:307
const Eigen::MatrixXd & biases_optimizer_additional() const
Get additional biases optimizer matrix.
Definition Dense.hpp:267
const Eigen::MatrixXd & biases() const
Get biases.
Definition Dense.hpp:128
int parameters() const
Calculates the number of trainable of the dense layer.
Definition Dense.hpp:317
const Eigen::MatrixXd & weights_optimizer() const
Get weights optimizer matrix.
Definition Dense.hpp:217
void shape(int &n_input, int &n_output) const
Gives the shape of the dense layer.
Definition Dense.hpp:328
void biases_optimizer_additional(Eigen::MatrixXd boptimizer)
Set's the additional biases optimizer matrix of the dense layer.
Definition Dense.hpp:247
void forward(Eigen::MatrixXd &out, const Eigen::MatrixXd &x)
Forward pass of the dense layer.
Definition Dense.hpp:63
void weights_optimizer_additional(Eigen::MatrixXd woptimizer)
Set's the additional weights optimizer matrix of the dense layer.
Definition Dense.hpp:237
const double & l2_weights_regularizer() const
Get L2 weights regularizer.
Definition Dense.hpp:287
void biases_optimizer(Eigen::MatrixXd boptimizer)
Set's the biases optimizer matrix of the dense layer.
Definition Dense.hpp:207
void weights_optimizer(Eigen::MatrixXd woptimizer)
Set's the weights optimizer matrix of the dense layer.
Definition Dense.hpp:197
const double & l1_biases_regularizer() const
Get L1 biases regularizer.
Definition Dense.hpp:297
void backward(Eigen::MatrixXd &out, const Eigen::MatrixXd &dx)
Backward pass of the dense layer.
Definition Dense.hpp:79
void weights(Eigen::MatrixXd &weights)
Set's the weights of the dense layer.
Definition Dense.hpp:162
Dense(int n_input, int n_output, double l1_weights_regularizer=.0, double l1_biases_regularizer=.0, double l2_weights_regularizer=.0, double l2_biases_regularizer=.0)
Construct a new Dense object.
Definition Dense.hpp:30
Base class for all layers.
Definition Layer.hpp:23
#define LOG_ERROR
Definition clue.hpp:138
Definition Activation.hpp:6
LayerType
Enum class for layer types.
Definition Layer.hpp:12