Skip to content

Commit ed3eb28

Browse files
committed
Cleaner training script
1 parent 081c714 commit ed3eb28

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

examples/train.zig

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
const std = @import("std");
66
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
77
const kiwigrad = @import("kiwigrad");
8-
const zbench = @import("zbench");
9-
10-
const ValueType = kiwigrad.engine.Value;
11-
const NeuronType = kiwigrad.nn.Neuron;
12-
const LayerType = kiwigrad.nn.Layer;
138

149
/// Write the computational graph to a Graphviz file
1510
pub fn draw_graph(comptime T: type, graph: *kiwigrad.engine.Value(T), name: []const u8, writer: anytype) !void {
@@ -33,24 +28,39 @@ pub fn main() !void {
3328
const stdout = bw.writer();
3429
const alloc = std.heap.page_allocator;
3530

36-
// Initialize the value allocator
37-
const Value = ValueType(f64);
38-
Value.init(alloc);
39-
defer Value.deinit();
31+
// Initialize the required components
32+
const ValueType = kiwigrad.engine.Value(f64);
33+
const NeuronType = kiwigrad.nn.Neuron(f64);
34+
const LayerType = kiwigrad.nn.Layer(f64);
35+
// const MLPType = kiwigrad.nn.MLP;
4036

41-
// Initialize the neuron allocator
42-
const Neuron = NeuronType(f64);
43-
Neuron.init(alloc);
44-
defer Neuron.deinit();
37+
// Initialize allocators and components
38+
ValueType.init(alloc);
39+
NeuronType.init(alloc);
40+
LayerType.init(alloc);
41+
defer {
42+
ValueType.deinit();
43+
NeuronType.deinit();
44+
LayerType.deinit();
45+
// MLPType.deinit();
46+
}
4547

4648
// Initialize the neuron
47-
const n = Neuron.new(2);
49+
const neuron = NeuronType.new(3);
50+
4851
// Create sample input data
49-
var input_data = [_]*kiwigrad.engine.Value(f64){
50-
kiwigrad.engine.Value(f64).new(1.0),
51-
kiwigrad.engine.Value(f64).new(2.0),
52+
var input_data = [_]*ValueType{
53+
ValueType.new(1.0),
54+
ValueType.new(2.0),
55+
ValueType.new(3.0),
5256
};
53-
const output = n.forward(input_data[0..]);
57+
58+
// Forward pass through the layer
59+
const output = neuron.forward(input_data[0..]);
60+
61+
// outputs now contains 2 ValueType pointers (one for each neuron)
62+
std.debug.print("Layer output: {d}\n", .{output.data});
63+
5464
try draw_graph(f64, output, "n_f64", stdout);
5565
try bw.flush(); // Don't forget to flush!
5666
}

0 commit comments

Comments
 (0)