refactor(network): separate optimizer state
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include "./layer_fwd.hpp"
|
||||
#include "./layer_traits.hpp"
|
||||
#include "./retained.hpp"
|
||||
#include "cenet/optim/adam.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
@@ -77,14 +78,6 @@ struct dense final : public layer_tag {
|
||||
weights_type weights;
|
||||
biases_type biases;
|
||||
|
||||
// TODO: move Adam into optimizer state
|
||||
weights_type momentumWeights;
|
||||
biases_type momentumBiases;
|
||||
weights_type varianceWeights;
|
||||
biases_type varianceBiases;
|
||||
|
||||
std::size_t time = 0;
|
||||
|
||||
void reset() {
|
||||
weights.view().fill();
|
||||
biases.view().fill();
|
||||
@@ -97,8 +90,51 @@ struct dense final : public layer_tag {
|
||||
|
||||
return std::forward<Error>(prevErr).matmul(data.weights.transpose());
|
||||
}
|
||||
};
|
||||
|
||||
void apply(data<N, I>& data, double divisor, double rate) {
|
||||
template <class N, std::size_t I, class Optim>
|
||||
struct optim {
|
||||
using traits = typename detail::layer_traits<network_layer<dense, N, I>>;
|
||||
|
||||
using input_type = typename traits::input_type;
|
||||
using value_type = typename input_type::value_type;
|
||||
using output_type = math::matrix<value_type, 1, Features>;
|
||||
|
||||
static constexpr auto input_cols = input_type::shape_type::template sget<1>();
|
||||
|
||||
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
|
||||
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
|
||||
|
||||
weights_type weigths;
|
||||
biases_type biases;
|
||||
|
||||
void apply(data<N, I>& data, grad<N, I>& grad, double divisor, double rate) {
|
||||
data.weights -= (grad.weights / divisor) * rate;
|
||||
data.biases -= (grad.biases / divisor) * rate;
|
||||
}
|
||||
};
|
||||
|
||||
template <class N, std::size_t I>
|
||||
struct optim<N, I, cenet::optim::adam> {
|
||||
using traits = typename detail::layer_traits<network_layer<dense, N, I>>;
|
||||
|
||||
using input_type = typename traits::input_type;
|
||||
using value_type = typename input_type::value_type;
|
||||
using output_type = math::matrix<value_type, 1, Features>;
|
||||
|
||||
static constexpr auto input_cols = input_type::shape_type::template sget<1>();
|
||||
|
||||
using weights_type = math::matrix<math::inline_storage<value_type>, input_cols, Features>;
|
||||
using biases_type = math::matrix<math::inline_storage<value_type>, 1, Features>;
|
||||
|
||||
std::size_t time = 0;
|
||||
|
||||
weights_type momentumWeights;
|
||||
biases_type momentumBiases;
|
||||
weights_type varianceWeights;
|
||||
biases_type varianceBiases;
|
||||
|
||||
void apply(data<N, I>& data, grad<N, I>& grad, double divisor, double rate) {
|
||||
++time;
|
||||
|
||||
constexpr double beta1 = 0.9;
|
||||
@@ -107,7 +143,7 @@ struct dense final : public layer_tag {
|
||||
|
||||
for (std::size_t row = 0; row < input_cols; ++row) {
|
||||
for (std::size_t col = 0; col < Features; ++col) {
|
||||
auto avgGrad = weights[row][col] / divisor;
|
||||
auto avgGrad = grad.weights[row][col] / divisor;
|
||||
|
||||
momentumWeights[row][col] = (beta1 * momentumWeights[row][col]) + ((1.0 - beta1) * avgGrad);
|
||||
varianceWeights[row][col] =
|
||||
@@ -121,7 +157,7 @@ struct dense final : public layer_tag {
|
||||
}
|
||||
|
||||
for (std::size_t col = 0; col < Features; ++col) {
|
||||
double avgGrad = biases[0][col] / divisor;
|
||||
double avgGrad = grad.biases[0][col] / divisor;
|
||||
|
||||
momentumBiases[0][col] = (beta1 * momentumBiases[0][col]) + ((1.0 - beta1) * avgGrad);
|
||||
varianceBiases[0][col] = (beta2 * varianceBiases[0][col]) + ((1.0 - beta2) * (avgGrad * avgGrad));
|
||||
@@ -172,8 +208,11 @@ struct sigmoid final : public layer_tag {
|
||||
return std::forward<Error>(prevErr) * retained *
|
||||
(static_cast<typename output_type::value_type>(1) - retained);
|
||||
}
|
||||
};
|
||||
|
||||
void apply(data<N, I>&, double, double) {}
|
||||
template <class N, std::size_t I, class Optim>
|
||||
struct optim {
|
||||
void apply(data<N, I>&, grad<N, I>&, double, double) {}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -213,8 +252,11 @@ struct relu final : public layer_tag {
|
||||
return std::forward<Error>(prevErr).apply(retained,
|
||||
[](auto in, auto check) { return check > 0.0 ? in : 0.0; });
|
||||
}
|
||||
};
|
||||
|
||||
void apply(data<N, I>&, double, double) {}
|
||||
template <class N, std::size_t I, class Optim>
|
||||
struct optim {
|
||||
void apply(data<N, I>&, grad<N, I>&, double, double) {}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user