about summary refs log tree commit diff
path: root/src/monkey_brain/neural_network
diff options
context:
space:
mode:
authormakefunstuff <[email protected]>2024-07-11 00:05:11 +0200
committermakefunstuff <[email protected]>2024-07-11 00:05:11 +0200
commitfd3a727f4b4f819178225cdd87d8788b85c4b86c (patch)
tree58c6ee139f7768ec739477354af0faf001b2d40d /src/monkey_brain/neural_network
parente5a064e82fe7d36f4423abdf798f9f1f0a90ae0e (diff)
downloadtinkerbunk-fd3a727f4b4f819178225cdd87d8788b85c4b86c.tar.gz
some fixes and dummy neuron
Diffstat (limited to 'src/monkey_brain/neural_network')
-rw-r--r--src/monkey_brain/neural_network/layer.zig5
-rw-r--r--src/monkey_brain/neural_network/neuron.zig56
2 files changed, 61 insertions, 0 deletions
diff --git a/src/monkey_brain/neural_network/layer.zig b/src/monkey_brain/neural_network/layer.zig
new file mode 100644
index 0000000..7b2bf6e
--- /dev/null
+++ b/src/monkey_brain/neural_network/layer.zig
@@ -0,0 +1,5 @@
+const std = @import("std");
+const math = std.math;
+const Random = std.Random;
+const ArrayList = std.ArrayList;
+const testing = std.testing;
diff --git a/src/monkey_brain/neural_network/neuron.zig b/src/monkey_brain/neural_network/neuron.zig
new file mode 100644
index 0000000..a26b787
--- /dev/null
+++ b/src/monkey_brain/neural_network/neuron.zig
@@ -0,0 +1,56 @@
+const std = @import("std");
+const math = std.math;
+const Random = std.Random;
+const ArrayList = std.ArrayList;
+const testing = std.testing;
+
+pub const Neuron = struct {
+    weights: ArrayList(f64),
+    bias: f64,
+
+    pub fn init(allocator: std.mem.Allocator, num_inputs: usize) !Neuron {
+        var weights = ArrayList(f64).init(allocator);
+        var prng = Random.DefaultPrng.init(blk: {
+            const seed: u64 = @intCast(std.time.milliTimestamp());
+            break :blk seed;
+        });
+
+        var random = prng.random();
+        var i: usize = 0;
+
+        while (i < num_inputs) : (i += 1) {
+            try weights.append(random.float(f64) * 2 - 1);
+        }
+
+        return Neuron{
+            .weights = weights,
+            .bias = random.float(f64) * 2 - 1,
+        };
+    }
+
+    pub fn deinit(self: *Neuron) void {
+        self.weights.deinit();
+    }
+
+    pub fn activate(self: *const Neuron, inputs: []const f64) f64 {
+        var sum: f64 = self.bias;
+        for (self.weights.items, 0..) |weight, i| {
+            sum += inputs[i] * weight;
+        }
+        return sigmoid(sum);
+    }
+};
+
+fn sigmoid(x: f64) f64 {
+    return 1.0 / (1.0 + math.exp(-x));
+}
+
+test "Neuron initialization" {
+    const allocator = testing.allocator;
+    var neuron = try Neuron.init(allocator, 2);
+    defer neuron.deinit();
+
+    std.debug.print("Testing neuron", .{});
+    try testing.expect(neuron.weights.items.len == 2);
+    try testing.expect(neuron.bias >= -1 and neuron.bias <= 1);
+}