Helpers

Firing functions

SNN layers use deterministic events with a smooth surrogate derivative by default. We can instead pass smooth when the forward output should be a continuous intensity, or stochastic when it should sample hard events. These functions take the membrane’s distance from threshold, rather than the raw membrane itself.

tracetorch.snn.spike_fn.smooth(x: torch.Tensor) → torch.Tensor[source]

Apply the smooth traceTorch spike function.

sigmoid4x is a steeper sigmoid used to turn membrane distance from threshold into a differentiable firing intensity.

Parameters:

x (torch.Tensor) – membrane distance from threshold.

Returns:

smooth firing intensity in (0, 1).

Return type:

torch.Tensor

tracetorch.snn.spike_fn.deterministic(x: torch.Tensor) → torch.Tensor[source]

Apply a deterministic hard spike with a sigmoid4x surrogate.

The forward pass returns 1 when the membrane distance from threshold is non-negative and 0 otherwise. The backward pass uses the derivative of sigmoid4x so the maximum surrogate gradient is 1 at threshold.

tracetorch.snn.spike_fn.stochastic(x: torch.Tensor) → torch.Tensor[source]

Apply a stochastic hard spike with a sigmoid4x surrogate.

The forward pass samples from Bernoulli(sigmoid4x(x)). The backward pass uses the derivative of sigmoid4x so the maximum surrogate gradient is 1 at threshold.

Initialization transforms

When defining a transformed parameter, we usually want to choose its effective initial value, such as decay 0.9, rather than its raw representation. These inverse functions perform that conversion so the forward transform recovers the intended starting value.

tracetorch.inverse_fn.sigmoid(x: torch.Tensor) → torch.Tensor[source]

Return the logit transform of a tensor in (0, 1).

traceTorch uses this when registering constrained decay parameters. The raw stored value can be optimized freely, while torch.sigmoid(raw) recovers the user-facing decay.

Parameters:

x (torch.Tensor) – tensor whose values must lie strictly between zero and one.

Returns:

unconstrained tensor such that sigmoid(output) == x up to numerical precision.

Return type:

torch.Tensor

tracetorch.inverse_fn.softplus(x: torch.Tensor) → torch.Tensor[source]

Return the inverse softplus transform of a positive tensor.

traceTorch uses this when registering positive constrained parameters such as SNN thresholds. The raw stored value can be optimized freely, while softplus(raw) recovers the positive user-facing value.

Parameters:

x (torch.Tensor) – tensor whose values must be strictly positive.

Returns:

unconstrained tensor such that softplus(output) == x up to numerical precision.

Return type:

torch.Tensor

Decay conversion

convert_decay(value, from_type, to_type) connects "decay" with "halflife", "tau" and "horizon" descriptions. Its time quantities are measured in timesteps, so relating them to seconds also requires the duration assigned to one call by the task.

tracetorch.utils.convert_decay(value: float | torch.Tensor, from_type: Literal['decay', 'halflife', 'tau', 'horizon'], to_type: Literal['decay', 'halflife', 'tau', 'horizon'])[source]

Convert a decay or its timestep-based memory measure.

Supported measures are decay, halflife (time to halve a trace), tau (time to decay by a factor of e), and horizon (1 / (1 - decay)). The equivalent decay must lie strictly between zero and one. Tensor inputs preserve device and dtype and support differentiation; numeric inputs return a scalar. Convert physical durations to timesteps before using this helper.

Layout movement

class tracetorch.utils.MoveDim(*args: Any, **kwargs: Any)[source]

Bases: Module

A torch.movedim wrapper for explicit layout changes in Sequential.

For channel-first images, place MoveDim(-3, -1) before a traceTorch layer and MoveDim(-1, -3) after it. States then stay feature-last too.