Files
cenet/include/nn.hpp
T

135 lines
4.4 KiB
C++

#ifndef NN_HPP
#define NN_HPP
#include "mat.hpp"
#include <array>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
template <std::size_t N, std::size_t... Layers>
class nn {
private:
template <std::size_t In, std::size_t Out>
struct layer {
static constexpr std::size_t input_count = In;
static constexpr std::size_t output_count = Out;
mat<In, Out> weights;
mat<1, Out> biases;
mat<1, Out> activations;
};
private:
static auto make_output() {
constexpr std::array<std::size_t, sizeof...(Layers) + 1> dims = { N, Layers... };
return mat<1, std::get<sizeof...(Layers)>(dims)>{};
}
public:
using output_type = decltype(make_output());
using input_type = mat<1, N>;
public:
const output_type& get_output() const { return std::get<sizeof...(Layers) - 1>(m_layers).activations; }
public:
template <typename Dist>
void fill_random(const Dist& dist) {
fill_random(dist, std::make_index_sequence<sizeof...(Layers)>{});
}
private:
template <typename Dist, std::size_t... I>
void fill_random(const Dist& dist, std::index_sequence<I...>) {
(fill_random<I>(dist), ...);
}
template <std::size_t I, typename Dist>
void fill_random(const Dist& dist) {
auto& layer = std::get<I>(m_layers);
layer.weights = decltype(layer.weights)::random(dist);
layer.biases = decltype(layer.biases)::random(dist);
}
public:
const output_type& feed(const input_type& input) {
feed(input, std::make_index_sequence<sizeof...(Layers)>{});
return get_output();
}
private:
template <std::size_t... I>
void feed(const input_type& input, std::index_sequence<I...>) {
((feed<I>(input)), ...);
}
template <std::size_t I>
void feed(const input_type& input) {
static constexpr auto input_count = std::tuple_element_t<I, layers_type>::input_count;
static constexpr auto output_count = std::tuple_element_t<I, layers_type>::output_count;
auto& layer = std::get<I>(m_layers);
mat<1, input_count> inputs;
if constexpr (I == 0) {
inputs = input;
} else {
inputs = std::get<I - 1>(m_layers).activations;
}
layer.activations = (inputs * layer.weights) + layer.biases;
for (std::size_t i = 0; i < output_count; ++i) {
layer.activations[0][i] = sigmoid(layer.activations[0][i]);
}
}
public:
void train(const input_type& input, const output_type& expected, double learningRate) {
feed(input);
auto outputErr = (get_output() - expected).hadamard(get_output()).hadamard(1.0 - get_output());
train_chain<sizeof...(Layers) - 1>(input, learningRate, outputErr);
}
private:
template <std::size_t I, typename ErrorType>
void train_chain(const input_type& input, double learningRate, ErrorType error) {
auto next = train<I>(input, learningRate, error);
if constexpr (I > 0) {
train_chain<I - 1>(input, learningRate, next);
}
}
template <std::size_t I, typename ErrorType>
auto train(const input_type& input, double learningRate, ErrorType prevErr) {
auto& layer = std::get<I>(m_layers);
const auto& layerInput = get_layer_input<I>(input);
layer.weights -= learningRate * (layerInput.transpose() * prevErr);
layer.biases -= learningRate * prevErr;
auto backpropErr = prevErr * layer.weights.transpose();
if constexpr (I > 0) {
return backpropErr.hadamard(layerInput.hadamard(1.0 - layerInput));
} else {
return backpropErr;
}
}
private:
template <std::size_t I>
const auto& get_layer_input(const input_type& input) {
if constexpr (I == 0) {
return input;
} else {
return std::get<I - 1>(m_layers).activations;
}
}
private:
template <std::size_t... I>
static auto make_layers_tuple(std::index_sequence<I...>) {
constexpr std::array<std::size_t, sizeof...(Layers) + 1> dims = { N, Layers... };
return std::tuple<layer<std::get<I>(dims), std::get<I + 1>(dims)>...>{};
}
private:
static double sigmoid(double input) { return 1 / (1 + std::exp(-input)); }
private:
using layers_type = decltype(make_layers_tuple(std::make_index_sequence<sizeof...(Layers)>{}));
layers_type m_layers;
};
#endif // NN_HPP