neuromap.trainer¶
neuromap.trainer
¶
Reusable training loop for Neuromap SNN networks.
The :class:Trainer encapsulates gradient clipping, LR scheduling with
warmup, early stopping, and best-checkpoint tracking in a single
high-level API.
Example::
from neuromap import Network, Trainer, chips
net = Network(chips.NEUROSOC_V1, use_decoder=False)
trainer = Trainer(net, lr=1e-3, epochs=20)
history = trainer.fit(train_loader, val_loader)
TrainHistory
dataclass
¶
Trainer
¶
High-level training driver for :class:~neuromap.network.Network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Any
|
A :class: |
required |
lr
|
float
|
Initial learning rate. |
0.0003
|
epochs
|
int
|
Maximum number of training epochs. |
40
|
device
|
str
|
Torch device string (e.g. |
'cpu'
|
optimizer
|
str
|
Optimizer class name: |
'adam'
|
weight_decay
|
float
|
L2 regularisation weight. |
1e-06
|
scheduler
|
str
|
LR scheduler type: |
'plateau'
|
scheduler_factor
|
float
|
Factor by which LR is reduced (plateau). |
0.5
|
scheduler_patience
|
int
|
Epochs of no improvement before LR reduction. |
2
|
min_lr
|
float
|
Minimum learning rate for the scheduler. |
1e-06
|
lr_warmup_epochs
|
int
|
Epochs for linear LR warmup from 0 to lr. |
3
|
early_stop_patience
|
int
|
Stop after this many epochs without val-loss improvement. |
8
|
early_stop_min_delta
|
float
|
Minimum decrease to count as improvement. |
0.0001
|
grad_clip_norm
|
float
|
Max gradient norm for clipping (<=0 disables). |
1.0
|
loss_fn
|
Callable[..., Tensor] | None
|
Optional custom loss |
None
|
use_sequence_forward
|
bool
|
If |
False
|
fit(train_loader, val_loader=None, *, verbose=True)
¶
Train the network and return a :class:TrainHistory.
Each element in train_loader / val_loader must yield either
a (x, y) tuple of tensors, or an object with .x and
.y attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader
|
DataLoader
|
Training data loader. |
required |
val_loader
|
DataLoader | None
|
Optional validation data loader. |
None
|
verbose
|
bool
|
Print epoch-level progress to stdout. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
TrainHistory
|
class: |
evaluate(dataloader)
¶
Evaluate the network and return a metric dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataloader
|
DataLoader
|
Data loader to evaluate on. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with at least a |