+31
-25
@@ -40,7 +40,11 @@ public:
|
||||
using input_type = Input;
|
||||
using output_type = typename output_layer_type::output_type;
|
||||
public:
|
||||
const output_type& get_output() const { return get_layer<sizeof...(Layers) - 1>().output; }
|
||||
const output_type& get_output() const { return m_output; }
|
||||
public:
|
||||
struct loss {
|
||||
double value;
|
||||
};
|
||||
public:
|
||||
void dump_network(std::ostream& os) const { dump_network(os, std::make_index_sequence<sizeof...(Layers)>{}); }
|
||||
private:
|
||||
@@ -66,27 +70,31 @@ private:
|
||||
(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;
|
||||
}
|
||||
output_type feed(input_type input) { return feed_impl<0>(std::move(input)); }
|
||||
private:
|
||||
template <std::size_t I>
|
||||
auto feed(const input_type& input) {
|
||||
if constexpr (I == 0) {
|
||||
return get_layer<I>().forward(input);
|
||||
template <std::size_t I, typename Current>
|
||||
auto feed_impl(Current&& current) {
|
||||
if constexpr (I == sizeof...(Layers)) {
|
||||
return std::forward<Current>(current);
|
||||
} else {
|
||||
return get_layer<I>().forward(feed<I - 1>(input));
|
||||
return feed_impl<I + 1>(get_layer<I>().feed(std::forward<Current>(current)));
|
||||
}
|
||||
}
|
||||
public:
|
||||
struct learning_state final {
|
||||
friend class network;
|
||||
|
||||
void reset() {
|
||||
m_count = 0;
|
||||
reset(std::make_index_sequence<sizeof...(Layers)>{});
|
||||
}
|
||||
|
||||
template <std::size_t I, typename = std::enable_if_t<(I < sizeof...(Layers))>>
|
||||
decltype(auto) get() {
|
||||
return std::get<I>(m_layers);
|
||||
}
|
||||
|
||||
void increment() { ++m_count; }
|
||||
|
||||
auto count() const { return m_count; }
|
||||
private:
|
||||
template <std::size_t... I>
|
||||
void reset(std::index_sequence<I...>) {
|
||||
@@ -106,20 +114,17 @@ public:
|
||||
typename 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() - expected;
|
||||
get_layer<0>().backward(input,
|
||||
std::get<0>(state.m_layers),
|
||||
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
||||
state.m_count += 1;
|
||||
template <typename Loss, typename LState>
|
||||
void backward(const Loss& loss, LState& state) {
|
||||
get_layer<0>().backward(state.template get<0>(), backward_chain<sizeof...(Layers) - 1>(state, loss.gradient));
|
||||
state.increment();
|
||||
}
|
||||
private:
|
||||
template <std::size_t I, typename ErrorType>
|
||||
auto backprop_chain(learning_state& state, ErrorType error) {
|
||||
template <std::size_t I, typename LState, typename ErrorType>
|
||||
auto backward_chain(LState& 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);
|
||||
auto next = get_layer<I>().backward(state.template get<I>(), error);
|
||||
return backward_chain<I - 1>(state, next);
|
||||
} else {
|
||||
return error;
|
||||
}
|
||||
@@ -131,16 +136,17 @@ public:
|
||||
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), ...);
|
||||
(std::get<I>(m_layers).apply_grad(state.template get<I>(), state.count(), rate), ...);
|
||||
}
|
||||
public:
|
||||
void train(const input_type& input, const output_type& expected, double learningRate) {
|
||||
learning_state state;
|
||||
backprop(input, expected, state);
|
||||
backward(input, expected, state);
|
||||
apply(state, learningRate);
|
||||
}
|
||||
private:
|
||||
layer_types m_layers;
|
||||
output_type m_output;
|
||||
};
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
Reference in New Issue
Block a user