micrograd.zig/src/main.zig

66 lines
1.8 KiB
Zig

const std = @import("std");
const print = std.debug.print;
const engine = @import("engine.zig");
const nn = @import("nn.zig");
const ValueType = engine.Value;
const BackpropType = engine.Backprop;
pub fn main() !void {
var gpa = std.heap.DebugAllocator(.{}){};
const alloc = gpa.allocator();
const Value = ValueType(f32);
// The MLP owns the Value arena: init() calls Value.init, deinit() frees it.
const Net = nn.MLP(f32, &.{ 3, 4, 4, 1 });
var net = Net.init(alloc);
defer net.deinit();
var backprop = BackpropType(Value).init(alloc);
defer backprop.deinit();
// Karpathy's tiny binary dataset (targets are -1 / +1).
const xs = [_][3]f32{
.{ 2, 3, -1 },
.{ 3, -1, 0.5 },
.{ 0.5, 1, 1 },
.{ 1, 1, -1 },
};
const ys = [_]f32{ 1, -1, -1, 1 };
const lr: f32 = 0.05;
var epoch: usize = 0;
while (epoch < 100) : (epoch += 1) {
// Build a single summed squared-error loss graph over the batch.
var loss = Value.new(0);
for (xs, ys) |x, y| {
var xv: [3]*Value = undefined;
for (x, 0..) |xi, i| xv[i] = Value.new(xi);
const pred = net.forward(xv);
loss = loss.add(pred[0].sub(y).pow(2));
}
net.zeroGrad();
backprop.backprop(loss);
for (net.parameters()) |p| p.data -= lr * p.grad;
if (epoch % 10 == 0) {
print("epoch {d:>3}: loss {d:.6}\n", .{ epoch, loss.data });
}
}
// Final predictions should track the targets (+/-1).
print("\npredictions:\n", .{});
for (xs, ys) |x, y| {
var xv: [3]*Value = undefined;
for (x, 0..) |xi, i| xv[i] = Value.new(xi);
print(" pred {d:>7.4} target {d:>5.1}\n", .{ net.forward(xv)[0].data, y });
}
}
test {
_ = @import("engine.zig");
_ = @import("nn.zig");
}