-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathArchitectureDiff.lpr
More file actions
113 lines (97 loc) · 3.47 KB
/
Copy pathArchitectureDiff.lpr
File metadata and controls
113 lines (97 loc) · 3.47 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
program ArchitectureDiff;
(*
ArchitectureDiff: prints a unified-diff-style report of architectural
differences between two near-identical SimpleImageClassifier-style
networks. Demonstrates TNNet.DiffArchitecture and the companion
TNNet.DiffArchitectureFromString overload.
Variant A: the baseline classifier (Conv -> Conv -> MaxPool -> FC).
Variant B: same shape but with an extra TNNetChannelStdNormalization
after the first conv and the hidden FC activation swapped from
ReLU to Sigmoid.
The diff format is:
ClassName OutputShape Params (matching layer)
- ClassName OutputShape Params (only in A)
+ ClassName OutputShape Params (only in B)
Pure-CPU, finishes in milliseconds; no training is performed.
Copyright (C) 2026 Joao Paulo Schwarz Schuler
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
Coded by Claude (AI).
*)
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} cthreads, {$ENDIF}
Classes, SysUtils,
neuralnetwork,
neuralvolume;
function BuildVariantA: TNNet;
begin
Result := TNNet.Create();
Result.AddLayer(TNNetInput.Create(32, 32, 3));
Result.AddLayer(TNNetConvolutionReLU.Create(16, 3, 1, 1));
Result.AddLayer(TNNetConvolutionReLU.Create(32, 3, 1, 1));
Result.AddLayer(TNNetMaxPool.Create(2));
Result.AddLayer(TNNetFullConnectReLU.Create(64));
Result.AddLayer(TNNetFullConnectLinear.Create(10));
end;
// Same skeleton plus a normalization after the first conv and a
// different final hidden activation.
function BuildVariantB: TNNet;
begin
Result := TNNet.Create();
Result.AddLayer(TNNetInput.Create(32, 32, 3));
Result.AddLayer(TNNetConvolutionReLU.Create(16, 3, 1, 1));
Result.AddLayer(TNNetChannelStdNormalization.Create());
Result.AddLayer(TNNetConvolutionReLU.Create(32, 3, 1, 1));
Result.AddLayer(TNNetMaxPool.Create(2));
Result.AddLayer(TNNetFullConnectSigmoid.Create(64));
Result.AddLayer(TNNetFullConnectLinear.Create(10));
end;
procedure RunDemo;
var
A, B: TNNet;
Diff, SelfDiff, GoldenStr: string;
begin
A := BuildVariantA();
B := BuildVariantB();
try
WriteLn('Variant A summary:');
A.PrintSummary();
WriteLn('Variant B summary:');
B.PrintSummary();
WriteLn('Diff (A vs B):');
WriteLn(StringOfChar('-', 75));
Diff := A.DiffArchitecture(B);
if Diff = '' then
WriteLn(' (identical)')
else
Write(Diff);
WriteLn(StringOfChar('-', 75));
WriteLn;
WriteLn('Sanity: A diffed against itself should be empty.');
SelfDiff := A.DiffArchitecture(A);
if SelfDiff = '' then
WriteLn(' OK (empty diff)')
else
WriteLn(' FAIL: self-diff was not empty.');
WriteLn;
WriteLn('Sanity: DiffArchitectureFromString round-trip through ' +
'SaveStructureToString.');
GoldenStr := B.SaveStructureToString();
Diff := A.DiffArchitectureFromString(GoldenStr);
if Diff = '' then
WriteLn(' FAIL: A vs B golden should differ.')
else
WriteLn(' OK (', Length(Diff), ' chars, same shape as live diff)');
finally
A.Free;
B.Free;
end;
end;
var
Application: record Title: string; end;
begin
Application.Title := 'Architecture Diff Example';
RunDemo();
end.