refactor: restructure the project
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#ifndef CENET_HPP
|
||||
#define CENET_HPP
|
||||
|
||||
#include "input.hpp"
|
||||
#include "layer.hpp"
|
||||
#include "network.hpp"
|
||||
|
||||
#endif // CENET_HPP
|
||||
@@ -0,0 +1,83 @@
|
||||
#ifndef CENET_INPUT_HPP
|
||||
#define CENET_INPUT_HPP
|
||||
|
||||
#include "mat.hpp"
|
||||
#include "tensor.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
|
||||
struct layer_input {
|
||||
static constexpr std::uint32_t DENSE_TAG = 0x444E5345;
|
||||
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
|
||||
};
|
||||
|
||||
template <std::size_t Features>
|
||||
class dense_input : public layer_input {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class network;
|
||||
|
||||
friend class layer_info;
|
||||
private:
|
||||
struct inner final {
|
||||
static constexpr std::uint32_t TAG = DENSE_TAG;
|
||||
|
||||
static constexpr std::size_t FEATURES = Features;
|
||||
|
||||
using type = mat<1, FEATURES>;
|
||||
|
||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||
inner(Args&&... args)
|
||||
: xs(std::forward<Args>(args)...) {}
|
||||
|
||||
operator type&() { return xs; }
|
||||
operator const type&() const { return xs; }
|
||||
|
||||
type& get() { return xs; }
|
||||
const type& get() const { return xs; }
|
||||
|
||||
type xs;
|
||||
};
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
class tensor_input : public layer_input {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class network;
|
||||
|
||||
friend class layer_info;
|
||||
private:
|
||||
struct inner final {
|
||||
static constexpr std::uint32_t TAG = TENSOR_TAG;
|
||||
|
||||
using type = tensor<double, Dims...>;
|
||||
|
||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||
inner(Args&&... args)
|
||||
: xs(std::forward<Args>(args)...) {}
|
||||
|
||||
operator type&() { return xs; }
|
||||
operator const type&() const { return xs; }
|
||||
|
||||
type& get() { return xs; }
|
||||
const type& get() const { return xs; }
|
||||
|
||||
type xs;
|
||||
};
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename = void>
|
||||
struct is_layer_input : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_layer_input<T, std::enable_if_t<std::is_base_of_v<layer_input, T>>> : std::true_type {};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_INPUT_HPP
|
||||
@@ -1,79 +1,15 @@
|
||||
#ifndef CENET_NN_HPP
|
||||
#define CENET_NN_HPP
|
||||
#ifndef CENET_LAYER_HPP
|
||||
#define CENET_LAYER_HPP
|
||||
|
||||
#include "mat.hpp"
|
||||
#include "tensor.hpp"
|
||||
#include "input.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace cenet {
|
||||
|
||||
struct layer_input {
|
||||
static constexpr std::uint32_t DENSE_TAG = 0x444E5345;
|
||||
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
|
||||
};
|
||||
|
||||
template <std::size_t Features>
|
||||
class dense_input : public layer_input {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
|
||||
friend class layer_info;
|
||||
private:
|
||||
struct inner final {
|
||||
static constexpr std::uint32_t TAG = DENSE_TAG;
|
||||
|
||||
static constexpr std::size_t FEATURES = Features;
|
||||
|
||||
using type = mat<1, FEATURES>;
|
||||
|
||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||
inner(Args&&... args)
|
||||
: xs(std::forward<Args>(args)...) {}
|
||||
|
||||
operator type&() { return xs; }
|
||||
operator const type&() const { return xs; }
|
||||
|
||||
type& get() { return xs; }
|
||||
const type& get() const { return xs; }
|
||||
|
||||
type xs;
|
||||
};
|
||||
};
|
||||
|
||||
template <std::size_t... Dims>
|
||||
class tensor_input : public layer_input {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
|
||||
friend class layer_info;
|
||||
private:
|
||||
struct inner final {
|
||||
static constexpr std::uint32_t TAG = TENSOR_TAG;
|
||||
|
||||
using type = tensor<double, Dims...>;
|
||||
|
||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||
inner(Args&&... args)
|
||||
: xs(std::forward<Args>(args)...) {}
|
||||
|
||||
operator type&() { return xs; }
|
||||
operator const type&() const { return xs; }
|
||||
|
||||
type& get() { return xs; }
|
||||
const type& get() const { return xs; }
|
||||
|
||||
type xs;
|
||||
};
|
||||
};
|
||||
|
||||
struct layer_info {
|
||||
protected:
|
||||
template <class NN, std::size_t I>
|
||||
@@ -93,7 +29,7 @@ protected:
|
||||
template <std::size_t Features>
|
||||
class dense_layer final : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -197,7 +133,7 @@ private:
|
||||
|
||||
class sigmoid_layer final : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -236,7 +172,7 @@ private:
|
||||
|
||||
class relu_layer final : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -275,7 +211,7 @@ private:
|
||||
template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0>
|
||||
class conv2d_layer final : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -453,7 +389,7 @@ private:
|
||||
template <std::size_t PoolSize = 2, std::size_t Stride = 2>
|
||||
class maxpool_layer : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -544,7 +480,7 @@ private:
|
||||
|
||||
class flatten_layer : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class nn;
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
@@ -587,140 +523,6 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
template <typename Input, typename... Layers>
|
||||
class nn {
|
||||
friend class layer_info;
|
||||
private:
|
||||
template <typename Seq>
|
||||
struct layer_types_gen {};
|
||||
|
||||
template <std::size_t... I>
|
||||
struct layer_types_gen<std::index_sequence<I...>> {
|
||||
using type = std::tuple<typename Layers::template inner<nn, I>...>;
|
||||
};
|
||||
|
||||
using layer_types = typename layer_types_gen<std::make_index_sequence<sizeof...(Layers)>>::type;
|
||||
using output_layer_type = std::tuple_element_t<sizeof...(Layers) - 1, layer_types>;
|
||||
|
||||
template <std::size_t I>
|
||||
using layer_input_t = typename std::tuple_element_t<I, layer_types>::input_type;
|
||||
private:
|
||||
template <std::size_t I>
|
||||
auto& get_layer() {
|
||||
return std::get<I>(m_layers);
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
const auto& get_layer() const {
|
||||
return std::get<I>(m_layers);
|
||||
}
|
||||
public:
|
||||
using input_type = Input::inner;
|
||||
using output_type = output_layer_type::output_type;
|
||||
public:
|
||||
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
|
||||
public:
|
||||
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
|
||||
(get_layer<I>().dump(os), ...);
|
||||
}
|
||||
public:
|
||||
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void load_network(std::istream& is, std::index_sequence<I...>) {
|
||||
(get_layer<I>().load(is), ...);
|
||||
}
|
||||
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...>) {
|
||||
(get_layer<I>().fill_random(dist), ...);
|
||||
}
|
||||
public:
|
||||
const output_type& feed(const input_type& input) {
|
||||
feed<sizeof...(Layers) - 1>(input);
|
||||
return get_layer<sizeof...(Layers) - 1>().output;
|
||||
}
|
||||
private:
|
||||
template <std::size_t I>
|
||||
auto feed(const input_type& input) {
|
||||
if constexpr (I == 0) {
|
||||
return get_layer<I>().forward(input);
|
||||
} else {
|
||||
return get_layer<I>().forward(feed<I - 1>(input));
|
||||
}
|
||||
}
|
||||
public:
|
||||
struct learning_state final {
|
||||
friend class nn;
|
||||
|
||||
void reset() {
|
||||
m_count = 0;
|
||||
reset(std::make_index_sequence<sizeof...(Layers)>{});
|
||||
}
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void reset(std::index_sequence<I...>) {
|
||||
(std::get<I>(m_layers).reset(), ...);
|
||||
}
|
||||
private:
|
||||
template <typename Seq>
|
||||
struct gen {};
|
||||
|
||||
template <std::size_t... I>
|
||||
struct gen<std::index_sequence<I...>> {
|
||||
using type = std::tuple<typename Layers::template inner<nn, I>::gradient...>;
|
||||
};
|
||||
private:
|
||||
std::size_t m_count = 0;
|
||||
|
||||
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
|
||||
};
|
||||
public:
|
||||
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
||||
feed(input);
|
||||
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
|
||||
get_layer<0>().backward(input,
|
||||
std::get<0>(state.m_layers),
|
||||
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
||||
state.m_count += 1;
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, typename ErrorType>
|
||||
auto backprop_chain(learning_state& state, ErrorType error) {
|
||||
if constexpr (I != 0) {
|
||||
auto next = get_layer<I>().backward(get_layer<I - 1>().output, std::get<I>(state.m_layers), error);
|
||||
return backprop_chain<I - 1>(state, next);
|
||||
} else {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
public:
|
||||
void apply(learning_state& state, double rate) {
|
||||
apply(state, rate, std::make_index_sequence<sizeof...(Layers)>{});
|
||||
}
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
|
||||
(std::get<I>(m_layers).apply_grad(std::get<I>(state.m_layers), state.m_count, rate), ...);
|
||||
}
|
||||
public:
|
||||
void train(const input_type& input, const output_type& expected, double learningRate) {
|
||||
learning_state state;
|
||||
backprop(input, expected, state);
|
||||
apply(state, learningRate);
|
||||
}
|
||||
private:
|
||||
layer_types m_layers;
|
||||
};
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_NN_HPP
|
||||
#endif // CENET_LAYER_HPP
|
||||
@@ -0,0 +1,148 @@
|
||||
#ifndef CENET_NETWORK_HPP
|
||||
#define CENET_NETWORK_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
namespace cenet {
|
||||
|
||||
template <typename Input, typename... Layers>
|
||||
class network {
|
||||
friend class layer_info;
|
||||
private:
|
||||
template <typename Seq>
|
||||
struct layer_types_gen {};
|
||||
|
||||
template <std::size_t... I>
|
||||
struct layer_types_gen<std::index_sequence<I...>> {
|
||||
using type = std::tuple<typename Layers::template inner<network, I>...>;
|
||||
};
|
||||
|
||||
using layer_types = typename layer_types_gen<std::make_index_sequence<sizeof...(Layers)>>::type;
|
||||
using output_layer_type = std::tuple_element_t<sizeof...(Layers) - 1, layer_types>;
|
||||
|
||||
template <std::size_t I>
|
||||
using layer_input_t = typename std::tuple_element_t<I, layer_types>::input_type;
|
||||
private:
|
||||
template <std::size_t I>
|
||||
auto& get_layer() {
|
||||
return std::get<I>(m_layers);
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
const auto& get_layer() const {
|
||||
return std::get<I>(m_layers);
|
||||
}
|
||||
public:
|
||||
using input_type = Input::inner;
|
||||
using output_type = output_layer_type::output_type;
|
||||
public:
|
||||
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
|
||||
public:
|
||||
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
|
||||
(get_layer<I>().dump(os), ...);
|
||||
}
|
||||
public:
|
||||
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void load_network(std::istream& is, std::index_sequence<I...>) {
|
||||
(get_layer<I>().load(is), ...);
|
||||
}
|
||||
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...>) {
|
||||
(get_layer<I>().fill_random(dist), ...);
|
||||
}
|
||||
public:
|
||||
const output_type& feed(const input_type& input) {
|
||||
feed<sizeof...(Layers) - 1>(input);
|
||||
return get_layer<sizeof...(Layers) - 1>().output;
|
||||
}
|
||||
private:
|
||||
template <std::size_t I>
|
||||
auto feed(const input_type& input) {
|
||||
if constexpr (I == 0) {
|
||||
return get_layer<I>().forward(input);
|
||||
} else {
|
||||
return get_layer<I>().forward(feed<I - 1>(input));
|
||||
}
|
||||
}
|
||||
public:
|
||||
struct learning_state final {
|
||||
friend class network;
|
||||
|
||||
void reset() {
|
||||
m_count = 0;
|
||||
reset(std::make_index_sequence<sizeof...(Layers)>{});
|
||||
}
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void reset(std::index_sequence<I...>) {
|
||||
(std::get<I>(m_layers).reset(), ...);
|
||||
}
|
||||
private:
|
||||
template <typename Seq>
|
||||
struct gen {};
|
||||
|
||||
template <std::size_t... I>
|
||||
struct gen<std::index_sequence<I...>> {
|
||||
using type = std::tuple<typename Layers::template inner<network, I>::gradient...>;
|
||||
};
|
||||
private:
|
||||
std::size_t m_count = 0;
|
||||
|
||||
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
|
||||
};
|
||||
public:
|
||||
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
||||
feed(input);
|
||||
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
|
||||
get_layer<0>().backward(input,
|
||||
std::get<0>(state.m_layers),
|
||||
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
||||
state.m_count += 1;
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, typename ErrorType>
|
||||
auto backprop_chain(learning_state& state, ErrorType error) {
|
||||
if constexpr (I != 0) {
|
||||
auto next = get_layer<I>().backward(get_layer<I - 1>().output, std::get<I>(state.m_layers), error);
|
||||
return backprop_chain<I - 1>(state, next);
|
||||
} else {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
public:
|
||||
void apply(learning_state& state, double rate) {
|
||||
apply(state, rate, std::make_index_sequence<sizeof...(Layers)>{});
|
||||
}
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
|
||||
(std::get<I>(m_layers).apply_grad(std::get<I>(state.m_layers), state.m_count, rate), ...);
|
||||
}
|
||||
public:
|
||||
void train(const input_type& input, const output_type& expected, double learningRate) {
|
||||
learning_state state;
|
||||
backprop(input, expected, state);
|
||||
apply(state, learningRate);
|
||||
}
|
||||
private:
|
||||
layer_types m_layers;
|
||||
};
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_NETWORK_HPP
|
||||
Reference in New Issue
Block a user