refactor: abstract out the layers
This commit is contained in:
+195
-144
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "mat.hpp"
|
#include "mat.hpp"
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
@@ -12,115 +11,221 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
template <std::size_t N, std::size_t... Layers>
|
enum class layer_type {
|
||||||
class nn {
|
Dense,
|
||||||
|
};
|
||||||
|
|
||||||
|
class layer_input {};
|
||||||
|
|
||||||
|
template <std::size_t Features>
|
||||||
|
class dense_input : public layer_input {
|
||||||
|
template <typename Input, typename... Layers>
|
||||||
|
friend class nn;
|
||||||
|
|
||||||
|
friend class layer_info;
|
||||||
private:
|
private:
|
||||||
template <std::size_t In, std::size_t Out>
|
struct inner final {
|
||||||
struct gradient {
|
static constexpr layer_type TYPE = layer_type::Dense;
|
||||||
static constexpr std::size_t input_count = In;
|
|
||||||
static constexpr std::size_t output_count = Out;
|
|
||||||
|
|
||||||
mat<In, Out> weights;
|
static constexpr std::size_t FEATURES = Features;
|
||||||
mat<1, Out> biases;
|
|
||||||
|
|
||||||
mat<In, Out> momentumWeights;
|
using type = mat<1, FEATURES>;
|
||||||
mat<1, Out> momentumBiases;
|
|
||||||
mat<In, Out> varianceWeights;
|
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||||
mat<1, Out> varianceBiases;
|
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>
|
||||||
|
struct input_type_s {
|
||||||
|
using type = std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class NN>
|
||||||
|
struct input_type_s<NN, 0> {
|
||||||
|
using type = typename NN::input_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using input_inner_t = typename T::inner;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t Features>
|
||||||
|
class dense_layer final : public layer_info {
|
||||||
|
template <typename Input, typename... Layers>
|
||||||
|
friend class nn;
|
||||||
|
private:
|
||||||
|
template <class NN, std::size_t I>
|
||||||
|
struct inner final {
|
||||||
|
static constexpr layer_type TYPE = layer_type::Dense;
|
||||||
|
|
||||||
|
using input_type = input_type_s<NN, I>::type;
|
||||||
|
static_assert(input_type::TYPE == layer_type::Dense);
|
||||||
|
|
||||||
|
using output_type = input_inner_t<dense_input<Features>>;
|
||||||
|
|
||||||
|
static constexpr std::size_t INPUT_FEATURES = input_type::FEATURES;
|
||||||
|
static constexpr std::size_t OUTPUT_FEATURES = Features;
|
||||||
|
|
||||||
|
using weights_type = mat<INPUT_FEATURES, OUTPUT_FEATURES>;
|
||||||
|
using biases_type = mat<1, OUTPUT_FEATURES>;
|
||||||
|
|
||||||
|
struct gradient final {
|
||||||
|
using parent_type = inner;
|
||||||
|
|
||||||
|
weights_type weights;
|
||||||
|
biases_type biases;
|
||||||
|
|
||||||
|
weights_type momentumWeights;
|
||||||
|
biases_type momentumBiases;
|
||||||
|
weights_type varianceWeights;
|
||||||
|
biases_type varianceBiases;
|
||||||
|
|
||||||
std::size_t time = 0;
|
std::size_t time = 0;
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
weights = mat<In, Out>{};
|
weights = weights_type{};
|
||||||
biases = mat<1, Out>{};
|
biases = biases_type{};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t In, std::size_t Out>
|
weights_type weights;
|
||||||
struct layer {
|
biases_type biases;
|
||||||
static constexpr std::size_t input_count = In;
|
output_type output;
|
||||||
static constexpr std::size_t output_count = Out;
|
|
||||||
|
|
||||||
mat<In, Out> weights;
|
static double sigmoid(double input) { return 1 / (1 + std::exp(-input)); }
|
||||||
mat<1, Out> biases;
|
|
||||||
mat<1, Out> activations;
|
|
||||||
|
|
||||||
void apply(gradient<In, Out>& gradient, double divisor, double rate) {
|
void dump(std::ostream& os) const {
|
||||||
++gradient.time;
|
os.write(reinterpret_cast<const char*>(weights.data()), sizeof(double) * weights.size());
|
||||||
|
os.write(reinterpret_cast<const char*>(biases.data()), sizeof(double) * biases.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void load(std::istream& is) {
|
||||||
|
is.read(reinterpret_cast<char*>(weights.data()), sizeof(double) * weights.size());
|
||||||
|
is.read(reinterpret_cast<char*>(biases.data()), sizeof(double) * biases.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Dist>
|
||||||
|
void fill_random(const Dist& dist) {
|
||||||
|
double scale = std::sqrt(2.0 / static_cast<double>(INPUT_FEATURES));
|
||||||
|
weights = decltype(weights)::random(dist) * scale;
|
||||||
|
biases = decltype(biases)::random(dist) * scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& forward(const input_type& input) {
|
||||||
|
output = (input.get() * weights) + biases;
|
||||||
|
for (std::size_t i = 0; i < OUTPUT_FEATURES; ++i) {
|
||||||
|
output.get()[0][i] = sigmoid(output.get()[0][i]);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Error>
|
||||||
|
auto backward(const input_type& input, gradient& grad, Error prevErr) {
|
||||||
|
grad.weights += input.get().transpose() * prevErr;
|
||||||
|
grad.biases += prevErr;
|
||||||
|
|
||||||
|
auto backpropErr = prevErr * weights.transpose();
|
||||||
|
if constexpr (I > 0) {
|
||||||
|
return backpropErr.hadamard(input.get().hadamard(1.0 - input.get()));
|
||||||
|
} else {
|
||||||
|
return backpropErr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void apply_grad(gradient& grad, double divisor, double rate) {
|
||||||
|
++grad.time;
|
||||||
|
|
||||||
constexpr double beta1 = 0.9;
|
constexpr double beta1 = 0.9;
|
||||||
constexpr double beta2 = 0.999;
|
constexpr double beta2 = 0.999;
|
||||||
constexpr double epsilon = 1e-8;
|
constexpr double epsilon = 1e-8;
|
||||||
|
|
||||||
for (std::size_t row = 0; row < In; ++row) {
|
for (std::size_t row = 0; row < INPUT_FEATURES; ++row) {
|
||||||
for (std::size_t col = 0; col < Out; ++col) {
|
for (std::size_t col = 0; col < OUTPUT_FEATURES; ++col) {
|
||||||
double avgGrad = gradient.weights[row][col] / divisor;
|
double avgGrad = grad.weights[row][col] / divisor;
|
||||||
|
|
||||||
gradient.momentumWeights[row][col] =
|
grad.momentumWeights[row][col] = beta1 * grad.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
||||||
beta1 * gradient.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
grad.varianceWeights[row][col] =
|
||||||
gradient.varianceWeights[row][col] =
|
beta2 * grad.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||||
beta2 * gradient.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
|
||||||
|
|
||||||
double momentumCorrected =
|
double momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
gradient.momentumWeights[row][col] / (1.0 - std::pow(beta1, gradient.time));
|
double varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
double varianceCorrected =
|
|
||||||
gradient.varianceWeights[row][col] / (1.0 - std::pow(beta2, gradient.time));
|
|
||||||
|
|
||||||
weights[row][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
weights[row][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t col = 0; col < Out; ++col) {
|
for (std::size_t col = 0; col < OUTPUT_FEATURES; ++col) {
|
||||||
double avgGrad = gradient.biases[0][col] / divisor;
|
double avgGrad = grad.biases[0][col] / divisor;
|
||||||
|
|
||||||
gradient.momentumBiases[0][col] = beta1 * gradient.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
grad.momentumBiases[0][col] = beta1 * grad.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
||||||
gradient.varianceBiases[0][col] =
|
grad.varianceBiases[0][col] = beta2 * grad.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||||
beta2 * gradient.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
|
||||||
|
|
||||||
double momentumCorrected = gradient.momentumBiases[0][col] / (1.0 - std::pow(beta1, gradient.time));
|
double momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
double varianceCorrected = gradient.varianceBiases[0][col] / (1.0 - std::pow(beta2, gradient.time));
|
double varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
|
|
||||||
biases[0][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
biases[0][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Input, typename... Layers>
|
||||||
|
class nn {
|
||||||
|
friend class layer_info;
|
||||||
private:
|
private:
|
||||||
static auto make_output() {
|
template <typename Seq>
|
||||||
constexpr std::array<std::size_t, sizeof...(Layers) + 1> dims = { N, Layers... };
|
struct layer_types_gen {};
|
||||||
return mat<1, std::get<sizeof...(Layers)>(dims)>{};
|
|
||||||
|
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:
|
public:
|
||||||
using output_type = decltype(make_output());
|
using input_type = Input::inner;
|
||||||
using input_type = mat<1, N>;
|
using output_type = output_layer_type::output_type;
|
||||||
public:
|
public:
|
||||||
const output_type& get_output() const { return std::get<sizeof...(Layers) - 1>(m_layers).activations; }
|
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
|
||||||
public:
|
public:
|
||||||
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
|
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||||
private:
|
private:
|
||||||
template <std::size_t... I>
|
template <std::size_t... I>
|
||||||
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
|
void dump_network(std::ostream& os, std::index_sequence<I...>) const {
|
||||||
(dump_layer<I>(os), ...);
|
(get_layer<I>().dump(os), ...);
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I>
|
|
||||||
void dump_layer(std::ostream& os) const {
|
|
||||||
const auto& layer = std::get<I>(m_layers);
|
|
||||||
os.write(reinterpret_cast<const char*>(layer.weights.data()), sizeof(double) * layer.weights.size());
|
|
||||||
os.write(reinterpret_cast<const char*>(layer.biases.data()), sizeof(double) * layer.biases.size());
|
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
|
void load_network(std::istream& is) { load_network(is, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||||
private:
|
private:
|
||||||
template <std::size_t... I>
|
template <std::size_t... I>
|
||||||
void load_network(std::istream& is, std::index_sequence<I...>) {
|
void load_network(std::istream& is, std::index_sequence<I...>) {
|
||||||
(load_layer<I>(is), ...);
|
(get_layer<I>().load(is), ...);
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I>
|
|
||||||
void load_layer(std::istream& is) {
|
|
||||||
auto& layer = std::get<I>(m_layers);
|
|
||||||
is.read(reinterpret_cast<char*>(layer.weights.data()), sizeof(double) * layer.weights.size());
|
|
||||||
is.read(reinterpret_cast<char*>(layer.biases.data()), sizeof(double) * layer.biases.size());
|
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
template <typename Dist>
|
template <typename Dist>
|
||||||
@@ -130,51 +235,21 @@ public:
|
|||||||
private:
|
private:
|
||||||
template <typename Dist, std::size_t... I>
|
template <typename Dist, std::size_t... I>
|
||||||
void fill_random(const Dist& dist, std::index_sequence<I...>) {
|
void fill_random(const Dist& dist, std::index_sequence<I...>) {
|
||||||
(fill_random<I>(dist), ...);
|
(get_layer<I>().fill_random(dist), ...);
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I, typename Dist>
|
|
||||||
void fill_random(const Dist& dist) {
|
|
||||||
auto& layer = std::get<I>(m_layers);
|
|
||||||
double scale = std::sqrt(2.0 / static_cast<double>(std::tuple_element_t<I, layers_type>::input_count));
|
|
||||||
layer.weights = decltype(layer.weights)::random(dist) * scale;
|
|
||||||
layer.biases = decltype(layer.biases)::random(dist) * scale;
|
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
const output_type& feed(const input_type& input) {
|
const output_type& feed(const input_type& input) {
|
||||||
feed(input, std::make_index_sequence<sizeof...(Layers)>{});
|
feed<sizeof...(Layers) - 1>(input);
|
||||||
return get_output();
|
return get_layer<sizeof...(Layers) - 1>().output;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template <std::size_t... I>
|
|
||||||
void feed(const input_type& input, std::index_sequence<I...>) {
|
|
||||||
((feed<I>(input)), ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I>
|
template <std::size_t I>
|
||||||
void feed(const input_type& input) {
|
auto 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) {
|
if constexpr (I == 0) {
|
||||||
inputs = input;
|
return get_layer<I>().forward(input);
|
||||||
} else {
|
} else {
|
||||||
inputs = std::get<I - 1>(m_layers).activations;
|
return get_layer<I>().forward(feed<I - 1>(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
template <std::size_t... I>
|
|
||||||
static auto make_gradients_tuple(std::index_sequence<I...>) {
|
|
||||||
constexpr std::array<std::size_t, sizeof...(Layers) + 1> dims = { N, Layers... };
|
|
||||||
return std::tuple<gradient<std::get<I>(dims), std::get<I + 1>(dims)>...>{};
|
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
struct learning_state final {
|
struct learning_state final {
|
||||||
@@ -189,42 +264,36 @@ public:
|
|||||||
void reset(std::index_sequence<I...>) {
|
void reset(std::index_sequence<I...>) {
|
||||||
(std::get<I>(m_layers).reset(), ...);
|
(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:
|
private:
|
||||||
std::size_t m_count = 0;
|
std::size_t m_count = 0;
|
||||||
|
|
||||||
decltype(make_gradients_tuple(std::make_index_sequence<sizeof...(Layers)>{})) m_layers;
|
gen<std::make_index_sequence<sizeof...(Layers)>>::type m_layers;
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
||||||
feed(input);
|
feed(input);
|
||||||
auto outputErr = (get_output() - expected).hadamard(get_output()).hadamard(1.0 - get_output());
|
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
|
||||||
backprop_chain<sizeof...(Layers) - 1>(input, state, outputErr);
|
get_layer<0>().backward(input,
|
||||||
|
std::get<0>(state.m_layers),
|
||||||
|
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
||||||
state.m_count += 1;
|
state.m_count += 1;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template <std::size_t I, typename ErrorType>
|
template <std::size_t I, typename ErrorType>
|
||||||
void backprop_chain(const input_type& input, learning_state& state, ErrorType error) {
|
auto backprop_chain(learning_state& state, ErrorType error) {
|
||||||
auto next = backprop<I>(input, state, error);
|
if constexpr (I != 0) {
|
||||||
if constexpr (I > 0) {
|
auto next = get_layer<I>().backward(get_layer<I - 1>().output, std::get<I>(state.m_layers), error);
|
||||||
backprop_chain<I - 1>(input, state, next);
|
return backprop_chain<I - 1>(state, next);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t I, typename ErrorType>
|
|
||||||
auto backprop(const input_type& input, learning_state& state, ErrorType prevErr) {
|
|
||||||
const auto& layer = std::get<I>(m_layers);
|
|
||||||
auto& gradient = std::get<I>(state.m_layers);
|
|
||||||
|
|
||||||
const auto& layerInput = get_layer_input<I>(input);
|
|
||||||
|
|
||||||
gradient.weights += layerInput.transpose() * prevErr;
|
|
||||||
gradient.biases += prevErr;
|
|
||||||
|
|
||||||
auto backpropErr = prevErr * layer.weights.transpose();
|
|
||||||
if constexpr (I > 0) {
|
|
||||||
return backpropErr.hadamard(layerInput.hadamard(1.0 - layerInput));
|
|
||||||
} else {
|
} else {
|
||||||
return backpropErr;
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
@@ -234,7 +303,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
template <std::size_t... I>
|
template <std::size_t... I>
|
||||||
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
|
void apply(learning_state& state, double rate, std::index_sequence<I...>) {
|
||||||
(std::get<I>(m_layers).apply(std::get<I>(state.m_layers), state.m_count, rate), ...);
|
(std::get<I>(m_layers).apply_grad(std::get<I>(state.m_layers), state.m_count, rate), ...);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
void train(const input_type& input, const output_type& expected, double learningRate) {
|
void train(const input_type& input, const output_type& expected, double learningRate) {
|
||||||
@@ -243,25 +312,7 @@ public:
|
|||||||
apply(state, learningRate);
|
apply(state, learningRate);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template <std::size_t I>
|
layer_types m_layers;
|
||||||
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
|
#endif // NN_HPP
|
||||||
|
|||||||
+4
-3
@@ -92,7 +92,8 @@ static void shuffle_dataset(dataset& set) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
using network_type = nn<784, 300, 100, 10>;
|
using network_type = nn<dense_input<784>, dense_layer<300>, dense_layer<100>, dense_layer<10>>;
|
||||||
|
|
||||||
network_type* mnistNet = new network_type();
|
network_type* mnistNet = new network_type();
|
||||||
|
|
||||||
static constexpr const char* NETWORK_PATH = "./mnist.nn";
|
static constexpr const char* NETWORK_PATH = "./mnist.nn";
|
||||||
@@ -149,11 +150,11 @@ int main(void) {
|
|||||||
const auto& output = mnistNet->feed(validationSet[i].xs);
|
const auto& output = mnistNet->feed(validationSet[i].xs);
|
||||||
|
|
||||||
for (std::size_t j = 0; j < 10; ++j) {
|
for (std::size_t j = 0; j < 10; ++j) {
|
||||||
double diff = output[0][j] - validationSet[i].ys[0][j];
|
double diff = output.get()[0][j] - validationSet[i].ys[0][j];
|
||||||
totalLoss += diff * diff;
|
totalLoss += diff * diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (col_largest_idx(output[0]) == col_largest_idx(validationSet[i].ys[0])) ++hits;
|
if (col_largest_idx(output.get()[0]) == col_largest_idx(validationSet[i].ys[0])) ++hits;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentLoss = totalLoss / static_cast<double>(validationSet.size());
|
currentLoss = totalLoss / static_cast<double>(validationSet.size());
|
||||||
|
|||||||
+3
-2
@@ -21,7 +21,8 @@ int main(void) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// network net;
|
// network net;
|
||||||
nn<2, 2, 1> xorNet;
|
using network_type = nn<dense_input<2>, dense_layer<2>, dense_layer<1>>;
|
||||||
|
network_type xorNet;
|
||||||
|
|
||||||
static constexpr const char* NETWORK_PATH = "./xor.nn";
|
static constexpr const char* NETWORK_PATH = "./xor.nn";
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ int main(void) {
|
|||||||
xs[0][0] = static_cast<double>(i & 1);
|
xs[0][0] = static_cast<double>(i & 1);
|
||||||
xs[0][1] = static_cast<double>(i > 1);
|
xs[0][1] = static_cast<double>(i > 1);
|
||||||
|
|
||||||
double yHat = xorNet.feed(xs)[0][0];
|
double yHat = xorNet.feed(xs).get()[0][0];
|
||||||
double error = yHat - s_trainData[i];
|
double error = yHat - s_trainData[i];
|
||||||
|
|
||||||
if ((gen % 1000) == 0) {
|
if ((gen % 1000) == 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user