-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinyAI.hpp
More file actions
122 lines (96 loc) · 3.51 KB
/
TinyAI.hpp
File metadata and controls
122 lines (96 loc) · 3.51 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// TinyAI.hpp
// 7-25
//
// Created by Amit Prakash on 2/11/26.
//
// TinyAI.hpp
#pragma once
#include <cmath>
#include <algorithm>
#include <random>
// A tiny 2->8->1 neural net that predicts coin bias from (heads, tails).
struct TinyAI {
static constexpr int H = 8;
double W1[H][2] = {}; // hidden weights
double b1[H] = {}; // hidden bias
double W2[H] = {}; // output weights
double b2 = 0.0; // output bias
static double sigmoid(double z) {
z = std::max(-60.0, std::min(60.0, z));
return 1.0 / (1.0 + std::exp(-z));
}
static double dtanh_from_tanh(double t) { return 1.0 - t * t; }
void init(unsigned seed = 12345) {
std::mt19937 gen(seed);
std::normal_distribution<double> nd(0.0, 0.3);
for (int i = 0; i < H; i++) {
for (int j = 0; j < 2; j++) W1[i][j] = nd(gen);
b1[i] = nd(gen);
W2[i] = nd(gen);
}
b2 = nd(gen);
}
double predict(int heads, int tails) const {
int n = heads + tails;
if (n <= 0) return 0.5;
double x1 = (double)heads / (double)n;
double x2 = std::log((double)n + 1.0);
double h[H];
for (int i = 0; i < H; i++) {
double z = W1[i][0] * x1 + W1[i][1] * x2 + b1[i];
h[i] = std::tanh(z);
}
double z2 = b2;
for (int i = 0; i < H; i++) z2 += W2[i] * h[i];
return sigmoid(z2);
}
void trainSynthetic(int steps = 2000, int batch = 64, double lr = 0.05, unsigned seed = 7) {
std::mt19937 gen(seed);
std::uniform_real_distribution<double> unif01(0.0, 1.0);
for (int step = 0; step < steps; step++) {
// gradients
double gW1[H][2] = {};
double gb1[H] = {};
double gW2[H] = {};
double gb2 = 0.0;
for (int k = 0; k < batch; k++) {
double theta = unif01(gen);
int n = 20 + (gen() % 281);
int heads = 0;
for (int i = 0; i < n; i++) if (unif01(gen) < theta) heads++;
double x1 = (double)heads / (double)n;
double x2 = std::log((double)n + 1.0);
double h[H], z1[H];
for (int i = 0; i < H; i++) {
z1[i] = W1[i][0] * x1 + W1[i][1] * x2 + b1[i];
h[i] = std::tanh(z1[i]);
}
double z2 = b2;
for (int i = 0; i < H; i++) z2 += W2[i] * h[i];
double y = sigmoid(z2); // prediction
// loss = (y - theta)^2
double dL_dy = 2.0 * (y - theta);
double dy_dz2 = y * (1.0 - y);
double dL_dz2 = dL_dy * dy_dz2;
for (int i = 0; i < H; i++) gW2[i] += dL_dz2 * h[i];
gb2 += dL_dz2;
for (int i = 0; i < H; i++) {
double dL_dhi = dL_dz2 * W2[i];
double dhi_dz1 = dtanh_from_tanh(h[i]);
double dL_dz1 = dL_dhi * dhi_dz1;
gW1[i][0] += dL_dz1 * x1;
gW1[i][1] += dL_dz1 * x2;
gb1[i] += dL_dz1;
}
}
double scale = 1.0 / (double)batch;
for (int i = 0; i < H; i++) {
W2[i] -= lr * gW2[i] * scale;
for (int j = 0; j < 2; j++) W1[i][j] -= lr * gW1[i][j] * scale;
b1[i] -= lr * gb1[i] * scale;
}
b2 -= lr * gb2 * scale;
}
}
};