feat: add network saving and loading
This commit is contained in:
@@ -138,6 +138,12 @@ public:
|
||||
|
||||
return res;
|
||||
}
|
||||
public:
|
||||
T* data() { return m_items; }
|
||||
|
||||
const T* data() const { return m_items; }
|
||||
|
||||
constexpr std::size_t size() const { return M * N; }
|
||||
private:
|
||||
T m_items[M * N];
|
||||
};
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@@ -31,6 +33,34 @@ public:
|
||||
using input_type = mat<1, N>;
|
||||
public:
|
||||
const output_type& get_output() const { return std::get<sizeof...(Layers) - 1>(m_layers).activations; }
|
||||
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 {
|
||||
(dump_layer<I>(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:
|
||||
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...>) {
|
||||
(load_layer<I>(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:
|
||||
template <typename Dist>
|
||||
void fill_random(const Dist& dist) {
|
||||
|
||||
Reference in New Issue
Block a user