From 793e7c041dbc94a478ae1b6d619c8ba22b8e6c02 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Mon, 8 Jul 2024 20:54:07 +0300 Subject: Setting up the build --- src/monkey_brain/main.zig | 5 +++++ src/monkey_brain/test.zig | 0 2 files changed, 5 insertions(+) create mode 100644 src/monkey_brain/main.zig create mode 100644 src/monkey_brain/test.zig (limited to 'src') diff --git a/src/monkey_brain/main.zig b/src/monkey_brain/main.zig new file mode 100644 index 0000000..d488c54 --- /dev/null +++ b/src/monkey_brain/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() !void { + std.debug.print("uga buga", .{}); +} diff --git a/src/monkey_brain/test.zig b/src/monkey_brain/test.zig new file mode 100644 index 0000000..e69de29 -- cgit 1.4.1-2-gfad0 From c01d9802552e44bf228de141c572d1e8419a16a9 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Tue, 9 Jul 2024 00:23:25 +0300 Subject: wip --- src/monkey_brain/main.zig | 59 ++++++++++++++++++++++++++++++++++++++++++++++- src/monkey_brain/test.zig | 5 ++++ 2 files changed, 63 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/monkey_brain/main.zig b/src/monkey_brain/main.zig index d488c54..52ff9e5 100644 --- a/src/monkey_brain/main.zig +++ b/src/monkey_brain/main.zig @@ -1,5 +1,62 @@ const std = @import("std"); +const testing = std.testing; + +const input_size: usize = 2; +const training_set_size: usize = 4; +const learning_rate: f64 = 0.1; +const epochs: u64 = 100 * 1000; + +fn sigmoid(x: f64) f64 { + return 1.0 / (1.0 + std.math.exp(-x)); +} + +fn sigmoid_derivative(output: f64) f64 { + return output * (1.0 - output); +} + +fn predict(weights: [input_size]f64, bias: f64, inputs: [input_size]f64) f64 { + var total: f64 = 0.0; + for (0..input_size) |i| { + total += weights[i] * inputs[i]; + } + total += bias; + return sigmoid(total); +} + +fn train(weights: *[input_size]f64, bias: *f64, training_data: [training_set_size][input_size]f64, labels: [training_set_size]f64) void { + for (0..epochs) |_| { + for (0..training_set_size) |i| { + const prediction = predict(weights.*, bias.*, training_data[i]); + const err = labels[i] - prediction; + const adjustment = err * sigmoid_derivative(prediction); + + for (0..input_size) |j| { + weights[j] += learning_rate * adjustment * training_data[i][j]; + } + bias.* += learning_rate * adjustment; + } + } +} pub fn main() !void { - std.debug.print("uga buga", .{}); + const w1 = std.crypto.random.float(f64); + const w2 = std.crypto.random.float(f64); + + var weights: [input_size]f64 = .{ w1, w2 }; + var bias: f64 = 0.0; + + const training_data: [training_set_size][input_size]f64 = .{ .{ 0, 0 }, .{ 0, 1 }, .{ 1, 0 }, .{ 1, 1 } }; + + const labels: [training_set_size]f64 = .{ 0, 0, 0, 1 }; + + train(&weights, &bias, training_data, labels); + + for (0..training_set_size) |i| { + const prediction = predict(weights, bias, training_data[i]); + std.log.info("Input {} {}, Predicted output: {}", .{ training_data[i][0], training_data[i][1], prediction }); + } +} + +test "hello" { + try testing.expect(true); } diff --git a/src/monkey_brain/test.zig b/src/monkey_brain/test.zig index e69de29..7a71043 100644 --- a/src/monkey_brain/test.zig +++ b/src/monkey_brain/test.zig @@ -0,0 +1,5 @@ +pub const main = @import("main.zig"); + +test { + @import("std").testing.refAllDecls(@This()); +} -- cgit 1.4.1-2-gfad0 From 82c57cbd54bc20c5a6b1f1a12f42db8018c0f07a Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Tue, 9 Jul 2024 00:44:06 +0300 Subject: monkey brain one --- build.zig | 62 +++++++++++++++++++++--------------------- src/monkey_brain/main.zig | 69 ++++++++++++++++++++++++++++++----------------- 2 files changed, 76 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/build.zig b/build.zig index cd4e0ae..dbaf428 100644 --- a/build.zig +++ b/build.zig @@ -15,42 +15,44 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const exe = b.addExecutable(.{ - .name = "tinkerbunk", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); + if (target.query.os_tag == .linux) { + const exe = b.addExecutable(.{ + .name = "tinkerbunk", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); - exe.linkSystemLibrary("SDL2"); - exe.linkSystemLibrary("mpg123"); - exe.linkSystemLibrary("asound"); - exe.linkLibC(); - exe.addCSourceFile(.{ .file = b.path("csrc/cbrr.c"), .flags = &.{} }); - exe.addIncludePath(b.path("./csrc")); + exe.linkSystemLibrary("SDL2"); + exe.linkSystemLibrary("mpg123"); + exe.linkSystemLibrary("asound"); + exe.linkLibC(); + exe.addCSourceFile(.{ .file = b.path("csrc/cbrr.c"), .flags = &.{} }); + exe.addIncludePath(b.path("./csrc")); - b.installArtifact(exe); + b.installArtifact(exe); - const run_cmd = b.addRunArtifact(exe); - run_cmd.step.dependOn(b.getInstallStep()); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` - if (b.args) |args| { - run_cmd.addArgs(args); - } + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); - const exe_unit_tests = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_exe_unit_tests.step); + const exe_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_exe_unit_tests.step); + } // monkey brains const monkey_brain = b.addExecutable(.{ diff --git a/src/monkey_brain/main.zig b/src/monkey_brain/main.zig index 52ff9e5..5d0e616 100644 --- a/src/monkey_brain/main.zig +++ b/src/monkey_brain/main.zig @@ -1,13 +1,14 @@ const std = @import("std"); const testing = std.testing; +const math = std.math; const input_size: usize = 2; const training_set_size: usize = 4; const learning_rate: f64 = 0.1; -const epochs: u64 = 100 * 1000; +const epochs: u64 = 1000000; fn sigmoid(x: f64) f64 { - return 1.0 / (1.0 + std.math.exp(-x)); + return 1.0 / (1.0 + math.exp(-x)); } fn sigmoid_derivative(output: f64) f64 { @@ -15,23 +16,21 @@ fn sigmoid_derivative(output: f64) f64 { } fn predict(weights: [input_size]f64, bias: f64, inputs: [input_size]f64) f64 { - var total: f64 = 0.0; - for (0..input_size) |i| { - total += weights[i] * inputs[i]; + var total: f64 = bias; + for (inputs, 0..) |input, i| { + total += weights[i] * input; } - total += bias; return sigmoid(total); } fn train(weights: *[input_size]f64, bias: *f64, training_data: [training_set_size][input_size]f64, labels: [training_set_size]f64) void { for (0..epochs) |_| { - for (0..training_set_size) |i| { - const prediction = predict(weights.*, bias.*, training_data[i]); - const err = labels[i] - prediction; + for (training_data, labels) |inputs, label| { + const prediction = predict(weights.*, bias.*, inputs); + const err = label - prediction; const adjustment = err * sigmoid_derivative(prediction); - - for (0..input_size) |j| { - weights[j] += learning_rate * adjustment * training_data[i][j]; + for (inputs, 0..) |input, j| { + weights[j] += learning_rate * adjustment * input; } bias.* += learning_rate * adjustment; } @@ -39,24 +38,44 @@ fn train(weights: *[input_size]f64, bias: *f64, training_data: [training_set_siz } pub fn main() !void { - const w1 = std.crypto.random.float(f64); - const w2 = std.crypto.random.float(f64); - - var weights: [input_size]f64 = .{ w1, w2 }; - var bias: f64 = 0.0; - - const training_data: [training_set_size][input_size]f64 = .{ .{ 0, 0 }, .{ 0, 1 }, .{ 1, 0 }, .{ 1, 1 } }; + var weights = [_]f64{ std.crypto.random.float(f64), std.crypto.random.float(f64) }; + var bias: f64 = std.crypto.random.float(f64); - const labels: [training_set_size]f64 = .{ 0, 0, 0, 1 }; + const training_data = [_][input_size]f64{ + .{ 0, 0 }, + .{ 0, 1 }, + .{ 1, 0 }, + .{ 1, 1 }, + }; + const labels = [_]f64{ 0, 1, 1, 1 }; // OR operation train(&weights, &bias, training_data, labels); - for (0..training_set_size) |i| { - const prediction = predict(weights, bias, training_data[i]); - std.log.info("Input {} {}, Predicted output: {}", .{ training_data[i][0], training_data[i][1], prediction }); + std.debug.print("Trained weights: {d}, {d}\n", .{ weights[0], weights[1] }); + std.debug.print("Trained bias: {d}\n", .{bias}); + + for (training_data, labels) |inputs, expected| { + const prediction = predict(weights, bias, inputs); + std.debug.print("Input: {d}, {d}, Predicted: {d:.4}, Expected: {d}\n", .{ inputs[0], inputs[1], prediction, expected }); } } -test "hello" { - try testing.expect(true); +test "OR gate" { + var weights = [_]f64{ 0, 0 }; + var bias: f64 = 0; + + const training_data = [_][input_size]f64{ + .{ 0, 0 }, + .{ 0, 1 }, + .{ 1, 0 }, + .{ 1, 1 }, + }; + const labels = [_]f64{ 0, 1, 1, 1 }; + + train(&weights, &bias, training_data, labels); + + for (training_data, labels) |inputs, expected| { + const prediction = predict(weights, bias, inputs); + try testing.expect((prediction - expected) < 0.1); + } } -- cgit 1.4.1-2-gfad0 From 8f2c7c513bc54bf127ff2ab00da1694fb981f442 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Tue, 9 Jul 2024 00:49:37 +0300 Subject: refactoring --- src/monkey_brain/main.zig | 80 +--------------------------- src/monkey_brain/multi_layer_perceptron.zig | 0 src/monkey_brain/perceptron.zig | 81 +++++++++++++++++++++++++++++ src/monkey_brain/test.zig | 2 +- 4 files changed, 84 insertions(+), 79 deletions(-) create mode 100644 src/monkey_brain/multi_layer_perceptron.zig create mode 100644 src/monkey_brain/perceptron.zig (limited to 'src') diff --git a/src/monkey_brain/main.zig b/src/monkey_brain/main.zig index 5d0e616..611dcd1 100644 --- a/src/monkey_brain/main.zig +++ b/src/monkey_brain/main.zig @@ -1,81 +1,5 @@ -const std = @import("std"); -const testing = std.testing; -const math = std.math; - -const input_size: usize = 2; -const training_set_size: usize = 4; -const learning_rate: f64 = 0.1; -const epochs: u64 = 1000000; - -fn sigmoid(x: f64) f64 { - return 1.0 / (1.0 + math.exp(-x)); -} - -fn sigmoid_derivative(output: f64) f64 { - return output * (1.0 - output); -} - -fn predict(weights: [input_size]f64, bias: f64, inputs: [input_size]f64) f64 { - var total: f64 = bias; - for (inputs, 0..) |input, i| { - total += weights[i] * input; - } - return sigmoid(total); -} - -fn train(weights: *[input_size]f64, bias: *f64, training_data: [training_set_size][input_size]f64, labels: [training_set_size]f64) void { - for (0..epochs) |_| { - for (training_data, labels) |inputs, label| { - const prediction = predict(weights.*, bias.*, inputs); - const err = label - prediction; - const adjustment = err * sigmoid_derivative(prediction); - for (inputs, 0..) |input, j| { - weights[j] += learning_rate * adjustment * input; - } - bias.* += learning_rate * adjustment; - } - } -} +const perceptron = @import("perceptron.zig"); pub fn main() !void { - var weights = [_]f64{ std.crypto.random.float(f64), std.crypto.random.float(f64) }; - var bias: f64 = std.crypto.random.float(f64); - - const training_data = [_][input_size]f64{ - .{ 0, 0 }, - .{ 0, 1 }, - .{ 1, 0 }, - .{ 1, 1 }, - }; - const labels = [_]f64{ 0, 1, 1, 1 }; // OR operation - - train(&weights, &bias, training_data, labels); - - std.debug.print("Trained weights: {d}, {d}\n", .{ weights[0], weights[1] }); - std.debug.print("Trained bias: {d}\n", .{bias}); - - for (training_data, labels) |inputs, expected| { - const prediction = predict(weights, bias, inputs); - std.debug.print("Input: {d}, {d}, Predicted: {d:.4}, Expected: {d}\n", .{ inputs[0], inputs[1], prediction, expected }); - } -} - -test "OR gate" { - var weights = [_]f64{ 0, 0 }; - var bias: f64 = 0; - - const training_data = [_][input_size]f64{ - .{ 0, 0 }, - .{ 0, 1 }, - .{ 1, 0 }, - .{ 1, 1 }, - }; - const labels = [_]f64{ 0, 1, 1, 1 }; - - train(&weights, &bias, training_data, labels); - - for (training_data, labels) |inputs, expected| { - const prediction = predict(weights, bias, inputs); - try testing.expect((prediction - expected) < 0.1); - } + try perceptron.demo(); } diff --git a/src/monkey_brain/multi_layer_perceptron.zig b/src/monkey_brain/multi_layer_perceptron.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/monkey_brain/perceptron.zig b/src/monkey_brain/perceptron.zig new file mode 100644 index 0000000..c65fc41 --- /dev/null +++ b/src/monkey_brain/perceptron.zig @@ -0,0 +1,81 @@ +const std = @import("std"); +const testing = std.testing; +const math = std.math; + +const input_size: usize = 2; +const training_set_size: usize = 4; +const learning_rate: f64 = 0.1; +const epochs: u64 = 1000000; + +fn sigmoid(x: f64) f64 { + return 1.0 / (1.0 + math.exp(-x)); +} + +fn sigmoid_derivative(output: f64) f64 { + return output * (1.0 - output); +} + +fn predict(weights: [input_size]f64, bias: f64, inputs: [input_size]f64) f64 { + var total: f64 = bias; + for (inputs, 0..) |input, i| { + total += weights[i] * input; + } + return sigmoid(total); +} + +fn train(weights: *[input_size]f64, bias: *f64, training_data: [training_set_size][input_size]f64, labels: [training_set_size]f64) void { + for (0..epochs) |_| { + for (training_data, labels) |inputs, label| { + const prediction = predict(weights.*, bias.*, inputs); + const err = label - prediction; + const adjustment = err * sigmoid_derivative(prediction); + for (inputs, 0..) |input, j| { + weights[j] += learning_rate * adjustment * input; + } + bias.* += learning_rate * adjustment; + } + } +} + +pub fn demo() !void { + var weights = [_]f64{ std.crypto.random.float(f64), std.crypto.random.float(f64) }; + var bias: f64 = std.crypto.random.float(f64); + + const training_data = [_][input_size]f64{ + .{ 0, 0 }, + .{ 0, 1 }, + .{ 1, 0 }, + .{ 1, 1 }, + }; + const labels = [_]f64{ 0, 1, 1, 1 }; // OR operation + + train(&weights, &bias, training_data, labels); + + std.debug.print("Trained weights: {d}, {d}\n", .{ weights[0], weights[1] }); + std.debug.print("Trained bias: {d}\n", .{bias}); + + for (training_data, labels) |inputs, expected| { + const prediction = predict(weights, bias, inputs); + std.debug.print("Input: {d}, {d}, Predicted: {d:.4}, Expected: {d}\n", .{ inputs[0], inputs[1], prediction, expected }); + } +} + +test "OR gate" { + var weights = [_]f64{ 0, 0 }; + var bias: f64 = 0; + + const training_data = [_][input_size]f64{ + .{ 0, 0 }, + .{ 0, 1 }, + .{ 1, 0 }, + .{ 1, 1 }, + }; + const labels = [_]f64{ 0, 1, 1, 1 }; + + train(&weights, &bias, training_data, labels); + + for (training_data, labels) |inputs, expected| { + const prediction = predict(weights, bias, inputs); + try testing.expect((prediction - expected) < 0.1); + } +} diff --git a/src/monkey_brain/test.zig b/src/monkey_brain/test.zig index 7a71043..4d4a04b 100644 --- a/src/monkey_brain/test.zig +++ b/src/monkey_brain/test.zig @@ -1,4 +1,4 @@ -pub const main = @import("main.zig"); +pub const perceptron = @import("perceptron.zig"); test { @import("std").testing.refAllDecls(@This()); -- cgit 1.4.1-2-gfad0 From aafe02ddf1931659b9d42e403d8fcb37450afb43 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Tue, 9 Jul 2024 00:55:59 +0300 Subject: annotate sigmoid --- src/monkey_brain/perceptron.zig | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/monkey_brain/perceptron.zig b/src/monkey_brain/perceptron.zig index c65fc41..893bb16 100644 --- a/src/monkey_brain/perceptron.zig +++ b/src/monkey_brain/perceptron.zig @@ -7,6 +7,8 @@ const training_set_size: usize = 4; const learning_rate: f64 = 0.1; const epochs: u64 = 1000000; +// https://en.wikipedia.org/wiki/Sigmoid_function - more details +// https://www.youtube.com/watch?v=TPqr8t919YM fn sigmoid(x: f64) f64 { return 1.0 / (1.0 + math.exp(-x)); } -- cgit 1.4.1-2-gfad0