-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (62 loc) · 2.17 KB
/
main.cpp
File metadata and controls
75 lines (62 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "engine.hpp"
#include "nn.hpp"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
using namespace micrograd;
// Helper to load CSV data
void load_data(const std::string& filename, std::vector<std::vector<double>>& inputs, std::vector<std::vector<double>>& targets) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
exit(1);
}
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string val;
std::vector<double> row;
while (std::getline(ss, val, ',')) {
row.push_back(std::stod(val));
}
if (row.size() >= 2) {
targets.push_back({row.back()});
row.pop_back();
inputs.push_back(row);
}
}
}
int main() {
// 1. Load Data
std::vector<std::vector<double>> X;
std::vector<std::vector<double>> y;
std::cout << "Loading data from moons.csv..." << std::endl;
load_data("moons.csv", X, y);
std::cout << "Loaded " << X.size() << " samples." << std::endl;
// 2. Initialize Model
MLP<double, ReLUActivation<double>, TanhActivation<double>, HeInit<double>> model(2, {16, 16, 1});
// 3. Setup Optimizer
SGD<decltype(model), double> optimizer(model, 0.001);
// 4. Train
std::cout << "Training..." << std::endl;
train(model, optimizer, X, y, 100);
// 5. Evaluate and Export Grid for Visualization
std::cout << "Generating decision boundary grid..." << std::endl;
std::ofstream out_file("predictions.csv");
// Grid ranges based on moons dataset
double x_min = -1.5, x_max = 2.5;
double y_min = -1.0, y_max = 1.5;
double step = 0.1;
for (double i = x_min; i <= x_max; i += step) {
for (double j = y_min; j <= y_max; j += step) {
std::vector<double> input = {i, j};
auto output = model(input);
out_file << i << "," << j << "," << output[0]->data << "\n";
}
}
out_file.close();
std::cout << "Predictions saved to predictions.csv" << std::endl;
return 0;
}