Initial commit
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 21 Jan 2021 02:27:23 +0000 (20:27 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 21 Jan 2021 02:27:23 +0000 (20:27 -0600)
.gitignore [new file with mode: 0644]
src/mnist.onyx [new file with mode: 0644]
src/neuralnet.onyx [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..4046358
--- /dev/null
@@ -0,0 +1,4 @@
+*.wasm
+data/
+*.sublime-workspace
+*.sublime-project
diff --git a/src/mnist.onyx b/src/mnist.onyx
new file mode 100644 (file)
index 0000000..c22ab2b
--- /dev/null
@@ -0,0 +1,15 @@
+#load "core/std/wasi"
+
+#load_path "src"
+#load "neuralnet"
+
+use package core
+
+
+
+// Load the data
+// Feed forward neural net
+
+main :: (args: [] cstr) {
+    println(sigmoid(-4.0f));
+}
\ No newline at end of file
diff --git a/src/neuralnet.onyx b/src/neuralnet.onyx
new file mode 100644 (file)
index 0000000..f354805
--- /dev/null
@@ -0,0 +1,50 @@
+use package core
+
+// To easily change to 64-bit floats if needed.
+float :: #type f32;
+
+
+NeuralNet :: struct {
+    layers  : [] Layer;
+
+    // CLEANUP: Move these to core.alloc, so the nesting isn't nearly as terrible.
+    layer_arena : alloc.arena.ArenaState;
+}
+
+make_neural_network :: (layer_sizes: ..i32) -> NeuralNet {
+    net : NeuralNet;
+
+    net.layer_arena = alloc.arena.make(alloc.heap_allocator, 64 * 1024 * 1024); // 64 MiB
+    layer_allocator := alloc.arena.make_allocator(^net.layer_arena);
+
+    net.layers = memory.make_slice(Layer, layer_sizes.count);
+}
+
+
+
+
+
+Layer :: struct {
+    neurons : [] float;
+    weights : [] float;
+}
+
+layer_forward :: (use layer: ^Layer, prev_layer: ^Layer) {
+    for i: 0 .. neurons.count {
+        neurons[i] = 0;
+        for j: 0 .. weights.count {
+            neurons[i] += prev_layer.neurons[j] * weights[j];
+        }
+
+        neurons[i] = sigmoid(neurons[i]);
+    }
+}
+
+
+
+
+sigmoid :: (x: float) -> float {
+    ex := math.exp(x);
+    return ex / (1 + ex);
+}
+