diff options
author | Iurii Plugatariov <[email protected]> | 2024-07-08 23:56:57 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-07-08 23:56:57 +0200 |
commit | 3335e781107f88646d9403357411303d4f8f0a4f (patch) | |
tree | d4e77fe62c5b0bc87632485f1968fb6c87bc013d | |
parent | c7b32cd10370f512bad154d0282f78004ffa4c42 (diff) | |
parent | aafe02ddf1931659b9d42e403d8fcb37450afb43 (diff) | |
download | tinkerbunk-3335e781107f88646d9403357411303d4f8f0a4f.tar.gz |
Merge pull request #3 from makefunstuff/perceptron
Learning simple neural nets just for fun
-rw-r--r-- | build.zig | 92 | ||||
-rw-r--r-- | src/monkey_brain/main.zig | 5 | ||||
-rw-r--r-- | src/monkey_brain/multi_layer_perceptron.zig | 0 | ||||
-rw-r--r-- | src/monkey_brain/perceptron.zig | 83 | ||||
-rw-r--r-- | src/monkey_brain/test.zig | 5 |
5 files changed, 140 insertions, 45 deletions
diff --git a/build.zig b/build.zig index 5a00841..dbaf428 100644 --- a/build.zig +++ b/build.zig @@ -15,61 +15,63 @@ 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, + }); - // linking for linux only - // TODO: link for mac later - 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")); - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). - b.installArtifact(exe); + b.installArtifact(exe); - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. - const run_cmd = b.addRunArtifact(exe); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. - 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); - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". - 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"), + // monkey brains + const monkey_brain = b.addExecutable(.{ + .name = "monkey_brain", + .root_source_file = b.path("src/monkey_brain/main.zig"), .target = target, .optimize = optimize, }); + b.installArtifact(monkey_brain); + + const monkey_brain_run = b.addRunArtifact(monkey_brain); + monkey_brain_run.step.dependOn(b.getInstallStep()); + + const monkey_run_step = b.step("monkey_run", "Run the monkey brain"); + monkey_run_step.dependOn(&monkey_brain_run.step); - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const monkey_test_exec = b.addTest(.{ .root_source_file = b.path("src/monkey_brain/test.zig"), .target = target, .optimize = optimize }); - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_exe_unit_tests.step); + const monkey_test_run = b.addRunArtifact(monkey_test_exec); + const monkey_test_step = b.step("monkey_test", "Run monkey brain cells test"); + monkey_test_step.dependOn(&monkey_test_run.step); } diff --git a/src/monkey_brain/main.zig b/src/monkey_brain/main.zig new file mode 100644 index 0000000..611dcd1 --- /dev/null +++ b/src/monkey_brain/main.zig @@ -0,0 +1,5 @@ +const perceptron = @import("perceptron.zig"); + +pub fn main() !void { + 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 --- /dev/null +++ b/src/monkey_brain/multi_layer_perceptron.zig diff --git a/src/monkey_brain/perceptron.zig b/src/monkey_brain/perceptron.zig new file mode 100644 index 0000000..893bb16 --- /dev/null +++ b/src/monkey_brain/perceptron.zig @@ -0,0 +1,83 @@ +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; + +// 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)); +} + +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 new file mode 100644 index 0000000..4d4a04b --- /dev/null +++ b/src/monkey_brain/test.zig @@ -0,0 +1,5 @@ +pub const perceptron = @import("perceptron.zig"); + +test { + @import("std").testing.refAllDecls(@This()); +} |