snnlab
API ReferenceLang

Training

Training recipes, objectives, parameter groups, optimizers and surrogate gradients.

Import recipe helpers from snnlab.lang.training; TrainSpec is also available as lang.TrainSpec. A recipe declares what should learn and how; ExecutionSpec controls the actual epochs, minibatches and checkpoint paths. Compile it with lang.compile.

TrainSpec

class TrainSpec:
    objectives: Sequence[Objective]
    parameter_groups: Sequence[ParameterGroup]
    optimizer: Optimizer
    regularizers: Sequence[Regularizer] = ()
    stop_gradients: Sequence[StopGradient] = ()
    epochs: int = 1
    gradient_clip: float | None = None
    surrogate: Spec | None = None
    presentation_duration: Quantity | None = None
FieldBehaviour
objectivesPredictions, target names and objective weights.
parameter_groupsNamed parameter selections, learning rates and frozen flags.
optimizerSerializable optimizer specification; graph training supports AdamW and registered optimizer factories.
regularizersOptional additional loss terms.
stop_gradientsSignal locations where the training graph should detach.
epochsRecipe metadata; specify run iteration using ExecutionSpec.epochs.
gradient_clipOptional global gradient norm limit.
surrogateSpike backward rule; FastSigmoid(slope=1.0) is the public helper.
presentation_durationTime quantity used by the recipe, including rate regularizers.

Objectives

CrossEntropy

def CrossEntropy(
    *, prediction: SignalLike | str, target: str, weight: float = 1.0
) -> Objective: ...

Objective

class Objective:
    kind: str
    prediction: str
    target: str
    weight: float = 1.0

prediction accepts a signal-like object or signal ID. target names a label tensor supplied through the execution request. Cross entropy uses class logits and integer class labels. Do not apply softmax before this loss. weight scales the objective contribution to total loss. Multiple objectives are allowed.

Parameter groups

ParameterGroup

class ParameterGroup:
    parameters: Sequence[ParameterRef | str]
    name: str
    lr: float
    frozen: bool = False

Use projection.weight, an explicit ParameterRef, or a graph parameter ID. ids() returns the resolved ID strings. Each group supplies its own learning rate; frozen=True excludes its parameters from optimization. Merely declaring a network parameter does not select it for training. Group membership and objective references are validated at compilation.

Optimizer and surrogate

AdamW

def AdamW(**config: Any) -> Optimizer: ...

Optimizer

class Optimizer:
    kind: str
    config: dict[str, Any] = field(default_factory=dict)

FastSigmoid

def FastSigmoid(*, slope: float = 1.0) -> Spec: ...

AdamW stores keyword configuration for the runtime optimizer, such as weight_decay=0.0. Group learning rates come from ParameterGroup. FastSigmoid stores a positive slope; forward spikes remain threshold events and the surrogate supplies their backward derivative. It does not replace the forward signal with a sigmoid. Built-in graph training applies the compiled surrogate slope; an external GraphExecutor must receive it explicitly.

Regularizers

SpikeBudgetPenalty

def SpikeBudgetPenalty(
    *, signals: Sequence[Signal | str], ceiling_hz: float, strength: float
) -> Regularizer: ...

UpperRatePenalty

def UpperRatePenalty(
    *, signal: Signal, threshold: float, strength: float
) -> Regularizer: ...

Regularizer

class Regularizer:
    kind: str
    signals: tuple[str, ...]
    strength: float
    config: dict[str, Any] = field(default_factory=dict)

SpikeBudgetPenalty penalizes population mean rates exceeding ceiling_hz, using a squared hinge. It averages presentations and then layers. Supply spike signals or IDs and the penalty strength. UpperRatePenalty is the single-signal convenience wrapper: threshold is in Hz. Rate evaluation requires an appropriate presentation duration. Regularizers are part of both total training and validation loss.

StopGradient

class StopGradient:
    signal: str

StopGradient.at(signal) constructs a declaration from a signal ID. It prevents downstream losses from propagating through that location; it does not remove the forward signal.

Execution and validation

Call train(ExecutionSpec(...)) once to run the built-in loop. Use positive epochs, optional batch_size, and shuffle for dataset iteration. ValidationSpec supplies held-out inputs and targets; it evaluates without updates. ExecutionResult metrics distinguish initial baseline, completed epochs and per-update losses.

See Training for a complete recipe and PyTorch Integration for an external optimizer loop.

Custom training definitions

CustomObjective, CustomRegularizer, CustomOptimizer and CustomSurrogate select versioned registered definitions. See Extensions for callback contracts, regression targets, optional classification metrics and checkpoint limits.