refactor: abstract out the layers

This commit is contained in:
2026-09-18 18:11:56 +02:00
parent 8ae45ad7a9
commit d76e26e2f0
4 changed files with 205 additions and 152 deletions
+4 -3
View File
@@ -92,7 +92,8 @@ static void shuffle_dataset(dataset& set) {
}
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();
static constexpr const char* NETWORK_PATH = "./mnist.nn";
@@ -149,11 +150,11 @@ int main(void) {
const auto& output = mnistNet->feed(validationSet[i].xs);
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;
}
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());
+3 -2
View File
@@ -21,7 +21,8 @@ int main(void) {
};
// 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";
@@ -51,7 +52,7 @@ int main(void) {
xs[0][0] = 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];
if ((gen % 1000) == 0) {