Aggiunta della struttura neuron e tensor

This commit is contained in:
Riccardo Forese 2026-02-02 17:25:04 +01:00
parent 687a24722b
commit 276641b9c6
3 changed files with 93 additions and 19 deletions

View file

@ -1,25 +1,28 @@
const std = @import("std"); const std = @import("std");
const AI_Zig = @import("AI_Zig"); const Neuron = @import("neuron.zig").Neuron;
const Tensor = @import("tensor.zig").Tensor;
pub fn main() !void { pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); // 1. Setup dell'allocatore per la memoria
try AI_Zig.bufferedPrint(); var gpa = std.heap.GeneralPurposeAllocator(.{}){};
} const allocator = gpa.allocator();
defer _ = gpa.deinit();
test "simple test" { // 2. Creiamo un neurone con 3 ingressi
const gpa = std.testing.allocator; var my_neuron = try Neuron.init(allocator, 3);
var list: std.ArrayList(i32) = .empty; defer my_neuron.deinit();
defer list.deinit(gpa);
try list.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "fuzz example" { // 3. Creiamo dei dati di input di esempio (es. [1.0, 0.5, -2.0])
const Context = struct { var inputs = try Tensor.init(allocator, &[_]usize{3});
fn testOne(context: @This(), input: []const u8) anyerror!void { defer inputs.deinit();
_ = context; inputs.data[0] = 1.0;
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); inputs.data[1] = 0.5;
} inputs.data[2] = -2.0;
};
try std.testing.fuzz(Context{}, Context.testOne, .{}); // 4. Eseguiamo il calcolo
const output = my_neuron.forward(inputs);
std.debug.print("Input: {any}\n", .{inputs.data});
std.debug.print("Pesi del neurone: {any}\n", .{my_neuron.weights.data});
std.debug.print("Risultato (Output): {d:.4}\n", .{output});
} }

43
src/neuron.zig Normal file
View file

@ -0,0 +1,43 @@
const std = @import("std");
const Tensor = @import("tensor.zig").Tensor;
pub const Neuron = struct {
weights: Tensor,
bias: f32,
pub fn init(allocator: std.mem.Allocator, input_size: usize) !Neuron {
const weights = try Tensor.init(allocator, &[_]usize{input_size});
var seed: u64 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
var prng = std.Random.DefaultPrng.init(seed);
const rand = prng.random();
for (weights.data) |*w| {
w.* = rand.float(f32) * 2.0 - 1.0;
}
return Neuron{
.weights = weights,
.bias = 0.0,
};
}
pub fn deinit(self: *Neuron) void {
self.weights.deinit();
}
pub fn forward(self: *Neuron, inputs: Tensor) f32 {
std.debug.assert(inputs.data.len == self.weights.data.len);
var sum: f32 = 0.0;
for (inputs.data, self.weights.data) |x, w| {
sum += x * w;
}
const z = sum + self.bias;
return if (z > 0) z else 0;
}
};

28
src/tensor.zig Normal file
View file

@ -0,0 +1,28 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const Tensor = struct {
data: []f32,
shape: []const usize,
allocator: Allocator,
pub fn init(allocator: Allocator, shape: []const usize) !Tensor {
var size: usize = 1;
for (shape) |dim| {
size *= dim;
}
const data = try allocator.alloc(f32, size);
@memset(data, 0);
return Tensor{
.data = data,
.shape = shape,
.allocator = allocator,
};
}
pub fn deinit(self: *Tensor) void {
self.allocator.free(self.data);
}
};