feat(nn): introduce layer traits
This commit is contained in:
@@ -5,22 +5,27 @@
|
||||
#ifndef CENET_LAYERS_DENSE_HPP
|
||||
#define CENET_LAYERS_DENSE_HPP
|
||||
|
||||
#include "../layer.hpp"
|
||||
#include "../math/tensor_fwd.hpp"
|
||||
#include "../network.hpp"
|
||||
#include "./layer_fwd.hpp"
|
||||
#include "./layer_traits.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace cenet {
|
||||
namespace layers {
|
||||
|
||||
// TODO: redesign the layer interface after porting the old layers
|
||||
template <std::size_t Features>
|
||||
class dense final : public layer_info {
|
||||
class dense final : public layer_tag {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class ::cenet::network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
template <class N, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
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>;
|
||||
|
||||
@@ -97,9 +102,10 @@ private:
|
||||
for (std::size_t col = 0; col < Features; ++col) {
|
||||
auto avgGrad = grad.weights[row][col] / divisor;
|
||||
|
||||
grad.momentumWeights[row][col] = beta1 * grad.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
||||
grad.momentumWeights[row][col] =
|
||||
(beta1 * grad.momentumWeights[row][col]) + ((1.0 - beta1) * avgGrad);
|
||||
grad.varianceWeights[row][col] =
|
||||
beta2 * grad.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||
(beta2 * grad.varianceWeights[row][col]) + ((1.0 - beta2) * (avgGrad * avgGrad));
|
||||
|
||||
auto momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
||||
auto varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
||||
@@ -111,8 +117,9 @@ private:
|
||||
for (std::size_t col = 0; col < Features; ++col) {
|
||||
double avgGrad = grad.biases[0][col] / divisor;
|
||||
|
||||
grad.momentumBiases[0][col] = beta1 * grad.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
||||
grad.varianceBiases[0][col] = beta2 * grad.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||
grad.momentumBiases[0][col] = (beta1 * grad.momentumBiases[0][col]) + ((1.0 - beta1) * avgGrad);
|
||||
grad.varianceBiases[0][col] =
|
||||
(beta2 * grad.varianceBiases[0][col]) + ((1.0 - beta2) * (avgGrad * avgGrad));
|
||||
|
||||
auto momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
||||
auto varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
||||
@@ -123,13 +130,15 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
class sigmoid final : public layer_info {
|
||||
class sigmoid final : public layer_tag {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class ::cenet::network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
template <class N, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
using traits = typename detail::layer_traits<network_layer<sigmoid, N, I>>;
|
||||
|
||||
using input_type = typename traits::input_type;
|
||||
using output_type = input_type;
|
||||
|
||||
typename output_type::template with_storage<math::inline_storage_tag> retained;
|
||||
@@ -160,13 +169,15 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
class relu final : public layer_info {
|
||||
class relu final : public layer_tag {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class ::cenet::network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
template <class N, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
using traits = typename detail::layer_traits<network_layer<relu, N, I>>;
|
||||
|
||||
using input_type = typename traits::input_type;
|
||||
using output_type = input_type;
|
||||
|
||||
typename input_type::template with_storage<math::inline_storage_tag> retained;
|
||||
|
||||
Reference in New Issue
Block a user