-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathPermutationAlign.lpr
More file actions
174 lines (153 loc) · 5.91 KB
/
Copy pathPermutationAlign.lpr
File metadata and controls
174 lines (153 loc) · 5.91 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
program PermutationAlign;
(*
PermutationAlign: demonstrates TNNet.PermutationAlignReport, the "Git Re-Basin"
weight-space NEURON-PERMUTATION alignment diagnostic (Ainsworth, Hayase &
Srinivasa 2022; Entezari et al. 2021) — the DUAL of TNNet.ModeConnectivityReport.
ModeConnectivityReport MEASURES the linear-interpolation loss barrier between
two independently-trained nets of the same architecture. PermutationAlignReport
goes one step further: it shows that most of that barrier is an ILLUSION of
neuron-LABELLING. A hidden layer's units are interchangeable up to a
permutation (permute the units AND, in the next layer, the matching
input-weight columns, and the represented FUNCTION is unchanged). After aligning
net B's hidden units to net A's and re-interpolating, the barrier largely
COLLAPSES, because both nets sit in the same basin once you quotient out the
permutation symmetry.
This program trains the SAME tiny MLP twice on a synthetic 3-cluster 2D
classification task, from DIFFERENT random inits (so a real barrier exists
pre-alignment), then prints:
RUN 1 (weight matching): align by hidden-unit weight-row cosine.
RUN 2 (activation matching): align by per-unit activation correlation over
the probe batch.
CHECK (align-to-self): SnapshotB := A -> identity permutations and a
flat zero barrier.
Each run prints the loss barrier BEFORE vs AFTER alignment, the per-layer
permutation churn, and the three built-in PASS/FAIL correctness checks
(permutation invariance, align-to-self, monotonicity). Pure forward-only; the
live net's weights are restored exactly afterwards. Self-contained synthetic
data, runs in well under a minute on CPU.
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, Math,
neuralnetwork,
neuralvolume;
const
cEpochs = 60;
cTrainPerCls = 60;
cProbePerCls = 12;
cLearningRate = 0.05;
cClasses = 3;
Centers: array[0..2, 0..1] of TNeuralFloat =
((-2.0, -2.0), (2.0, 2.0), (2.0, -2.0));
procedure BuildNet(out NN: TNNet);
begin
NN := TNNet.Create();
NN.AddLayer(TNNetInput.Create(2, 1, 1));
NN.AddLayer(TNNetFullConnectReLU.Create(12));
NN.AddLayer(TNNetFullConnectReLU.Create(12));
NN.AddLayer(TNNetFullConnectLinear.Create(cClasses));
NN.SetLearningRate(cLearningRate, 0.9);
end;
// Deterministic sample for class C (so two nets can be fed the SAME data,
// differing only in init / batch order).
procedure MakeSample(C: integer; out X, Y: TNNetVolume);
begin
X := TNNetVolume.Create(2, 1, 1);
Y := TNNetVolume.Create(cClasses, 1, 1);
X.FData[0] := Centers[C][0] + (Random - 0.5) * 0.6;
X.FData[1] := Centers[C][1] + (Random - 0.5) * 0.6;
Y.Fill(0);
Y.FData[C] := 1.0;
end;
procedure TrainNet(NN: TNNet);
var
Epoch, I, C: integer;
X, Y: TNNetVolume;
begin
for Epoch := 1 to cEpochs do
for I := 1 to cTrainPerCls do
for C := 0 to cClasses - 1 do
begin
MakeSample(C, X, Y);
try
NN.Compute(X);
NN.Backpropagate(Y);
finally
X.Free;
Y.Free;
end;
end;
end;
procedure BuildProbes(out Probes: TNNetVolumePairList);
var
C, I: integer;
X, Y: TNNetVolume;
begin
Probes := TNNetVolumePairList.Create();
RandSeed := 777;
for C := 0 to cClasses - 1 do
for I := 1 to cProbePerCls do
begin
MakeSample(C, X, Y);
Probes.Add(TNNetVolumePair.Create(X, Y));
end;
end;
var
NNA, NNB: TNNet;
Probes: TNNetVolumePairList;
SnapB, Report: string;
begin
WriteLn('=== PermutationAlign demo: Git Re-Basin neuron-permutation alignment ===');
WriteLn('Two MLPs trained from DIFFERENT inits -> a real barrier pre-alignment');
WriteLn('that should visibly shrink once the permutation symmetry is removed.');
WriteLn;
BuildProbes(Probes);
try
// ---------------------------------------------------------------
// RUN 1: WEIGHT matching (ScoreMode = 0).
// ---------------------------------------------------------------
WriteLn('RUN 1 - DIFFERENT inits, WEIGHT matching (align by weight-row cosine).');
RandSeed := 101; BuildNet(NNA);
RandSeed := 999; BuildNet(NNB);
RandSeed := 11; TrainNet(NNA);
RandSeed := 22; TrainNet(NNB);
SnapB := NNB.SaveDataToString();
Report := TNNet.PermutationAlignReport(NNA, SnapB, Probes, 0, 10);
WriteLn(Report);
NNA.Free;
NNB.Free;
WriteLn;
// ---------------------------------------------------------------
// RUN 2: ACTIVATION matching (ScoreMode = 1).
// ---------------------------------------------------------------
WriteLn('RUN 2 - DIFFERENT inits, ACTIVATION matching (align by activation corr).');
RandSeed := 101; BuildNet(NNA);
RandSeed := 999; BuildNet(NNB);
RandSeed := 11; TrainNet(NNA);
RandSeed := 22; TrainNet(NNB);
SnapB := NNB.SaveDataToString();
Report := TNNet.PermutationAlignReport(NNA, SnapB, Probes, 1, 10);
WriteLn(Report);
NNA.Free;
NNB.Free;
WriteLn;
// ---------------------------------------------------------------
// CHECK: align-to-self (SnapshotB := A) -> identity perms, zero barrier.
// ---------------------------------------------------------------
WriteLn('CHECK - align-to-self (SnapshotB := A): identity perms, zero barrier.');
RandSeed := 303; BuildNet(NNA);
RandSeed := 11; TrainNet(NNA);
SnapB := NNA.SaveDataToString(); // B == A
Report := TNNet.PermutationAlignReport(NNA, SnapB, Probes, 0, 8);
WriteLn(Report);
NNA.Free;
finally
Probes.Free;
end;
end.