@@ -6,8 +6,6 @@ const std = @import("std");
66const kiwigrad = @import ("kiwigrad" );
77const zbench = @import ("zbench" );
88
9- const print = std .debug .print ;
10-
119pub fn main () ! void {
1210 const stdout_file = std .io .getStdOut ().writer ();
1311 var bw = std .io .bufferedWriter (stdout_file );
@@ -19,43 +17,53 @@ pub fn main() !void {
1917 const ValueType = kiwigrad .engine .Value (f64 );
2018 const NeuronType = kiwigrad .nn .Neuron (f64 );
2119 const LayerType = kiwigrad .nn .Layer (f64 );
22- // const MLPType = kiwigrad.nn.MLP;
20+ const MLPType = kiwigrad .nn .MLP ( f64 ) ;
2321
2422 // Initialize allocators and components
2523 ValueType .init (alloc );
2624 NeuronType .init (alloc );
2725 LayerType .init (alloc );
26+ MLPType .init (alloc );
2827 defer {
2928 ValueType .deinit ();
3029 NeuronType .deinit ();
3130 LayerType .deinit ();
32- // MLPType.deinit();
31+ MLPType .deinit ();
3332 }
3433
34+ var sizes = [_ ]usize { 3 , 2 , 1 };
35+
3536 // Initialize the neuron
36- const neuron = NeuronType .new (3 );
37+ const mlp = MLPType .new (sizes . len - 1 , sizes [0 .. ] );
3738
38- // Create sample input data
39- var input_data = [_ ]* ValueType {
40- ValueType .new (1.0 ) ,
41- ValueType .new (2.0 ) ,
42- ValueType .new (3.0 ) ,
39+ const inputs = [ _ ][ 3 ] * ValueType {
40+ [_ ]* ValueType { ValueType . new ( 2 ), ValueType . new ( 3 ), ValueType . new ( -1 ) },
41+ [ _ ] * ValueType { ValueType .new (3 ), ValueType . new ( -1 ), ValueType . new ( 0.5 ) } ,
42+ [ _ ] * ValueType { ValueType .new (0.5 ), ValueType . new ( 1 ), ValueType . new ( 1 ) } ,
43+ [ _ ] * ValueType { ValueType .new (1 ), ValueType . new ( 2 ), ValueType . new ( 3 ) } ,
4344 };
4445
45- // Forward pass through the layer
46- const output = neuron .forward (input_data [0.. ]);
46+ mlp .draw_graph ("assets/img/mlp" , stdout );
47+
48+ for (inputs ) | in | {
49+ // Forward pass through the layer
50+ const output = mlp .forward (@constCast (& in ));
51+ stdout .print ("{d:7.4} " , .{output [0 ].data }) catch unreachable ;
52+ for (output ) | o | {
53+ _ = o .draw_graph ("assets/img/perceptron" , stdout );
54+ }
55+ }
4756
48- // outputs now contains 2 ValueType pointers (one for each neuron)
49- print ("Layer output: {d:.4}\n " , .{output .data });
57+ // // outputs now contains 2 ValueType pointers (one for each neuron)
58+ // print("Layer output: {d:.4}\n", .{output.data});
5059
51- print ("output.data: {d:.4}\n " , .{output .data });
52- print ("output.grad: {d:.4}\n " , .{output .grad });
60+ // print("output.data: {d:.4}\n", .{output.data});
61+ // print("output.grad: {d:.4}\n", .{output.grad});
5362
54- output .backwardPass (alloc );
63+ // output.backwardPass(alloc);
5564
56- print ("output.data: {d:.4}\n " , .{output .data });
57- print ("output.grad: {d:.4}\n " , .{output .grad });
65+ // print("output.data: {d:.4}\n", .{output.data});
66+ // print("output.grad: {d:.4}\n", .{output.grad});
5867
59- output .draw_graph ("assets/img/train" , stdout );
6068 try bw .flush (); // Don't forget to flush!
6169}
0 commit comments