-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
348 lines (289 loc) · 13.7 KB
/
train.py
File metadata and controls
348 lines (289 loc) · 13.7 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
"""
Training script for OctopusNet.
Three training modes:
1. Standard (default): FF modules + backprop coordinator. ~52.75% CIFAR-10.
2. SFF mode (--use_sff): FF modules + AuxClassifier + LogitCoordinator.
Fully local — no global backprop anywhere. ~53.16% CIFAR-10.
3. A6b mode (--channel_grouping --module_dropout 0.5): CGCNNModule + Module Dropout.
Best overall — 64.34% CIFAR-10, single-failure floor 61.12%.
Usage:
python train.py # standard mode
python train.py --use_sff # 100% local SFF
python train.py --channel_grouping --module_dropout 0.5 # A6b (recommended)
python train.py --dataset cifar100 --epochs 50
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim.lr_scheduler import MultiStepLR
from tqdm import tqdm
import json
import argparse
from datetime import datetime
from config import OctopusNetConfig
from modules import CGCNNModule
from octopusnet import OctopusNet, overlay_label_on_image, create_negative_samples
from coordinator import AuxClassifier, LogitCoordinator
from data import get_dataloaders
def _is_cg_model(model):
return isinstance(model.modules_list[0], CGCNNModule)
def train_epoch_ff(model, train_loader, config, epoch, cg_optimizer=None):
"""
Train modules with Forward-Forward for one epoch.
CG mode: uses cg_optimizer (external, with MultiStepLR) instead of
per-module Adam — lets the scheduler control lr across epochs.
"""
model.train()
total_losses = [0.0] * model.num_modules
num_batches = 0
pbar = tqdm(train_loader, desc=f"Epoch {epoch} [FF]")
for images, labels in pbar:
images = images.to(config.device)
labels = labels.to(config.device)
if _is_cg_model(model):
cg_optimizer.zero_grad()
ff_loss = sum(mod.ff_loss(images, labels) for mod in model.modules_list)
ff_loss.backward()
cg_optimizer.step()
per_mod = ff_loss.item() / model.num_modules
losses = [per_mod] * model.num_modules
else:
x_pos = overlay_label_on_image(images, labels, config.num_classes)
x_neg = create_negative_samples(images, labels, config.num_classes)
losses, _, _ = model.train_modules_ff(x_pos, x_neg)
for i in range(model.num_modules):
total_losses[i] += losses[i]
num_batches += 1
pbar.set_postfix({'avg_ff_loss': f'{sum(losses)/len(losses):.4f}'})
return [l / num_batches for l in total_losses]
def train_epoch_coordinator(model, train_loader, config, epoch, coord_optimizer=None):
"""
Train coordinator with backprop for one epoch.
Module dropout (if configured) is applied inside model.train_coordinator().
coord_optimizer is unused here — MultiStepLR steps it externally in train().
"""
model.train()
total_loss = 0.0
total_acc = 0.0
num_batches = 0
pbar = tqdm(train_loader, desc=f"Epoch {epoch} [Coord]")
for images, labels in pbar:
images = images.to(config.device)
labels = labels.to(config.device)
loss, acc = model.train_coordinator(images, labels)
total_loss += loss
total_acc += acc
num_batches += 1
pbar.set_postfix({'loss': f'{loss:.4f}', 'acc': f'{acc:.4f}'})
return total_loss / num_batches, total_acc / num_batches
def train_epoch_sff(model, aux_classifiers, logit_coord, aux_optimizer,
train_loader, config, epoch):
"""
Train AuxClassifiers + LogitCoordinator for one epoch (SFF mode).
100% local — no global backprop. Each module has its own AuxClassifier
trained with CrossEntropy. LogitCoordinator learns attention over logits.
detach() prevents gradients from flowing back to FF modules.
Based on: Krutsylo (arXiv:2501.03176, 2025) — SFF applied to OctopusNet.
Best result: 53.16% on CIFAR-10 (vs 52.50% with backprop coordinator).
"""
model.train()
aux_classifiers.train()
logit_coord.train()
criterion = nn.CrossEntropyLoss()
resolutions = config.input_scales if getattr(config, 'use_multiscale', False) else [32] * model.num_modules
total_loss = 0.0
total_acc = 0.0
num_batches = 0
pbar = tqdm(train_loader, desc=f"Epoch {epoch} [SFF]")
for images, labels in pbar:
images = images.to(config.device)
labels = labels.to(config.device)
# FF training (internal optimizer per module)
x_pos = overlay_label_on_image(images, labels, config.num_classes)
x_neg = create_negative_samples(images, labels, config.num_classes)
model.train_modules_ff(x_pos, x_neg)
# AuxClassifier + LogitCoordinator (local, no backprop to FF modules)
aux_optimizer.zero_grad()
logits_list = []
loss_aux = 0.0
for i, (module, aux_cls) in enumerate(zip(model.modules_list, aux_classifiers)):
res = resolutions[i]
x_i = F.interpolate(images, size=(res, res), mode='bilinear', align_corners=False)
_, _, f3 = module._conv_features(x_i)
logits_i = aux_cls(f3.detach()) # detach: no backprop to FF modules
logits_list.append(logits_i)
loss_aux += criterion(logits_i, labels)
logits_coord = logit_coord([l.detach() for l in logits_list])
loss_coord = criterion(logits_coord, labels)
(loss_aux + loss_coord).backward()
aux_optimizer.step()
acc = (logits_coord.argmax(1) == labels).float().mean().item()
total_loss += (loss_aux + loss_coord).item()
total_acc += acc
num_batches += 1
pbar.set_postfix({'loss': f'{(loss_aux+loss_coord).item():.4f}', 'acc': f'{acc:.4f}'})
return total_loss / num_batches, total_acc / num_batches
def evaluate(model, test_loader, config,
aux_classifiers=None, logit_coord=None):
"""
Evaluate model. If aux_classifiers provided, uses SFF inference.
Otherwise uses standard coordinator forward pass.
"""
model.eval()
if aux_classifiers is not None:
aux_classifiers.eval()
logit_coord.eval()
correct = 0
total = 0
resolutions = config.input_scales if getattr(config, 'use_multiscale', False) else [32] * model.num_modules
with torch.no_grad():
for images, labels in test_loader:
images = images.to(config.device)
labels = labels.to(config.device)
if aux_classifiers is not None:
# SFF inference: logit coordinator
logits_list = []
for i, (module, aux_cls) in enumerate(zip(model.modules_list, aux_classifiers)):
res = resolutions[i]
x_i = F.interpolate(images, size=(res, res), mode='bilinear', align_corners=False)
_, _, f3 = module._conv_features(x_i)
logits_list.append(aux_cls(f3))
preds = logit_coord(logits_list).argmax(1)
else:
preds = model.predict(images)
correct += (preds == labels).sum().item()
total += labels.size(0)
return correct / total
def train(config, use_sff=False, seed=42):
"""
Full training loop for OctopusNet.
Args:
config: OctopusNetConfig
use_sff: if True, use 100% local SFF mode (AuxClassifier + LogitCoordinator)
seed: random seed for reproducibility
Mode is determined by config flags:
ff_channel_grouping=True → CGCNNModule (A18b/A6b)
module_dropout_prob=0.5 → Module Dropout during coordinator training (A6b)
"""
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
cg_mode = getattr(config, 'ff_channel_grouping', False)
mod_drop = getattr(config, 'module_dropout_prob', 0.0)
mode_str = 'SFF 100% local' if use_sff else (
f'CG + ModDrop(p={mod_drop})' if cg_mode and mod_drop > 0 else
'CG (channel grouping)' if cg_mode else
'Standard (FF + backprop coord)'
)
print(f"Training OctopusNet on {config.dataset} — {mode_str}")
train_loader, test_loader = get_dataloaders(config)
model = OctopusNet(config).to(config.device)
# Schedulers for CG mode (Ortiz Torres prescription)
cg_optimizer = None
coord_optimizer = None
cg_sched = None
coord_sched = None
if cg_mode:
cg_optimizer = torch.optim.Adam(model.modules_list.parameters(), lr=1e-3)
coord_params = (
list(model.nerve_ring.parameters()) if model.nerve_ring else []
) + list(model.coordinator.parameters())
coord_optimizer = torch.optim.Adam(coord_params, lr=1e-3)
# Replace internal coordinator optimizer so MultiStepLR controls it
model.coordinator.optimizer = coord_optimizer
cg_sched = MultiStepLR(cg_optimizer, milestones=[10, 20, 27], gamma=0.1)
coord_sched = MultiStepLR(coord_optimizer, milestones=[10, 20, 27], gamma=0.1)
# SFF components (only if use_sff=True)
aux_classifiers = None
logit_coord = None
aux_optimizer = None
if use_sff:
aux_classifiers = nn.ModuleList([
AuxClassifier(in_channels=256, num_classes=config.num_classes).to(config.device)
for _ in range(model.num_modules)
])
logit_coord = LogitCoordinator(
num_modules=model.num_modules,
num_classes=config.num_classes
).to(config.device)
aux_optimizer = torch.optim.Adam(
list(aux_classifiers.parameters()) + list(logit_coord.parameters()),
lr=0.001
)
history = {'ff_losses': [], 'coord_losses': [], 'coord_train_acc': [], 'test_acc': []}
best_acc = 0.0
for epoch in range(1, config.epochs + 1):
print(f"\n{'='*50}\nEpoch {epoch}/{config.epochs}\n{'='*50}")
# Phase 1: FF training
ff_losses = train_epoch_ff(model, train_loader, config, epoch, cg_optimizer)
print(f"FF Losses: {[f'{l:.4f}' for l in ff_losses]}")
# Phase 2: Coordinator training
if use_sff:
coord_loss, train_acc = train_epoch_sff(
model, aux_classifiers, logit_coord, aux_optimizer,
train_loader, config, epoch
)
else:
coord_loss, train_acc = train_epoch_coordinator(
model, train_loader, config, epoch, coord_optimizer
)
if cg_sched:
cg_sched.step()
coord_sched.step()
print(f"Coordinator loss: {coord_loss:.4f} | Train acc: {train_acc:.4f}")
test_acc = evaluate(model, test_loader, config, aux_classifiers, logit_coord)
print(f"Test accuracy: {test_acc:.4f}")
history['ff_losses'].append(ff_losses)
history['coord_losses'].append(coord_loss)
history['coord_train_acc'].append(train_acc)
history['test_acc'].append(test_acc)
if test_acc > best_acc:
best_acc = test_acc
ckpt = {'model': model.state_dict(), 'epoch': epoch, 'acc': best_acc}
if use_sff:
ckpt['aux'] = aux_classifiers.state_dict()
ckpt['coord'] = logit_coord.state_dict()
torch.save(ckpt, 'best_model.pt')
print(f"New best: {best_acc:.4f}")
print(f"\nTraining complete! Best: {best_acc:.4f}")
with open(f'history_{datetime.now().strftime("%Y%m%d_%H%M%S")}.json', 'w') as f:
json.dump(history, f, indent=2)
return model, history
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Train OctopusNet")
parser.add_argument('--dataset', default='cifar10', choices=['cifar10', 'cifar100', 'mnist'])
parser.add_argument('--epochs', type=int, default=30)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--bottleneck', type=int, default=64)
parser.add_argument('--use_sff', action='store_true',
help='Use 100%% local SFF mode (AuxClassifier + LogitCoordinator). '
'Best accuracy: 53.16%% on CIFAR-10.')
parser.add_argument('--no_multiscale', action='store_true',
help='Disable multiscale input. Default: ON.')
parser.add_argument('--channel_grouping', action='store_true', default=True,
help='Use CGCNNModule (Ortiz Torres et al.). Default: ON (A21b).')
parser.add_argument('--no_channel_grouping', dest='channel_grouping', action='store_false',
help='Disable channel grouping.')
parser.add_argument('--stride_compress', action='store_true', default=True,
help='Use stride conv compression instead of AdaptiveAvgPool (A21b). Default: ON.')
parser.add_argument('--no_stride_compress', dest='stride_compress', action='store_false',
help='Disable stride conv compression (falls back to A6b pool).')
parser.add_argument('--module_dropout', type=float, default=0.7,
help='Module dropout probability during coordinator training. '
'p=0.7 default (A21b). Use p=0.5 for A6b config. '
'Raises single-failure floor: A21b floor 67.03%%.')
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--device', default='cuda' if torch.cuda.is_available() else 'cpu')
args = parser.parse_args()
config = OctopusNetConfig(
dataset=args.dataset,
epochs=args.epochs,
batch_size=args.batch_size,
bottleneck_size=args.bottleneck,
device=args.device,
use_multiscale=not args.no_multiscale,
ff_channel_grouping=args.channel_grouping,
use_stride_compress=args.stride_compress,
module_dropout_prob=args.module_dropout,
)
model, history = train(config, use_sff=args.use_sff, seed=args.seed)