-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.zig
More file actions
445 lines (414 loc) · 15.3 KB
/
Copy pathautoencoder.zig
File metadata and controls
445 lines (414 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const std = @import("std");
const nn = @import("nn");
const image_side: usize = 4;
const feature_count: usize = image_side * image_side;
const prototype_count: usize = 4;
const train_samples: usize = 128;
const test_samples: usize = 32;
const default_steps: usize = 300;
const default_seed: u64 = 42;
const widths = [_]usize{ feature_count, 8, 2, 8, feature_count };
const Options = struct {
backend: nn.DevicePreference = .auto,
steps: usize = default_steps,
seed: u64 = default_seed,
};
const Dataset = struct {
allocator: std.mem.Allocator,
noisy: []f32,
clean: []f32,
labels: []usize,
fn deinit(self: *Dataset) void {
self.allocator.free(self.noisy);
self.allocator.free(self.clean);
self.allocator.free(self.labels);
self.* = undefined;
}
};
const Metrics = struct {
mse: f32,
pixel_accuracy: f32,
};
const ExperimentResult = struct {
backend: nn.BackendType,
initial: Metrics,
final: Metrics,
update_steps: usize,
training_kernels: usize,
training_readbacks: usize,
latent_centers: [prototype_count][2]f32,
noisy_sample: [feature_count]f32,
clean_sample: [feature_count]f32,
reconstruction: [feature_count]f32,
};
pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
const options = parseArgs(args[1..]) catch |err| {
printUsage();
if (err == error.HelpRequested) return;
return err;
};
const result = try runExperiment(init.gpa, options);
std.debug.print("Denoising autoencoder on {s}\n", .{backendName(result.backend)});
std.debug.print("architecture: 16 -> 8 -> 2 -> 8 -> 16\n", .{});
std.debug.print(
"reconstruction MSE: {d:.6} -> {d:.6}\n" ++
"held-out pixel accuracy: {d:.2}%\n" ++
"updates: {d}, kernels: {d}, training readbacks: {d}\n",
.{
result.initial.mse,
result.final.mse,
result.final.pixel_accuracy * 100,
result.update_steps,
result.training_kernels,
result.training_readbacks,
},
);
std.debug.print("\nlatent centers by pattern:\n", .{});
for (result.latent_centers, 0..) |center, prototype| {
std.debug.print(
" {s: <14} ({d:>7.3}, {d:>7.3})\n",
.{ prototypeName(prototype), center[0], center[1] },
);
}
printImage("noisy input", &result.noisy_sample);
printImage("clean target", &result.clean_sample);
printImage("reconstruction", &result.reconstruction);
}
fn parseArgs(args: []const []const u8) !Options {
var options: Options = .{};
var index: usize = 0;
while (index < args.len) : (index += 1) {
const arg = args[index];
if (std.mem.eql(u8, arg, "--backend")) {
index += 1;
if (index >= args.len) return error.MissingArgument;
options.backend = try parseBackend(args[index]);
} else if (std.mem.eql(u8, arg, "--steps")) {
index += 1;
if (index >= args.len) return error.MissingArgument;
options.steps = try std.fmt.parseInt(usize, args[index], 10);
if (options.steps == 0) return error.InvalidStepCount;
} else if (std.mem.eql(u8, arg, "--seed")) {
index += 1;
if (index >= args.len) return error.MissingArgument;
options.seed = try std.fmt.parseInt(u64, args[index], 10);
} else if (std.mem.eql(u8, arg, "--help")) {
return error.HelpRequested;
} else {
return error.UnknownArgument;
}
}
return options;
}
fn parseBackend(value: []const u8) !nn.DevicePreference {
if (std.mem.eql(u8, value, "cpu")) return .cpu;
if (std.mem.eql(u8, value, "auto")) return .auto;
if (std.mem.eql(u8, value, "metal")) return .metal;
if (std.mem.eql(u8, value, "cuda")) return .cuda;
if (std.mem.eql(u8, value, "rocm")) return .rocm;
return error.UnknownBackend;
}
fn printUsage() void {
std.debug.print(
\\Usage: autoencoder [options]
\\
\\Options:
\\ --backend <name> cpu, auto, metal, cuda, or rocm (default: auto)
\\ --steps <count> full-batch training steps (default: 300)
\\ --seed <number> model and dataset seed (default: 42)
\\ --help show this help
\\
, .{});
}
fn runExperiment(allocator: std.mem.Allocator, options: Options) !ExperimentResult {
var training_data = try generateDataset(allocator, train_samples, options.seed);
defer training_data.deinit();
var testing_data = try generateDataset(
allocator,
test_samples,
options.seed ^ 0x9e3779b97f4a7c15,
);
defer testing_data.deinit();
var device = try nn.Device.init(allocator, options.backend);
defer device.deinit();
var context = nn.ExecutionContext.init(&device);
var training_input = try context.upload(
&.{ train_samples, feature_count },
training_data.noisy,
);
defer training_input.deinit();
var training_target = try context.upload(
&.{ train_samples, feature_count },
training_data.clean,
);
defer training_target.deinit();
var testing_input = try context.upload(
&.{ test_samples, feature_count },
testing_data.noisy,
);
defer testing_input.deinit();
var prng = std.Random.DefaultPrng.init(options.seed);
var model = try nn.Modules.Mlp.init(&context, .{
.widths = &widths,
.hidden_activation = .tanh,
.output_activation = .sigmoid,
}, prng.random());
defer model.deinit();
const parameter_buffer = try allocator.alloc(*nn.Tensor, model.parameterTensorCount());
defer allocator.free(parameter_buffer);
const parameters = try model.parameters(parameter_buffer);
var optimizer = try nn.Training.Optimizer.init(&context, .{
.kind = .adamw,
.learning_rate = 0.025,
.weight_decay = 0.0005,
.max_gradient_norm = 5,
}, parameters);
defer optimizer.deinit();
const gradient_buffer = try allocator.alloc(nn.Tensor, model.parameterTensorCount());
defer allocator.free(gradient_buffer);
const initial = try evaluate(
allocator,
&context,
&model,
testing_input,
testing_data.clean,
);
context.resetStats();
for (0..options.steps) |_| {
try context.beginBatch();
var batch_active = true;
errdefer if (batch_active) context.endBatch() catch {};
var forward = try model.forward(&context, training_input);
defer forward.deinit();
var difference = try context.subtract(forward.output, training_target);
defer difference.deinit();
const element_count = train_samples * feature_count;
var output_gradient = try context.scale(
difference,
2.0 / @as(f32, @floatFromInt(element_count)),
);
defer output_gradient.deinit();
var gradients = try model.backward(&context, &forward.cache, output_gradient);
defer gradients.deinit();
const parameter_gradients = try gradients.parameterGradients(gradient_buffer);
const update = try optimizer.step(&context, parameters, parameter_gradients);
std.debug.assert(update.updated);
try context.endBatch();
batch_active = false;
}
const training_stats = context.stats;
const final = try evaluate(
allocator,
&context,
&model,
testing_input,
testing_data.clean,
);
const sample_index = mostCorruptedSample(testing_data);
const summary = try summarize(
allocator,
&context,
&model,
testing_input,
testing_data.labels,
sample_index,
);
var noisy_sample: [feature_count]f32 = undefined;
var clean_sample: [feature_count]f32 = undefined;
const sample_offset = sample_index * feature_count;
@memcpy(&noisy_sample, testing_data.noisy[sample_offset..][0..feature_count]);
@memcpy(&clean_sample, testing_data.clean[sample_offset..][0..feature_count]);
return .{
.backend = device.backendType(),
.initial = initial,
.final = final,
.update_steps = optimizer.update_steps,
.training_kernels = training_stats.kernels,
.training_readbacks = training_stats.readbacks,
.latent_centers = summary.latent_centers,
.noisy_sample = noisy_sample,
.clean_sample = clean_sample,
.reconstruction = summary.reconstruction,
};
}
const Summary = struct {
latent_centers: [prototype_count][2]f32,
reconstruction: [feature_count]f32,
};
fn summarize(
allocator: std.mem.Allocator,
context: *nn.ExecutionContext,
model: *const nn.Modules.Mlp,
input: nn.Tensor,
labels: []const usize,
sample_index: usize,
) !Summary {
var forward = try model.forward(context, input);
defer forward.deinit();
const output_values = try allocator.alloc(f32, labels.len * feature_count);
defer allocator.free(output_values);
try context.readback(forward.output, output_values);
const bottleneck = forward.cache.hidden_activations[1];
const latent_values = try allocator.alloc(f32, labels.len * 2);
defer allocator.free(latent_values);
try context.readback(bottleneck, latent_values);
var centers = [_][2]f32{.{ 0, 0 }} ** prototype_count;
var counts = [_]usize{0} ** prototype_count;
for (labels, 0..) |label, sample| {
centers[label][0] += latent_values[sample * 2];
centers[label][1] += latent_values[sample * 2 + 1];
counts[label] += 1;
}
for (¢ers, counts) |*center, count| {
const divisor = @as(f32, @floatFromInt(count));
center[0] /= divisor;
center[1] /= divisor;
}
var reconstruction: [feature_count]f32 = undefined;
const sample_offset = sample_index * feature_count;
@memcpy(&reconstruction, output_values[sample_offset..][0..feature_count]);
return .{ .latent_centers = centers, .reconstruction = reconstruction };
}
fn mostCorruptedSample(dataset: Dataset) usize {
var selected: usize = 0;
var selected_error: f32 = -1;
for (0..dataset.labels.len) |sample| {
const offset = sample * feature_count;
var sample_error: f32 = 0;
for (
dataset.noisy[offset..][0..feature_count],
dataset.clean[offset..][0..feature_count],
) |noisy, clean| {
sample_error += @abs(noisy - clean);
}
if (sample_error > selected_error) {
selected = sample;
selected_error = sample_error;
}
}
return selected;
}
fn evaluate(
allocator: std.mem.Allocator,
context: *nn.ExecutionContext,
model: *const nn.Modules.Mlp,
input: nn.Tensor,
targets: []const f32,
) !Metrics {
var forward = try model.forward(context, input);
defer forward.deinit();
const output_values = try allocator.alloc(f32, targets.len);
defer allocator.free(output_values);
try context.readback(forward.output, output_values);
var squared_error: f32 = 0;
var correct: usize = 0;
for (output_values, targets) |output, target| {
const difference = output - target;
squared_error += difference * difference;
if ((output >= 0.5) == (target >= 0.5)) correct += 1;
}
const count = @as(f32, @floatFromInt(targets.len));
return .{
.mse = squared_error / count,
.pixel_accuracy = @as(f32, @floatFromInt(correct)) / count,
};
}
fn generateDataset(allocator: std.mem.Allocator, count: usize, seed: u64) !Dataset {
if (count == 0) return error.EmptyDataset;
const noisy = try allocator.alloc(f32, count * feature_count);
errdefer allocator.free(noisy);
const clean = try allocator.alloc(f32, count * feature_count);
errdefer allocator.free(clean);
const labels = try allocator.alloc(usize, count);
errdefer allocator.free(labels);
var prng = std.Random.DefaultPrng.init(seed);
var random = prng.random();
for (0..count) |sample| {
const prototype = sample % prototype_count;
labels[sample] = prototype;
for (0..image_side) |y| {
for (0..image_side) |x| {
const index = sample * feature_count + y * image_side + x;
const target: f32 = if (prototypePixel(prototype, y, x)) 1 else 0;
clean[index] = target;
const jitter = (random.float(f32) * 2 - 1) * 0.22;
const flipped = random.float(f32) < 0.04;
const base = if (flipped) 1 - target else target;
noisy[index] = std.math.clamp(base + jitter, 0, 1);
}
}
}
return .{ .allocator = allocator, .noisy = noisy, .clean = clean, .labels = labels };
}
fn prototypePixel(prototype: usize, y: usize, x: usize) bool {
return switch (prototype) {
0 => x == 1,
1 => y == 2,
2 => x == y,
3 => x + y == image_side - 1,
else => unreachable,
};
}
fn prototypeName(prototype: usize) []const u8 {
return switch (prototype) {
0 => "vertical",
1 => "horizontal",
2 => "diagonal",
3 => "anti-diagonal",
else => unreachable,
};
}
fn printImage(label: []const u8, values: []const f32) void {
std.debug.print("\n{s}:\n", .{label});
for (0..image_side) |y| {
for (0..image_side) |x| {
const value = values[y * image_side + x];
const symbol: []const u8 = if (value >= 0.65)
"# "
else if (value >= 0.35)
"+ "
else
". ";
std.debug.print("{s}", .{symbol});
}
std.debug.print("\n", .{});
}
}
fn backendName(backend: nn.BackendType) []const u8 {
return switch (backend) {
.CPU => "cpu",
.Metal => "metal",
.CUDA => "cuda",
.ROCm => "rocm",
};
}
test "autoencoder options validate backend and steps" {
const options = try parseArgs(&.{ "--backend", "cpu", "--steps", "12", "--seed", "7" });
try std.testing.expectEqual(nn.DevicePreference.cpu, options.backend);
try std.testing.expectEqual(@as(usize, 12), options.steps);
try std.testing.expectEqual(@as(u64, 7), options.seed);
try std.testing.expectError(error.UnknownBackend, parseBackend("quantum"));
try std.testing.expectError(error.InvalidStepCount, parseArgs(&.{ "--steps", "0" }));
}
test "autoencoder dataset is deterministic and balanced" {
var first = try generateDataset(std.testing.allocator, 8, 3);
defer first.deinit();
var second = try generateDataset(std.testing.allocator, 8, 3);
defer second.deinit();
try std.testing.expectEqualSlices(f32, first.noisy, second.noisy);
try std.testing.expectEqualSlices(f32, first.clean, second.clean);
try std.testing.expectEqualSlices(usize, first.labels, second.labels);
try std.testing.expectEqualSlices(usize, &.{ 0, 1, 2, 3, 0, 1, 2, 3 }, first.labels);
}
test "autoencoder learns denoising through a two-value bottleneck" {
const result = try runExperiment(std.testing.allocator, .{
.backend = .cpu,
.steps = 220,
.seed = default_seed,
});
try std.testing.expect(result.final.mse < result.initial.mse * 0.25);
try std.testing.expect(result.final.pixel_accuracy >= 0.95);
try std.testing.expectEqual(@as(usize, 220), result.update_steps);
try std.testing.expectEqual(@as(usize, 0), result.training_readbacks);
}