refactor(examples): port the mnist example

This commit is contained in:
2026-11-02 03:01:48 +01:00
parent f59602e99a
commit d97b65678a
3 changed files with 369 additions and 20 deletions
+23 -20
View File
@@ -1,3 +1,5 @@
#include "cenet/layers/convolutional.hpp"
#include "cenet/layers/dense.hpp"
#include "cenet/loss/mse.hpp"
#include <algorithm>
@@ -13,28 +15,28 @@
#include <random>
#include <vector>
using network_type = cenet::network<cenet::tensor<float, 1, 28, 28>,
cenet::conv2d_layer<8, 3, 1, 1>,
cenet::relu_layer,
cenet::maxpool_layer<>,
cenet::conv2d_layer<16, 3, 1, 1>,
cenet::relu_layer,
cenet::maxpool_layer<>,
cenet::flatten_layer,
cenet::dense_layer<10>,
cenet::sigmoid_layer>;
using network_type = cenet::network<cenet::math::tensor<float, 1, 28, 28>,
cenet::layers::conv2d<8, 3, 1, 1>,
cenet::layers::relu,
cenet::layers::maxpool<>,
cenet::layers::conv2d<16, 3, 1, 1>,
cenet::layers::relu,
cenet::layers::maxpool<>,
cenet::layers::flatten,
cenet::layers::dense<10>,
cenet::layers::sigmoid>;
static std::uniform_real_distribution<> dist(-1, 1);
struct data {
cenet::tensor<float, 1, 28, 28> xs;
cenet::mat<1, 10, float> ys;
cenet::math::tensor<float, 1, 28, 28> xs{};
cenet::math::matrix<float, 1, 10> ys{};
};
using dataset = std::vector<data>;
template <std::size_t N, typename T>
static std::size_t col_largest_idx(const cenet::mat_col<N, T>& col) {
static std::size_t col_largest_idx(const cenet::math::tensor_view<T, N>& col) {
std::size_t maxIdx = 0;
std::remove_cv_t<T> max = col[0];
for (std::size_t i = 1; i < N; ++i) {
@@ -89,6 +91,7 @@ static dataset load_dataset(const char* labelsPath, const char* imagesPath) {
std::uint8_t label = 0;
labels.read(reinterpret_cast<char*>(&label), sizeof(label));
set[i].ys.view().fill();
set[i].ys[0][label] = 1.0;
}
@@ -108,10 +111,10 @@ int main(void) {
static constexpr const char* NETWORK_PATH = "./mnist-conv.nn";
std::ifstream file(NETWORK_PATH, std::ios::in | std::ios::binary);
if (file.is_open())
mnistNet->load_network(file);
else
mnistNet->fill_random(dist);
// if (file.is_open())
// mnistNet->load_network(file);
// else
mnistNet->fill_random(dist);
dataset trainSet = load_dataset("./mnist/train-labels-idx1-ubyte", "./mnist/train-images-idx3-ubyte");
if (trainSet.empty()) return 1;
@@ -123,7 +126,7 @@ int main(void) {
std::size_t patienceCounter = 0;
static constexpr std::size_t MAX_PATIENCE = 10;
static constexpr std::size_t MAX_EPOCHS = 1000;
static constexpr std::size_t MAX_EPOCHS = 2;
static constexpr std::size_t BATCH_SIZE = 64;
static constexpr std::size_t EPL = 1; // Epochs Per Log
@@ -140,10 +143,10 @@ int main(void) {
auto epochStart = std::chrono::high_resolution_clock::now();
for (std::size_t i = 0; i < trainSet.size(); i += BATCH_SIZE) {
if (i > 0) state->reset();
state->reset();
for (std::size_t j = 0; j < std::min(BATCH_SIZE, trainSet.size() - i); ++j) {
auto output = mnistNet->feed(trainSet[i + j].xs);
auto output = std::move(mnistNet->feed(trainSet[i + j].xs));
auto loss = cenet::loss::mean_squared_error::calculate(trainSet[i + j].ys, output);
mnistNet->backward(loss, *state);
}