Neurons¶
btorch.models.neurons
¶
Attributes¶
__all__ = ['alif', 'dlif', 'glif', 'izhikevich', 'lif', 'mixed', 'two_compartment', 'LIF', 'ALIF', 'ELIF', 'GLIF3', 'Izhikevich', 'MixedNeuronPopulation', 'TwoCompartmentGLIF', 'DendriticLIF', 'DLIF', 'DBNN']
module-attribute
¶
Classes¶
ALIF
¶
Bases: BaseNode
Adaptive leaky integrate-and-fire neuron with conductance adaptation.
The ALIF model extends standard LIF by adding a voltage-dependent potassium conductance (g_k) that creates spike-frequency adaptation. Each spike increases g_k by dg_k, which then decays exponentially.
Dynamics
dv/dt = (-g_leak * (v - E_leak) - g_k * (v - E_k) + x) / c_m dg_k/dt = -g_k / tau_adapt
At spike: g_k += dg_k
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 1.0. |
1.0
|
g_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak conductance (nS). Default: 1.0. |
1.0
|
E_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak reversal potential (mV). Default: 0.0. |
0.0
|
E_k
|
float | Float[TensorLike, ' n_neuron']
|
Potassium reversal potential (mV). Default: -70.0. |
-70.0
|
g_k_init
|
float | Float[TensorLike, ' n_neuron']
|
Initial adaptation conductance (nS). Default: 0.0. |
0.0
|
tau_adapt
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation time constant (ms). Default: 20.0. |
20.0
|
dg_k
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation increment per spike (nS). Default: 0.0. |
0.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential, shape (*batch, n_neuron). |
g_k |
Tensor
|
Adaptation conductance, shape (*batch, n_neuron). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref specified). |
c_m, |
(g_leak, E_leak, E_k)
|
Neuron parameters. |
tau_adapt |
Tensor | Parameter
|
Adaptation time constant. |
dg_k |
Tensor | Parameter
|
Per-spike adaptation increment. |
Source code in btorch/models/neurons/alif.py
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 | |
DBNN
¶
Bases: DendriticLIF
Dendritic LIF cell with synaptic dynamics (default: dual-exponential PSC).
Source code in btorch/models/neurons/dlif.py
DLIF
¶
Bases: DendriticLIF
Dendritic LIF cell without synaptic dynamics (identity receptor current).
Source code in btorch/models/neurons/dlif.py
DendriticLIF
¶
Bases: MemoryModule
Composite dendritic-soma LIF recurrent cell.
Per step, this cell computes:
- optional receptor-wise synaptic filtering,
- bilinear + linear receptor mixing,
- somatic LIF spike update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
n_receptor
|
int
|
Number of receptor channels per neuron. |
required |
synapse_module
|
BasePSC | None
|
Optional pre-built synapse module. When |
None
|
synapse_cls
|
type[BasePSC] | None
|
Optional |
None
|
synapse_kwargs
|
dict | None
|
Keyword arguments passed to |
None
|
bilinear_mask
|
float | Tensor | None
|
Optional mask for :class: |
None
|
soma
|
LIF | None
|
Optional pre-built soma module. |
None
|
soma_kwargs
|
dict | None
|
Keyword arguments used to construct default |
None
|
step_mode
|
StepMode
|
Step mode label. Default: |
's'
|
backend
|
Backend
|
Backend label. Default: |
'torch'
|
Shape
Input per step:
(*batch, *n_neuron, n_receptor).
If n_receptor == 1, (*batch, *n_neuron) is also accepted.
Output per step:
(*batch, *n_neuron) spike tensor.
Source code in btorch/models/neurons/dlif.py
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 | |
ELIF
¶
Bases: ALIF
Exponential integrate-and-fire neuron with adaptation.
The ELIF model extends ALIF by adding an exponential term to the voltage dynamics, creating a sharp upswing when approaching threshold (the "initiation zone"). This captures the rapid depolarization seen in real neurons.
Dynamics
dv/dt = (g_leak * delta_T * exp((v - v_T) / delta_T) - g_leak * (v - E_leak) - g_k * (v - E_k) + x) / c_m dg_k/dt = -g_k / tau_adapt
The exponential term creates a soft threshold effect where membrane potential accelerates as it approaches v_T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 1.0. |
1.0
|
g_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak conductance (nS). Default: 1.0. |
1.0
|
E_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak reversal potential (mV). Default: 0.0. |
0.0
|
E_k
|
float | Float[TensorLike, ' n_neuron']
|
Potassium reversal potential (mV). Default: -70.0. |
-70.0
|
g_k_init
|
float | Float[TensorLike, ' n_neuron']
|
Initial adaptation conductance (nS). Default: 0.0. |
0.0
|
tau_adapt
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation time constant (ms). Default: 20.0. |
20.0
|
dg_k
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation increment per spike (nS). Default: 0.0. |
0.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
delta_T
|
float | Float[TensorLike, ' n_neuron']
|
Slope factor for exponential term (mV). Default: 1.0. |
1.0
|
v_T
|
float | Float[TensorLike, ' n_neuron']
|
Soft threshold potential (mV). Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
delta_T |
Tensor | Parameter
|
Slope factor for exponential term. |
v_T |
Tensor | Parameter
|
Soft threshold potential. |
Source code in btorch/models/neurons/alif.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
GLIF3
¶
Bases: BaseNode
GLIF3 model with after-spike currents and refractory period.
The GLIF3 model extends standard LIF by adding after-spike currents (ASC) that capture spike-frequency adaptation. Each spike adds asc_amps to the ASC vector, which then decays exponentially with time constants 1/k.
Dynamics
dV/dt = -(V - V_rest) / tau + (I_in + sum(I_asc)) / c_m dI_asc/dt = -k * I_asc
At spike: I_asc += asc_amps
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: -50.0. |
-50.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: -70.0. |
-70.0
|
v_rest
|
None | float | Float[TensorLike, ' n_neuron']
|
Resting potential (mV). Defaults to v_reset if None. |
None
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 0.05. |
0.05
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Membrane time constant (ms). Default: 20.0. |
20.0
|
k
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}'] | None
|
ASC decay rates (ms^-1), can be list for multiple ASC components. Default: [0.2]. |
None
|
asc_amps
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}'] | None
|
ASC amplitudes (pA) added at each spike. Default: [0.0]. |
None
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function.
Default: None, which builds a fresh |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential, shape (*batch, n_neuron). |
Iasc |
Tensor
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref is not None). |
c_m, |
(tau, tau_ref)
|
Neuron parameters. |
k |
Tensor | Parameter
|
ASC decay rates, shape (n_neuron, n_Iasc) or (n_Iasc,). |
asc_amps |
Tensor | Parameter
|
ASC amplitudes, shape (n_neuron, n_Iasc) or (n_Iasc,). |
n_Iasc |
int
|
Number of ASC components. |
References
Teeter et al., "Generalized leaky integrate-and-fire models classify multiple neuron types," Nature Communications, 2018.
Chen, G., Scherr, F., & Maass, W. (2022). A data-based large-scale model for primary visual cortex enables brain-like robust and versatile visual processing. Science Advances, 8(44), eabq7592. https://doi.org/10.1126/sciadv.abq7592
Source code in btorch/models/neurons/glif.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
Attributes¶
v_rest
property
writable
¶
Resting potential (mV).
For compatibility with GLIF4/GLIF5, falls back to v_reset if not explicitly set during initialization.
Returns:
| Type | Description |
|---|---|
Tensor
|
Resting potential tensor. |
Methods:¶
dIasc(Iasc)
¶
Compute ASC derivative for exponential Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iasc
|
Float[Tensor, '*batch n_neuron {self.n_Iasc}']
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (derivative, linear_coefficient) for exp_euler_step. |
Source code in btorch/models/neurons/glif.py
dV(v, Iasc, x)
¶
Compute membrane potential derivative for exp Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Float[Tensor, '*batch n_neuron']
|
Membrane potential, shape (*batch, n_neuron). |
required |
Iasc
|
Float[Tensor, '*batch n_neuron {self.n_Iasc}']
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
required |
x
|
Float[Tensor, '*batch n_neuron']
|
Input current, shape (*batch, n_neuron). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (derivative, linear_coefficient) for exp_euler_step. |
Source code in btorch/models/neurons/glif.py
exact_no_spike_at(x, t, v0=None, Iasc0=None)
¶
Closed-form no-spike state t after (v0, Iasc0), elementwise.
Between spikes the GLIF3 ODEs are linear, so for a constant input
:math:x they have an exact solution:
.. math:: I_{asc}(t) = I_{asc,0} e^{-k t}
and :math:v(t) is the leaky-integrator response to :math:x plus the
contribution of the decaying after-spike currents. Spikes and resets are
not modelled (sub-threshold regime).
This is the elementwise primitive: there is no time axis. Every
argument is broadcast against the others from the right, like ordinary
tensor arithmetic, so each batch element and each neuron can have its own
x, t, v0 and Iasc0 (and its own parameters). state below
is the broadcast of x, t, v0 and Iasc0[..., 0]:
x: constant input current,(*#state)(a python/0-dim scalar,(n_neuron,),(*batch, n_neuron), ...). A per-batch scalar needs an explicit trailing axis, e.g.x[..., None].t: elapsed time sincev0/Iasc0,(*#state), in the same unit asdt.v0:(*state), defaults to the currentself.v.Iasc0:(*state, n_Iasc), defaults to the currentself.Iasc.
It is a pure, differentiable function (the module state is only read,
and gradients flow to t, x, v0 and Iasc0), which makes it
a building block for iterative root finding such as "when does v
reach the threshold?". For example one Newton step on v(t) - v_th,
with dv/dt from the model's own ODE (:meth:dV):
v, Iasc = neuron.exact_no_spike_at(x, t, v0, Iasc0) dv_dt, _ = neuron.dV(v, Iasc, x) t = t - (v - neuron.v_threshold) / dv_dt
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[TensorLike, '*#state'] | float
|
Constant input current. |
required |
t
|
Float[TensorLike, '*#state'] | float
|
Elapsed time since |
required |
v0
|
Float[Tensor, '*state'] | None
|
Initial membrane potential. |
None
|
Iasc0
|
Float[Tensor, '*state {self.n_Iasc}'] | None
|
Initial after-spike currents. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Float[Tensor, '*state'], Float[Tensor, '*state {self.n_Iasc}']]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in btorch/models/neurons/glif.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
forward_exact_no_spike(x, t=None, v0=None, Iasc0=None, t_mode='homo')
¶
Evaluate the closed-form no-spike trajectory at several times.
The trajectory version of :meth:exact_no_spike_at (see there for the
maths and the broadcasting rules). The first axis of t is always the
time axis; t_mode says how the remaining axes are read, so the layout
is never guessed from shapes:
"homo"(default): one time grid shared by every batch element and neuron.tis a python float / 0-dim tensor (one time point) or a 1-D tensor(n_time,); any other shape raisesValueError."heter": the grid may differ per batch element and/or neuron. The axes oftafter the time axis are aligned to the right ofstateand broadcast against it:
======================== ==========================================
t shape meaning
======================== ==========================================
(n_time, n_neuron) a different time per neuron
(n_time, batch, 1) a different time per batch element
(n_time, batch, n) a different time per batch element and neuron
======================== ==========================================
If t carries batch/neuron axes that v0 / Iasc0 / x lack,
the output gains them. t defaults to a single step
environ.get("dt"). For heterogeneous times without a time axis (e.g.
inside a root finder) call :meth:exact_no_spike_at directly.
This is a pure function: the module state (self.v /
self.Iasc) is only read, never written. To continue a simulation
from the end of the trajectory, assign the last time point yourself:
v, Iasc = neuron.forward_exact_no_spike(x, t=t) neuron.v, neuron.Iasc = v[-1], Iasc[-1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[TensorLike, '*#state'] | float
|
Constant input current. |
required |
t
|
float | Float[TensorLike, ' n_time *#state'] | None
|
Elapsed time(s) since |
None
|
v0
|
Float[Tensor, '*state'] | None
|
Initial membrane potential. |
None
|
Iasc0
|
Float[Tensor, '*state {self.n_Iasc}'] | None
|
Initial after-spike currents. |
None
|
t_mode
|
Literal['homo', 'heter']
|
|
'homo'
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, ' n_time *state']
|
|
Float[Tensor, ' n_time *state {self.n_Iasc}']
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in btorch/models/neurons/glif.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
get_rheobase()
¶
Calculate rheobase current, the minimum constant input current required to make the neuron fire.
Izhikevich
¶
Bases: BaseNode
Izhikevich neuron with quadratic dynamics and recovery variable.
Efficient model reproducing diverse spiking patterns (tonic, bursting, etc.) via a 2D ODE system with quadratic nonlinearity.
Dynamics
dv/dt = (k(v-v_rest)(v-v_threshold) - u + I) / c_m du/dt = a * (b*(v-v_rest) - u)
At spike: v=v_reset, u=u+d
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons. |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Threshold (mV). Default: 30.0. |
30.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage (mV). Default: -65.0. |
-65.0
|
v_rest
|
float | Float[TensorLike, ' n_neuron']
|
Resting potential (mV). Default: -65.0. |
-65.0
|
v_peak
|
float | Float[TensorLike, ' n_neuron']
|
Spike cutoff (mV). Default: -40.0. |
-40.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Capacitance (pF). Default: 100.0. |
100.0
|
k
|
float | Float[TensorLike, ' n_neuron']
|
Scaling factor (nS/mV). Default: 0.7. |
0.7
|
a
|
float | Float[TensorLike, ' n_neuron']
|
Recovery timescale (ms^-1). Default: 0.03. |
0.03
|
b
|
float | Float[TensorLike, ' n_neuron']
|
Recovery coupling (nS). Default: -2.0. |
-2.0
|
d
|
float | Float[TensorLike, ' n_neuron']
|
Recovery jump (pA). Default: 100.0. |
100.0
|
trainable_param
|
set[str] | None
|
Trainable parameters. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike
|
bool
|
Store pre-spike values. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device. Default: None. |
None
|
dtype
|
dtype | None
|
Dtype. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential (*batch, n_neuron). |
u |
Tensor
|
Recovery variable (*batch, n_neuron). |
Reference
Izhikevich, IEEE Trans. Neural Networks, 2003.
Source code in btorch/models/neurons/izhikevich.py
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 | |
Methods:¶
from_canonical_quadratic(n_neuron, p1=0.04, p2=5.0, v_rest=-65.0, c_m=1.0, v_peak=30.0, **kwargs)
classmethod
¶
Instantiate using the canonical quadratic form
dV/dt = p1*v^2 + p2*v + p3 - u + I.
The mapping assumes c_m acts as the membrane capacitance and that
k/c_m equals p1. The linear term enforces
v_threshold = -p2/p1 - v_rest. Remaining
keyword arguments are passed directly to :class:Izhikevich.
The constant term p3 of the canonical form is not supported; it is
fixed to p1 * v_rest * v_threshold by the other parameters.
Source code in btorch/models/neurons/izhikevich.py
from_hippocampome(n_neuron, k, a, b, d, C, vr, vt, vpeak, vmin, **kwargs)
classmethod
¶
Build an :class:Izhikevich neuron using parameter names from
https://hippocampome.org.
Parameter mapping (HippoCampome -> Izhikevich args): - k -> k (scaling factor) - a -> a (recovery time constant) - b -> b (recovery sensitivity) - d -> d (reset current) - C -> c_m (capacitance) - vr -> v_rest (resting potential) - vt -> v_threshold (instantaneous threshold) - vpeak -> v_peak (spike cutoff) - vmin -> v_reset (post-spike reset voltage)
All values are expected in the same units as the canonical Izhikevich model (mV, pF, pA).
Source code in btorch/models/neurons/izhikevich.py
LIF
¶
Bases: BaseNode
Leaky integrate-and-fire neuron with optional refractory period.
The LIF neuron integrates input current while leaking towards a resting potential. When the membrane potential exceeds a threshold, a spike is emitted and the potential is reset.
Dynamics
dV/dt = -(V - V_reset) / tau + I / c_m
If tau_ref is specified, a refractory period prevents spiking for tau_ref milliseconds after each spike.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance. Default: 1.0. |
1.0
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Membrane time constant (ms). Default: 20.0. |
20.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period duration (ms). None disables refractory behavior. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. Default: None (empty set). |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for backpropagation. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal from computation graph. Default: False. |
False
|
hard_reset
|
bool
|
If True, reset to v_reset directly. If False, subtract (v_threshold - v_reset) from membrane potential (soft reset). Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage in v_pre_spike buffer. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode, currently only "s" (single step) supported. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential tensor, shape (*batch, n_neuron). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref specified). |
c_m |
Tensor | Parameter
|
Membrane capacitance (parameter or buffer). |
tau |
Tensor | Parameter
|
Time constant (parameter or buffer). |
tau_ref |
Tensor | Parameter | None
|
Refractory period (parameter or buffer, or None). |
Shape
- Input: (*batch, n_neuron)
- Output: (*batch, n_neuron) spike tensor (0 or 1)
Source code in btorch/models/neurons/lif.py
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 | |
MixedNeuronPopulation
¶
Bases: Module
Mix multiple neuron populations into a single logical layer.
The input current tensor is sliced along the last (neuron) dimension and
dispatched to each sub-population. Spikes from all groups are
concatenated back together so the layer behaves like a single neuron
module from the point of view of :class:~btorch.models.rnn.RecurrentNN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groups
|
Sequence[tuple[int, Module]] | Mapping[str, tuple[int, Module]]
|
Either a sequence of |
required |
step_mode
|
StepMode
|
|
's'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> from btorch.models.neurons import GLIF3, TwoCompartmentGLIF
>>> glif = GLIF3(n_neuron=60)
>>> tc = TwoCompartmentGLIF(n_neuron=40)
>>> mixed = MixedNeuronPopulation([(60, glif), (40, tc)])
Source code in btorch/models/neurons/mixed.py
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 | |
Methods:¶
forward(x, i_apical=None)
¶
Dispatch to single-step or multi-step forward.
Source code in btorch/models/neurons/mixed.py
multi_step_forward(x, i_apical=None)
¶
Advance all sub-populations over a full time sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input sequence of shape |
required |
i_apical
|
Tensor | None
|
Optional apical sequence of the same shape. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Spike sequence of shape |
Source code in btorch/models/neurons/mixed.py
single_step_forward(x, i_apical=None)
¶
Advance all sub-populations by one timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input current of shape |
required |
i_apical
|
Tensor | None
|
Optional apical current of the same shape. Only slices
corresponding to :class: |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Spike tensor of shape |
Source code in btorch/models/neurons/mixed.py
TwoCompartmentGLIF
¶
Bases: ParamBufferMixin, MemoryModule
Two-compartment current-based neuron with apical plateaus.
The model combines a fast somatic compartment with a slow apical current:
.. math:: \tau_s \frac{dV_s}{dt} = -(V_s - E_L) + R_s \left(I_{soma} + w_{as} I_a\right) + \Delta_T \exp\left(\frac{V_s - V_{th,eff}}{\Delta_T}\right)
.. math:: \tau_a \frac{dI_a}{dt} = -I_a + I_{apical} + w_{Ca} \sigma(I_a - \theta_{Ca}) + I_{bap}
Somatic spikes are generated by a surrogate threshold on V_s and stored
as a pending back-propagating current for the next timestep.
A minimal adaptive threshold state can optionally be enabled:
.. math:: V_{th,eff} = V_{th} + \theta_h
.. math:: \tau_h \frac{d\theta_h}{dt} = -\theta_h
followed by an instantaneous post-spike increment
:math:\theta_h \leftarrow \theta_h + \Delta_h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons, as an |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Somatic spike threshold. |
-50.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Somatic reset voltage after a spike. |
-65.0
|
tau_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic membrane time constant. |
20.0
|
R_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic input resistance. |
1.0
|
E_L
|
float | Float[TensorLike, ' n_neuron']
|
Somatic leak reversal / resting voltage. |
-70.0
|
tau_a
|
float | Float[TensorLike, ' n_neuron']
|
Apical current time constant. |
100.0
|
tau_th
|
float | Float[TensorLike, ' n_neuron']
|
Adaptive-threshold decay time constant. |
50.0
|
delta_th
|
float | Float[TensorLike, ' n_neuron']
|
Post-spike threshold increment. |
0.0
|
delta_T
|
float | Float[TensorLike, ' n_neuron']
|
Exponential spike-initiation scale. Set to zero to disable. |
0.0
|
w_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Calcium plateau amplitude. |
0.0
|
theta_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Apical plateau activation threshold. |
1.0
|
w_sa
|
float | Float[TensorLike, ' n_neuron']
|
Somatic-to-apical coupling weight for the delayed bAP current. |
0.5
|
w_as
|
float | Float[TensorLike, ' n_neuron']
|
Apical-to-somatic coupling weight for top-down drive. |
1.0
|
trainable_param
|
set[str] | None
|
Names of parameters to optimize. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for soma spikes.
Default: None, which builds a fresh |
None
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
StepMode
|
|
's'
|
backend
|
Backend
|
Backend label for consistency with other neuron modules. |
'torch'
|
device
|
device | None
|
Optional torch device. |
None
|
dtype
|
dtype | None
|
Optional torch dtype. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Somatic membrane voltage memory. |
i_a |
Tensor
|
Slow apical current memory. |
i_bap |
Tensor
|
Pending back-propagating current injected on the next step. |
theta_th |
Tensor
|
Adaptive threshold memory added to |
Examples:
>>> from btorch.models import environ, functional
>>> neuron = TwoCompartmentGLIF(n_neuron=1)
>>> functional.init_net_state(neuron, batch_size=2)
>>> with environ.context(dt=1.0):
... spike, voltage = neuron.single_step_forward(
... torch.zeros(2, 1),
... torch.zeros(2, 1),
... )
>>> tuple(spike.shape), tuple(voltage.shape)
((2, 1), (2, 1))
Notes
The integration uses a semi-implicit Euler update: the leak / decay term is treated implicitly while external and coupling currents are evaluated from the previous state. This keeps the update stable while preserving a simple differentiable recurrence.
Source code in btorch/models/neurons/two_compartment.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
Methods:¶
forward(i_soma, i_apical=None, *, return_state=False)
¶
Dispatch to single-step or multi-step forward.
Source code in btorch/models/neurons/two_compartment.py
multi_step_forward(i_soma_seq, i_apical_seq=None, *, return_state=False)
¶
Advance the neuron over a full input sequence.
Source code in btorch/models/neurons/two_compartment.py
single_step_forward(i_soma, i_apical=None, *, return_state=False)
¶
Advance the neuron by one timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i_soma
|
Float[Tensor | float, '*batch n_neuron']
|
Somatic current input. |
required |
i_apical
|
Float[Tensor | float, '*batch n_neuron'] | None
|
Optional apical current input. Defaults to zero. |
None
|
return_state
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
A tuple |
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
Source code in btorch/models/neurons/two_compartment.py
btorch.models.neurons.alif
¶
Adaptive leaky integrate-and-fire (ALIF) neuron models.
This module provides ALIF and ELIF (exponential LIF) neuron implementations with conductance-based adaptation mechanisms.
The ALIF neuron extends LIF by adding a potassium conductance (g_k) that increases with each spike, creating spike-frequency adaptation:
dv/dt = (-g_leak * (v - E_leak) - g_k * (v - E_k) + x) / c_m
dg_k/dt = -g_k / tau_adapt
where g_k increments by dg_k at each spike and decays exponentially, creating negative feedback that slows firing rate over time.
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
TensorLike = np.ndarray | torch.Tensor
module-attribute
¶
Classes¶
ALIF
¶
Bases: BaseNode
Adaptive leaky integrate-and-fire neuron with conductance adaptation.
The ALIF model extends standard LIF by adding a voltage-dependent potassium conductance (g_k) that creates spike-frequency adaptation. Each spike increases g_k by dg_k, which then decays exponentially.
Dynamics
dv/dt = (-g_leak * (v - E_leak) - g_k * (v - E_k) + x) / c_m dg_k/dt = -g_k / tau_adapt
At spike: g_k += dg_k
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 1.0. |
1.0
|
g_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak conductance (nS). Default: 1.0. |
1.0
|
E_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak reversal potential (mV). Default: 0.0. |
0.0
|
E_k
|
float | Float[TensorLike, ' n_neuron']
|
Potassium reversal potential (mV). Default: -70.0. |
-70.0
|
g_k_init
|
float | Float[TensorLike, ' n_neuron']
|
Initial adaptation conductance (nS). Default: 0.0. |
0.0
|
tau_adapt
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation time constant (ms). Default: 20.0. |
20.0
|
dg_k
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation increment per spike (nS). Default: 0.0. |
0.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential, shape (*batch, n_neuron). |
g_k |
Tensor
|
Adaptation conductance, shape (*batch, n_neuron). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref specified). |
c_m, |
(g_leak, E_leak, E_k)
|
Neuron parameters. |
tau_adapt |
Tensor | Parameter
|
Adaptation time constant. |
dg_k |
Tensor | Parameter
|
Per-spike adaptation increment. |
Source code in btorch/models/neurons/alif.py
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 | |
BaseNode
¶
Bases: ParamBufferMixin, MemoryModule
Base class for differentiable spiking neurons.
Implements the spiking neuron lifecycle: charge -> adapt -> fire -> reset. Subclasses implement neuronal_charge() and neuronal_adaptation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold. Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage. Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Trainable parameter names. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike_v
|
bool
|
Store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
"s" or "m". Default: "s". |
's'
|
backend
|
Backend
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Note
v_pre_spike is a separate buffer (a clone of v taken before the
reset), so enabling pre_spike_v roughly doubles the membrane-voltage
memory; it is only allocated when requested. The ODE integrator is not
configured on the base class: each subclass picks its own scheme in
neuronal_charge (e.g. :func:~btorch.models.ode.euler_step or
:func:~btorch.models.ode.exp_euler_step).
Source code in btorch/models/base.py
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
Methods:¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=None, surrogate_function=None, detach_reset=False, hard_reset=False, pre_spike_v=False, step_mode='s', backend='torch', device=None, dtype=None)
¶
Modified spikingjelly BaseNode.
- :ref:
API in English <BaseNode.__init__-en>
This class is the base class of differentiable spiking neurons.
Source code in btorch/models/base.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
Calculate output spikes from the current membrane potential and threshold.
neuronal_reset(spike)
¶
Reset the membrane potential according to the neurons' output spikes.
Source code in btorch/models/base.py
single_step_forward(x)
¶
- :ref:
API in English <BaseNode.single_step_forward-en>
Source code in btorch/models/base.py
ELIF
¶
Bases: ALIF
Exponential integrate-and-fire neuron with adaptation.
The ELIF model extends ALIF by adding an exponential term to the voltage dynamics, creating a sharp upswing when approaching threshold (the "initiation zone"). This captures the rapid depolarization seen in real neurons.
Dynamics
dv/dt = (g_leak * delta_T * exp((v - v_T) / delta_T) - g_leak * (v - E_leak) - g_k * (v - E_k) + x) / c_m dg_k/dt = -g_k / tau_adapt
The exponential term creates a soft threshold effect where membrane potential accelerates as it approaches v_T.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 1.0. |
1.0
|
g_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak conductance (nS). Default: 1.0. |
1.0
|
E_leak
|
float | Float[TensorLike, ' n_neuron']
|
Leak reversal potential (mV). Default: 0.0. |
0.0
|
E_k
|
float | Float[TensorLike, ' n_neuron']
|
Potassium reversal potential (mV). Default: -70.0. |
-70.0
|
g_k_init
|
float | Float[TensorLike, ' n_neuron']
|
Initial adaptation conductance (nS). Default: 0.0. |
0.0
|
tau_adapt
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation time constant (ms). Default: 20.0. |
20.0
|
dg_k
|
float | Float[TensorLike, ' n_neuron']
|
Adaptation increment per spike (nS). Default: 0.0. |
0.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
delta_T
|
float | Float[TensorLike, ' n_neuron']
|
Slope factor for exponential term (mV). Default: 1.0. |
1.0
|
v_T
|
float | Float[TensorLike, ' n_neuron']
|
Soft threshold potential (mV). Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
delta_T |
Tensor | Parameter
|
Slope factor for exponential term. |
v_T |
Tensor | Parameter
|
Soft threshold potential. |
Source code in btorch/models/neurons/alif.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
Functions:¶
exp_euler_step(f, *args, dt=1.0, linear=None)
¶
One integration step applying the exponential Euler method.
.. math:: rac{dx}{dt} = f(x) = Ax + B
where :math:A is the linear term and :math:f(x) is the derivative.
The update rule is:
.. math:: x_{n+1} = x_n + rac{e^{dt A} - 1}{A} f(x_n) \ &= e^{dt A}x_n + rac{e^{dt A} - 1}{A} B
Source code in btorch/models/ode.py
btorch.models.neurons.dlif
¶
Dendritic LIF-style recurrent cells.
This module implements composite recurrent cells that keep responsibilities separate:
- optional receptor-wise synaptic dynamics via a
BasePSCmodule, - dendritic receptor mixing via bilinear + linear terms,
- somatic spiking via :class:
btorch.models.neurons.lif.LIF.
The cells are designed for recurrent usage through make_rnn(cell).
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
__all__ = ['DendriticLIF', 'DLIF', 'DBNN']
module-attribute
¶
Classes¶
BasePSC
¶
Bases: MemoryModule
Base class for post-synaptic current models.
Provides infrastructure for synaptic dynamics including weight application and PSC state management.
Per-edge delays. When linear declares n_delay > 1 (e.g. a
:class:~btorch.models.connection.SparseConnection or
:class:~btorch.models.connection.Projection built with
Synapse(delay=...)), the PSC owns a
:class:~btorch.models.history.SpikeHistory of that depth in
self.history: every step pushes the incoming spikes and feeds
history.get_flattened(n_delay) to linear. A spike delivered at
step t on an edge with delay d first changes the PSC returned at
step t + d (delay 0 = the delivery step). The history is a registered
memory (history.history, shape (*batch, n_delay, n_source)), so it
follows init_net_state / reset_net / detach_net, gradient
checkpointing and named_hidden_states. It uses the torch.cat update
(autograd and torch.compile safe). With n_delay == 1 nothing is
allocated and self.history is None. A uniform extra delay can be
stacked on top with :class:DelayedPSC; the two add up.
Timing convention: a spike delivered to :meth:single_step_forward at
step t already changes the returned PSC at step t for every
subclass (the first response is at the delivery step, delay 0). The
impulse response k[t] returned by get_kernel therefore starts at
t = 0 with a non-zero value and :meth:multi_step_forward equals
stepping :meth:single_step_forward exactly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of post-synaptic neurons. |
required |
linear
|
Module
|
Linear layer for weight application. An integer |
required |
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Compute backend. Default: "torch". |
'torch'
|
n_delay
|
int | None
|
Number of delay bins of |
None
|
n_source
|
int | None
|
Expected number of source neurons (validated against
|
None
|
use_circular_buffer
|
bool
|
Update mode of the delay history; see
:class: |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in btorch/models/synapse.py
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 349 350 351 352 353 354 355 356 357 | |
Methods:¶
expect_expanded_input()
¶
Drop the own delay history; the caller feeds the expanded layout.
For a PSC whose owner already buffers the spikes and passes
[..., n_source * n_delay] itself (:class:HeterSynapsePSC does this
for its base_psc; a hand-written SpikeHistory loop can too).
Returns:
| Type | Description |
|---|---|
BasePSC
|
Self for method chaining. |
Source code in btorch/models/synapse.py
multi_step_forward(z_seq, kernel_len=64)
¶
Full-sequence forward via grouped 1D conv.
Extends :meth:MemoryModule.multi_step_forward (whose *args, **kwargs
are subclass-defined) with the optional kernel_len truncation. The
sequence is the first positional argument, as in the base signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z_seq
|
Tensor
|
(T, batch, n_neuron) spike sequence |
required |
kernel_len
|
int
|
length of the PSC impulse response kernel (truncation). Default: 64. |
64
|
Returns:
| Type | Description |
|---|---|
Tensor
|
(T, batch, n_neuron) PSC sequence. |
Notes
This path is stateless: it computes the response from rest and
neither reads nor updates psc or the delay history. With a
delayed connection the sequence is delay-expanded from an empty
history, which equals stepping :meth:single_step_forward after a
reset.
Source code in btorch/models/synapse.py
DBNN
¶
Bases: DendriticLIF
Dendritic LIF cell with synaptic dynamics (default: dual-exponential PSC).
Source code in btorch/models/neurons/dlif.py
DLIF
¶
Bases: DendriticLIF
Dendritic LIF cell without synaptic dynamics (identity receptor current).
Source code in btorch/models/neurons/dlif.py
DendriticLIF
¶
Bases: MemoryModule
Composite dendritic-soma LIF recurrent cell.
Per step, this cell computes:
- optional receptor-wise synaptic filtering,
- bilinear + linear receptor mixing,
- somatic LIF spike update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
n_receptor
|
int
|
Number of receptor channels per neuron. |
required |
synapse_module
|
BasePSC | None
|
Optional pre-built synapse module. When |
None
|
synapse_cls
|
type[BasePSC] | None
|
Optional |
None
|
synapse_kwargs
|
dict | None
|
Keyword arguments passed to |
None
|
bilinear_mask
|
float | Tensor | None
|
Optional mask for :class: |
None
|
soma
|
LIF | None
|
Optional pre-built soma module. |
None
|
soma_kwargs
|
dict | None
|
Keyword arguments used to construct default |
None
|
step_mode
|
StepMode
|
Step mode label. Default: |
's'
|
backend
|
Backend
|
Backend label. Default: |
'torch'
|
Shape
Input per step:
(*batch, *n_neuron, n_receptor).
If n_receptor == 1, (*batch, *n_neuron) is also accepted.
Output per step:
(*batch, *n_neuron) spike tensor.
Source code in btorch/models/neurons/dlif.py
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 | |
DualExponentialPSC
¶
Bases: BasePSC
Source code in btorch/models/synapse.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
Methods:¶
__init__(n_neuron, tau_decay, tau_rise, linear, A=None, step_mode='s', backend='torch')
¶
The Double Exponential form of PSC, from Brainpy/BrainState.
Source code in btorch/models/synapse.py
get_kernel(dt, kernel_len)
¶
Dual-exponential (alpha-shaped) kernel.
Kernel: k[t] = a * (a_d^(t+1) - a_r^(t+1)) for t >= 0 so a spike acts in its delivery step, where a = self.a, a_d = exp(-dt/tau_decay), a_r = exp(-dt/tau_rise).
Source code in btorch/models/synapse.py
LIF
¶
Bases: BaseNode
Leaky integrate-and-fire neuron with optional refractory period.
The LIF neuron integrates input current while leaking towards a resting potential. When the membrane potential exceeds a threshold, a spike is emitted and the potential is reset.
Dynamics
dV/dt = -(V - V_reset) / tau + I / c_m
If tau_ref is specified, a refractory period prevents spiking for tau_ref milliseconds after each spike.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance. Default: 1.0. |
1.0
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Membrane time constant (ms). Default: 20.0. |
20.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period duration (ms). None disables refractory behavior. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. Default: None (empty set). |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for backpropagation. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal from computation graph. Default: False. |
False
|
hard_reset
|
bool
|
If True, reset to v_reset directly. If False, subtract (v_threshold - v_reset) from membrane potential (soft reset). Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage in v_pre_spike buffer. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode, currently only "s" (single step) supported. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential tensor, shape (*batch, n_neuron). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref specified). |
c_m |
Tensor | Parameter
|
Membrane capacitance (parameter or buffer). |
tau |
Tensor | Parameter
|
Time constant (parameter or buffer). |
tau_ref |
Tensor | Parameter | None
|
Refractory period (parameter or buffer, or None). |
Shape
- Input: (*batch, n_neuron)
- Output: (*batch, n_neuron) spike tensor (0 or 1)
Source code in btorch/models/neurons/lif.py
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 | |
MemoryModule
¶
Bases: StepModule, Module
Base class for all stateful modules with managed memory buffers.
MemoryModule provides infrastructure for managing stateful tensors (memories) in neuromorphic models. Unlike SpikingJelly's MemoryModule, this implementation:
- Stores all memories as torch.Tensor buffers (enables ONNX export)
- Does not support list/tuple memories (override reset/init for history)
- Uses fixed memory sizes, with variable batch size
Memories are registered via register_memory() and automatically initialized/reset via init_state() and reset(). Each memory has a ResetValue configuration controlling its initialization behavior.
Example
class MyNeuron(MemoryModule): ... def init(self, n_neuron): ... super().init() ... self.register_memory("v", 0.0, n_neuron) ... ... def forward(self, x): ... self.v = self.v + x # simple integration ... return self.v
neuron = MyNeuron(10) neuron.init_state(batch_size=2) # init with batch dim out = neuron(torch.randn(2, 10))
Source code in btorch/models/base.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | |
Attributes¶
memories_rv
property
¶
Registered reset values, keyed by memory name (read-only view).
Methods:¶
multi_step_forward(x_seq, *args, **kwargs)
¶
Run single_step_forward over the leading time dim of x_seq.
The generic *args, **kwargs are forwarded to every step. Subclasses
may override this with extra, subclass-specific optional arguments
(e.g. kernel_len for PSC convolution) as long as the sequence stays
the first positional argument.
Source code in btorch/models/base.py
reset(batch_size=None, dtype=None, device=None, skip_mem_name=(), inplace=False)
¶
Reset every memory to its registered value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int | tuple[int, ...] | None
|
Leading batch shape to allocate, e.g. |
None
|
dtype
|
dtype | None
|
Fallback dtype for memories that do not pin their own
|
None
|
device
|
device | str | None
|
Target device. If None, the buffer's current device is kept. |
None
|
skip_mem_name
|
tuple[str, ...]
|
Names of memories to leave untouched. |
()
|
inplace
|
bool
|
If True, write into the existing buffers instead of rebinding
them to freshly-allocated tensors. This keeps each buffer's identity
and address stable (required to reset state inside a captured CUDA
graph), and the common zero-init case stays on-device (no host copy).
Cannot change a buffer's shape -- call |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in btorch/models/base.py
set_memories_rv(value, strict=True)
¶
Update the reset value of several registered memories.
For a whole network use
:func:~btorch.models.functional.set_memories_rv, which takes
dotted names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
dict
|
Flat mapping |
required |
strict
|
bool
|
Passed through to :meth: |
True
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If a key is not a registered memory. |
Source code in btorch/models/base.py
SymmetricBilinear
¶
Bases: Bilinear, HasConstraint
Source code in btorch/models/bilinear.py
Methods:¶
__init__(in_features, out_features, bias=True, mask=None, enforce_dale=False, device=None, dtype=None)
¶
Symmetric Bilinear layer where both inputs are the same. Supports weight masking and Dale's Law enforcement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Number of input features for both inputs. |
required |
out_features
|
int
|
Number of output features. |
required |
bias
|
bool
|
Whether to use a bias term. |
True
|
mask
|
float | Tensor | None
|
If float, a random binary mask with this density is generated. If Tensor, it must have shape (out_features, in_features, in_features). |
None
|
enforce_dale
|
bool
|
If True, enforces weights to maintain their initial sign
in the |
False
|
device
|
device | str | None
|
Torch device. |
None
|
dtype
|
dtype | None
|
Torch dtype. |
None
|
Source code in btorch/models/bilinear.py
constrain(*args, **kwargs)
¶
Apply the weight mask and Dale's Law constraints to the weight matrix.
Source code in btorch/models/bilinear.py
Functions:¶
normalize_n_neuron(n_neuron)
¶
Source code in btorch/models/base.py
btorch.models.neurons.glif
¶
Generalized leaky integrate-and-fire (GLIF) neuron models.
This module implements the GLIF3 model from the Allen Institute [1], which extends standard LIF with after-spike currents (ASC) that capture spike-frequency adaptation and other slow currents.
The GLIF3 neuron follows
dV/dt = -(V - V_rest) / tau + (I_in + sum(I_asc)) / c_m dI_asc/dt = -k * I_asc
where I_asc are after-spike currents that increment by asc_amps at each spike.
References
[1] Teeter et al., "Generalized leaky integrate-and-fire models classify multiple neuron types," Nat. Commun., 2018.
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
TensorLike = np.ndarray | torch.Tensor
module-attribute
¶
Classes¶
BaseNode
¶
Bases: ParamBufferMixin, MemoryModule
Base class for differentiable spiking neurons.
Implements the spiking neuron lifecycle: charge -> adapt -> fire -> reset. Subclasses implement neuronal_charge() and neuronal_adaptation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold. Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage. Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Trainable parameter names. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike_v
|
bool
|
Store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
"s" or "m". Default: "s". |
's'
|
backend
|
Backend
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Note
v_pre_spike is a separate buffer (a clone of v taken before the
reset), so enabling pre_spike_v roughly doubles the membrane-voltage
memory; it is only allocated when requested. The ODE integrator is not
configured on the base class: each subclass picks its own scheme in
neuronal_charge (e.g. :func:~btorch.models.ode.euler_step or
:func:~btorch.models.ode.exp_euler_step).
Source code in btorch/models/base.py
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
Methods:¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=None, surrogate_function=None, detach_reset=False, hard_reset=False, pre_spike_v=False, step_mode='s', backend='torch', device=None, dtype=None)
¶
Modified spikingjelly BaseNode.
- :ref:
API in English <BaseNode.__init__-en>
This class is the base class of differentiable spiking neurons.
Source code in btorch/models/base.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
Calculate output spikes from the current membrane potential and threshold.
neuronal_reset(spike)
¶
Reset the membrane potential according to the neurons' output spikes.
Source code in btorch/models/base.py
single_step_forward(x)
¶
- :ref:
API in English <BaseNode.single_step_forward-en>
Source code in btorch/models/base.py
Erf
¶
Bases: SurrogateFunctionBase
Error-function (Gaussian) surrogate gradient.
Surrogate gradient: g(v) = 2^{−(alpha·v)²} = exp(−ln2·(alpha·v)²)
alpha is the inverse half-width: HWHM = 1/alpha for any alpha.
Peak at threshold is 1.0 when damping=1.
The default alpha=4 (HWHM=0.25) matches the Gaussian surrogate used
in the large-scale V1 model of Chen et al. (2022), which uses
gauss_std=0.28 (mapping to alpha = 1/(0.28·√ln2) ≈ 4.3) with
damping_factor=0.5.
Parameters¶
alpha : float
Inverse half-width. variance = 1/alpha² sets the Gaussian
variance of the gradient envelope; alpha = 1/sqrt(variance).
variance : float, optional
Convenience alternative to alpha. Sets alpha = 1/sqrt(variance),
so the HWHM equals sqrt(variance).
References
Chen, G., Scherr, F., & Maass, W. (2022). A data-based large-scale model for primary visual cortex enables brain-like robust and versatile visual processing. Science Advances, 8(44), eabq7592. https://doi.org/10.1126/sciadv.abq7592
Source code in btorch/models/surrogate/erf.py
GLIF3
¶
Bases: BaseNode
GLIF3 model with after-spike currents and refractory period.
The GLIF3 model extends standard LIF by adding after-spike currents (ASC) that capture spike-frequency adaptation. Each spike adds asc_amps to the ASC vector, which then decays exponentially with time constants 1/k.
Dynamics
dV/dt = -(V - V_rest) / tau + (I_in + sum(I_asc)) / c_m dI_asc/dt = -k * I_asc
At spike: I_asc += asc_amps
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: -50.0. |
-50.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: -70.0. |
-70.0
|
v_rest
|
None | float | Float[TensorLike, ' n_neuron']
|
Resting potential (mV). Defaults to v_reset if None. |
None
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance (pF). Default: 0.05. |
0.05
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Membrane time constant (ms). Default: 20.0. |
20.0
|
k
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}'] | None
|
ASC decay rates (ms^-1), can be list for multiple ASC components. Default: [0.2]. |
None
|
asc_amps
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}'] | None
|
ASC amplitudes (pA) added at each spike. Default: [0.0]. |
None
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). None disables refractory. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function.
Default: None, which builds a fresh |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential, shape (*batch, n_neuron). |
Iasc |
Tensor
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref is not None). |
c_m, |
(tau, tau_ref)
|
Neuron parameters. |
k |
Tensor | Parameter
|
ASC decay rates, shape (n_neuron, n_Iasc) or (n_Iasc,). |
asc_amps |
Tensor | Parameter
|
ASC amplitudes, shape (n_neuron, n_Iasc) or (n_Iasc,). |
n_Iasc |
int
|
Number of ASC components. |
References
Teeter et al., "Generalized leaky integrate-and-fire models classify multiple neuron types," Nature Communications, 2018.
Chen, G., Scherr, F., & Maass, W. (2022). A data-based large-scale model for primary visual cortex enables brain-like robust and versatile visual processing. Science Advances, 8(44), eabq7592. https://doi.org/10.1126/sciadv.abq7592
Source code in btorch/models/neurons/glif.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
Attributes¶
v_rest
property
writable
¶
Resting potential (mV).
For compatibility with GLIF4/GLIF5, falls back to v_reset if not explicitly set during initialization.
Returns:
| Type | Description |
|---|---|
Tensor
|
Resting potential tensor. |
Methods:¶
dIasc(Iasc)
¶
Compute ASC derivative for exponential Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iasc
|
Float[Tensor, '*batch n_neuron {self.n_Iasc}']
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (derivative, linear_coefficient) for exp_euler_step. |
Source code in btorch/models/neurons/glif.py
dV(v, Iasc, x)
¶
Compute membrane potential derivative for exp Euler integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Float[Tensor, '*batch n_neuron']
|
Membrane potential, shape (*batch, n_neuron). |
required |
Iasc
|
Float[Tensor, '*batch n_neuron {self.n_Iasc}']
|
After-spike currents, shape (*batch, n_neuron, n_Iasc). |
required |
x
|
Float[Tensor, '*batch n_neuron']
|
Input current, shape (*batch, n_neuron). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (derivative, linear_coefficient) for exp_euler_step. |
Source code in btorch/models/neurons/glif.py
exact_no_spike_at(x, t, v0=None, Iasc0=None)
¶
Closed-form no-spike state t after (v0, Iasc0), elementwise.
Between spikes the GLIF3 ODEs are linear, so for a constant input
:math:x they have an exact solution:
.. math:: I_{asc}(t) = I_{asc,0} e^{-k t}
and :math:v(t) is the leaky-integrator response to :math:x plus the
contribution of the decaying after-spike currents. Spikes and resets are
not modelled (sub-threshold regime).
This is the elementwise primitive: there is no time axis. Every
argument is broadcast against the others from the right, like ordinary
tensor arithmetic, so each batch element and each neuron can have its own
x, t, v0 and Iasc0 (and its own parameters). state below
is the broadcast of x, t, v0 and Iasc0[..., 0]:
x: constant input current,(*#state)(a python/0-dim scalar,(n_neuron,),(*batch, n_neuron), ...). A per-batch scalar needs an explicit trailing axis, e.g.x[..., None].t: elapsed time sincev0/Iasc0,(*#state), in the same unit asdt.v0:(*state), defaults to the currentself.v.Iasc0:(*state, n_Iasc), defaults to the currentself.Iasc.
It is a pure, differentiable function (the module state is only read,
and gradients flow to t, x, v0 and Iasc0), which makes it
a building block for iterative root finding such as "when does v
reach the threshold?". For example one Newton step on v(t) - v_th,
with dv/dt from the model's own ODE (:meth:dV):
v, Iasc = neuron.exact_no_spike_at(x, t, v0, Iasc0) dv_dt, _ = neuron.dV(v, Iasc, x) t = t - (v - neuron.v_threshold) / dv_dt
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[TensorLike, '*#state'] | float
|
Constant input current. |
required |
t
|
Float[TensorLike, '*#state'] | float
|
Elapsed time since |
required |
v0
|
Float[Tensor, '*state'] | None
|
Initial membrane potential. |
None
|
Iasc0
|
Float[Tensor, '*state {self.n_Iasc}'] | None
|
Initial after-spike currents. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Float[Tensor, '*state'], Float[Tensor, '*state {self.n_Iasc}']]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in btorch/models/neurons/glif.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
forward_exact_no_spike(x, t=None, v0=None, Iasc0=None, t_mode='homo')
¶
Evaluate the closed-form no-spike trajectory at several times.
The trajectory version of :meth:exact_no_spike_at (see there for the
maths and the broadcasting rules). The first axis of t is always the
time axis; t_mode says how the remaining axes are read, so the layout
is never guessed from shapes:
"homo"(default): one time grid shared by every batch element and neuron.tis a python float / 0-dim tensor (one time point) or a 1-D tensor(n_time,); any other shape raisesValueError."heter": the grid may differ per batch element and/or neuron. The axes oftafter the time axis are aligned to the right ofstateand broadcast against it:
======================== ==========================================
t shape meaning
======================== ==========================================
(n_time, n_neuron) a different time per neuron
(n_time, batch, 1) a different time per batch element
(n_time, batch, n) a different time per batch element and neuron
======================== ==========================================
If t carries batch/neuron axes that v0 / Iasc0 / x lack,
the output gains them. t defaults to a single step
environ.get("dt"). For heterogeneous times without a time axis (e.g.
inside a root finder) call :meth:exact_no_spike_at directly.
This is a pure function: the module state (self.v /
self.Iasc) is only read, never written. To continue a simulation
from the end of the trajectory, assign the last time point yourself:
v, Iasc = neuron.forward_exact_no_spike(x, t=t) neuron.v, neuron.Iasc = v[-1], Iasc[-1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[TensorLike, '*#state'] | float
|
Constant input current. |
required |
t
|
float | Float[TensorLike, ' n_time *#state'] | None
|
Elapsed time(s) since |
None
|
v0
|
Float[Tensor, '*state'] | None
|
Initial membrane potential. |
None
|
Iasc0
|
Float[Tensor, '*state {self.n_Iasc}'] | None
|
Initial after-spike currents. |
None
|
t_mode
|
Literal['homo', 'heter']
|
|
'homo'
|
Returns:
| Type | Description |
|---|---|
Float[Tensor, ' n_time *state']
|
|
Float[Tensor, ' n_time *state {self.n_Iasc}']
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in btorch/models/neurons/glif.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
get_rheobase()
¶
Calculate rheobase current, the minimum constant input current required to make the neuron fire.
Functions:¶
exp_euler_step(f, *args, dt=1.0, linear=None)
¶
One integration step applying the exponential Euler method.
.. math:: rac{dx}{dt} = f(x) = Ax + B
where :math:A is the linear term and :math:f(x) is the derivative.
The update rule is:
.. math:: x_{n+1} = x_n + rac{e^{dt A} - 1}{A} f(x_n) \ &= e^{dt A}x_n + rac{e^{dt A} - 1}{A} B
Source code in btorch/models/ode.py
get_rheobase(v_threshold, v_rest, c_m, tau)
¶
Calculate rheobase current.
The rheobase is the minimum constant input current required to make the neuron fire. For GLIF models: I_rheobase = (v_threshold - v_rest) * c_m / tau
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v_threshold
|
float | Tensor
|
Firing threshold (mV). |
required |
v_rest
|
float | Tensor
|
Resting potential (mV). |
required |
c_m
|
float | Tensor
|
Membrane capacitance (pF). |
required |
tau
|
float | Tensor
|
Membrane time constant (ms). |
required |
Returns:
| Type | Description |
|---|---|
float | Tensor
|
Rheobase current (pA). |
Source code in btorch/models/neurons/glif.py
btorch.models.neurons.izhikevich
¶
Izhikevich neuron model.
Efficient 2D model reproducing diverse cortical spiking patterns.
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
TensorLike = np.ndarray | torch.Tensor
module-attribute
¶
Classes¶
BaseNode
¶
Bases: ParamBufferMixin, MemoryModule
Base class for differentiable spiking neurons.
Implements the spiking neuron lifecycle: charge -> adapt -> fire -> reset. Subclasses implement neuronal_charge() and neuronal_adaptation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold. Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage. Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Trainable parameter names. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike_v
|
bool
|
Store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
"s" or "m". Default: "s". |
's'
|
backend
|
Backend
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Note
v_pre_spike is a separate buffer (a clone of v taken before the
reset), so enabling pre_spike_v roughly doubles the membrane-voltage
memory; it is only allocated when requested. The ODE integrator is not
configured on the base class: each subclass picks its own scheme in
neuronal_charge (e.g. :func:~btorch.models.ode.euler_step or
:func:~btorch.models.ode.exp_euler_step).
Source code in btorch/models/base.py
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
Methods:¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=None, surrogate_function=None, detach_reset=False, hard_reset=False, pre_spike_v=False, step_mode='s', backend='torch', device=None, dtype=None)
¶
Modified spikingjelly BaseNode.
- :ref:
API in English <BaseNode.__init__-en>
This class is the base class of differentiable spiking neurons.
Source code in btorch/models/base.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
Calculate output spikes from the current membrane potential and threshold.
neuronal_reset(spike)
¶
Reset the membrane potential according to the neurons' output spikes.
Source code in btorch/models/base.py
single_step_forward(x)
¶
- :ref:
API in English <BaseNode.single_step_forward-en>
Source code in btorch/models/base.py
Izhikevich
¶
Bases: BaseNode
Izhikevich neuron with quadratic dynamics and recovery variable.
Efficient model reproducing diverse spiking patterns (tonic, bursting, etc.) via a 2D ODE system with quadratic nonlinearity.
Dynamics
dv/dt = (k(v-v_rest)(v-v_threshold) - u + I) / c_m du/dt = a * (b*(v-v_rest) - u)
At spike: v=v_reset, u=u+d
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons. |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Threshold (mV). Default: 30.0. |
30.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage (mV). Default: -65.0. |
-65.0
|
v_rest
|
float | Float[TensorLike, ' n_neuron']
|
Resting potential (mV). Default: -65.0. |
-65.0
|
v_peak
|
float | Float[TensorLike, ' n_neuron']
|
Spike cutoff (mV). Default: -40.0. |
-40.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Capacitance (pF). Default: 100.0. |
100.0
|
k
|
float | Float[TensorLike, ' n_neuron']
|
Scaling factor (nS/mV). Default: 0.7. |
0.7
|
a
|
float | Float[TensorLike, ' n_neuron']
|
Recovery timescale (ms^-1). Default: 0.03. |
0.03
|
b
|
float | Float[TensorLike, ' n_neuron']
|
Recovery coupling (nS). Default: -2.0. |
-2.0
|
d
|
float | Float[TensorLike, ' n_neuron']
|
Recovery jump (pA). Default: 100.0. |
100.0
|
trainable_param
|
set[str] | None
|
Trainable parameters. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike
|
bool
|
Store pre-spike values. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device. Default: None. |
None
|
dtype
|
dtype | None
|
Dtype. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential (*batch, n_neuron). |
u |
Tensor
|
Recovery variable (*batch, n_neuron). |
Reference
Izhikevich, IEEE Trans. Neural Networks, 2003.
Source code in btorch/models/neurons/izhikevich.py
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 | |
Methods:¶
from_canonical_quadratic(n_neuron, p1=0.04, p2=5.0, v_rest=-65.0, c_m=1.0, v_peak=30.0, **kwargs)
classmethod
¶
Instantiate using the canonical quadratic form
dV/dt = p1*v^2 + p2*v + p3 - u + I.
The mapping assumes c_m acts as the membrane capacitance and that
k/c_m equals p1. The linear term enforces
v_threshold = -p2/p1 - v_rest. Remaining
keyword arguments are passed directly to :class:Izhikevich.
The constant term p3 of the canonical form is not supported; it is
fixed to p1 * v_rest * v_threshold by the other parameters.
Source code in btorch/models/neurons/izhikevich.py
from_hippocampome(n_neuron, k, a, b, d, C, vr, vt, vpeak, vmin, **kwargs)
classmethod
¶
Build an :class:Izhikevich neuron using parameter names from
https://hippocampome.org.
Parameter mapping (HippoCampome -> Izhikevich args): - k -> k (scaling factor) - a -> a (recovery time constant) - b -> b (recovery sensitivity) - d -> d (reset current) - C -> c_m (capacitance) - vr -> v_rest (resting potential) - vt -> v_threshold (instantaneous threshold) - vpeak -> v_peak (spike cutoff) - vmin -> v_reset (post-spike reset voltage)
All values are expected in the same units as the canonical Izhikevich model (mV, pF, pA).
Source code in btorch/models/neurons/izhikevich.py
Functions:¶
btorch.models.neurons.lif
¶
Leaky integrate-and-fire (LIF) neuron models.
This module provides LIF and IF (integrate-and-fire) neuron implementations with optional refractory periods. These are the simplest spiking neuron models, suitable for basic neuromorphic computing tasks and as building blocks for more complex networks.
The LIF neuron follows the dynamics
dV/dt = -(V - V_reset) / tau + I / c_m
where V is membrane potential, tau is the time constant, I is input current, and c_m is membrane capacitance.
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
TensorLike = np.ndarray | torch.Tensor
module-attribute
¶
Classes¶
BaseNode
¶
Bases: ParamBufferMixin, MemoryModule
Base class for differentiable spiking neurons.
Implements the spiking neuron lifecycle: charge -> adapt -> fire -> reset. Subclasses implement neuronal_charge() and neuronal_adaptation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold. Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage. Default: 0.0. |
0.0
|
trainable_param
|
set[str] | None
|
Trainable parameter names. Default: None (empty). |
None
|
surrogate_function
|
Callable | None
|
Surrogate for backprop. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
Detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
Hard vs soft reset. Default: False. |
False
|
pre_spike_v
|
bool
|
Store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
"s" or "m". Default: "s". |
's'
|
backend
|
Backend
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Note
v_pre_spike is a separate buffer (a clone of v taken before the
reset), so enabling pre_spike_v roughly doubles the membrane-voltage
memory; it is only allocated when requested. The ODE integrator is not
configured on the base class: each subclass picks its own scheme in
neuronal_charge (e.g. :func:~btorch.models.ode.euler_step or
:func:~btorch.models.ode.exp_euler_step).
Source code in btorch/models/base.py
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
Methods:¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=None, surrogate_function=None, detach_reset=False, hard_reset=False, pre_spike_v=False, step_mode='s', backend='torch', device=None, dtype=None)
¶
Modified spikingjelly BaseNode.
- :ref:
API in English <BaseNode.__init__-en>
This class is the base class of differentiable spiking neurons.
Source code in btorch/models/base.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
Calculate output spikes from the current membrane potential and threshold.
neuronal_reset(spike)
¶
Reset the membrane potential according to the neurons' output spikes.
Source code in btorch/models/base.py
single_step_forward(x)
¶
- :ref:
API in English <BaseNode.single_step_forward-en>
Source code in btorch/models/base.py
IF
¶
Bases: LIF
Integrate-and-fire neuron without leak.
Simplified variant of LIF that lacks the leak term, meaning the membrane potential integrates input current linearly without decay:
dV/dt = I / c_m
This model is useful for theoretical analysis and as a baseline, though it lacks biological realism due to unbounded integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold. Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike. Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance. Default: 1.0. |
1.0
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Time constant (inherited from LIF but not used in dynamics). |
20.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period duration. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal. Default: False. |
False
|
hard_reset
|
bool
|
If True, use hard reset. Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Source code in btorch/models/neurons/lif.py
Methods:¶
dV(v, x)
¶
Compute membrane potential derivative (no leak term).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Tensor, '*batch n_neuron']
|
Input current, shape (*batch, n_neuron). |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, '*batch n_neuron']
|
dV/dt derivative, shape (*batch, n_neuron). |
Source code in btorch/models/neurons/lif.py
neuronal_charge(x)
¶
Update membrane potential using Euler integration (no leak).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Tensor, '*batch n_neuron']
|
Input current, shape (*batch, n_neuron). |
required |
Source code in btorch/models/neurons/lif.py
LIF
¶
Bases: BaseNode
Leaky integrate-and-fire neuron with optional refractory period.
The LIF neuron integrates input current while leaking towards a resting potential. When the membrane potential exceeds a threshold, a spike is emitted and the potential is reset.
Dynamics
dV/dt = -(V - V_reset) / tau + I / c_m
If tau_ref is specified, a refractory period prevents spiking for tau_ref milliseconds after each spike.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons (int or tuple of dimensions). |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Firing threshold (mV). Default: 1.0. |
1.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Reset voltage after spike (mV). Default: 0.0. |
0.0
|
c_m
|
float | Float[TensorLike, ' n_neuron']
|
Membrane capacitance. Default: 1.0. |
1.0
|
tau
|
float | Float[TensorLike, ' n_neuron']
|
Membrane time constant (ms). Default: 20.0. |
20.0
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period duration (ms). None disables refractory behavior. Default: None. |
None
|
trainable_param
|
set[str] | None
|
Set of parameter names to make trainable. Default: None (empty set). |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for backpropagation. Default: None, which builds a fresh Sigmoid() per neuron. |
None
|
detach_reset
|
bool
|
If True, detach reset signal from computation graph. Default: False. |
False
|
hard_reset
|
bool
|
If True, reset to v_reset directly. If False, subtract (v_threshold - v_reset) from membrane potential (soft reset). Default: False. |
False
|
pre_spike_v
|
bool
|
If True, store pre-spike voltage in v_pre_spike buffer. Default: False. |
False
|
step_mode
|
StepMode
|
Step mode, currently only "s" (single step) supported. Default: "s". |
's'
|
backend
|
Backend
|
Backend implementation. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Device for tensors. Default: None. |
None
|
dtype
|
dtype | None
|
Data type for tensors. Default: None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Membrane potential tensor, shape (*batch, n_neuron). |
refractory |
Tensor | None
|
Refractory counter (if tau_ref specified). |
c_m |
Tensor | Parameter
|
Membrane capacitance (parameter or buffer). |
tau |
Tensor | Parameter
|
Time constant (parameter or buffer). |
tau_ref |
Tensor | Parameter | None
|
Refractory period (parameter or buffer, or None). |
Shape
- Input: (*batch, n_neuron)
- Output: (*batch, n_neuron) spike tensor (0 or 1)
Source code in btorch/models/neurons/lif.py
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 | |
Functions:¶
btorch.models.neurons.mixed
¶
Heterogeneous neuron population that mixes multiple neuron models.
This module allows a single recurrent layer to contain different neuron
types (e.g. GLIF3 and TwoCompartmentGLIF) by slicing the input
current and dispatching to sub-populations.
Attributes¶
StepMode = Literal['s', 'm']
module-attribute
¶
Classes¶
MixedNeuronPopulation
¶
Bases: Module
Mix multiple neuron populations into a single logical layer.
The input current tensor is sliced along the last (neuron) dimension and
dispatched to each sub-population. Spikes from all groups are
concatenated back together so the layer behaves like a single neuron
module from the point of view of :class:~btorch.models.rnn.RecurrentNN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
groups
|
Sequence[tuple[int, Module]] | Mapping[str, tuple[int, Module]]
|
Either a sequence of |
required |
step_mode
|
StepMode
|
|
's'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> from btorch.models.neurons import GLIF3, TwoCompartmentGLIF
>>> glif = GLIF3(n_neuron=60)
>>> tc = TwoCompartmentGLIF(n_neuron=40)
>>> mixed = MixedNeuronPopulation([(60, glif), (40, tc)])
Source code in btorch/models/neurons/mixed.py
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 | |
Methods:¶
forward(x, i_apical=None)
¶
Dispatch to single-step or multi-step forward.
Source code in btorch/models/neurons/mixed.py
multi_step_forward(x, i_apical=None)
¶
Advance all sub-populations over a full time sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input sequence of shape |
required |
i_apical
|
Tensor | None
|
Optional apical sequence of the same shape. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Spike sequence of shape |
Source code in btorch/models/neurons/mixed.py
single_step_forward(x, i_apical=None)
¶
Advance all sub-populations by one timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input current of shape |
required |
i_apical
|
Tensor | None
|
Optional apical current of the same shape. Only slices
corresponding to :class: |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Spike tensor of shape |
Source code in btorch/models/neurons/mixed.py
TwoCompartmentGLIF
¶
Bases: ParamBufferMixin, MemoryModule
Two-compartment current-based neuron with apical plateaus.
The model combines a fast somatic compartment with a slow apical current:
.. math:: \tau_s \frac{dV_s}{dt} = -(V_s - E_L) + R_s \left(I_{soma} + w_{as} I_a\right) + \Delta_T \exp\left(\frac{V_s - V_{th,eff}}{\Delta_T}\right)
.. math:: \tau_a \frac{dI_a}{dt} = -I_a + I_{apical} + w_{Ca} \sigma(I_a - \theta_{Ca}) + I_{bap}
Somatic spikes are generated by a surrogate threshold on V_s and stored
as a pending back-propagating current for the next timestep.
A minimal adaptive threshold state can optionally be enabled:
.. math:: V_{th,eff} = V_{th} + \theta_h
.. math:: \tau_h \frac{d\theta_h}{dt} = -\theta_h
followed by an instantaneous post-spike increment
:math:\theta_h \leftarrow \theta_h + \Delta_h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons, as an |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Somatic spike threshold. |
-50.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Somatic reset voltage after a spike. |
-65.0
|
tau_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic membrane time constant. |
20.0
|
R_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic input resistance. |
1.0
|
E_L
|
float | Float[TensorLike, ' n_neuron']
|
Somatic leak reversal / resting voltage. |
-70.0
|
tau_a
|
float | Float[TensorLike, ' n_neuron']
|
Apical current time constant. |
100.0
|
tau_th
|
float | Float[TensorLike, ' n_neuron']
|
Adaptive-threshold decay time constant. |
50.0
|
delta_th
|
float | Float[TensorLike, ' n_neuron']
|
Post-spike threshold increment. |
0.0
|
delta_T
|
float | Float[TensorLike, ' n_neuron']
|
Exponential spike-initiation scale. Set to zero to disable. |
0.0
|
w_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Calcium plateau amplitude. |
0.0
|
theta_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Apical plateau activation threshold. |
1.0
|
w_sa
|
float | Float[TensorLike, ' n_neuron']
|
Somatic-to-apical coupling weight for the delayed bAP current. |
0.5
|
w_as
|
float | Float[TensorLike, ' n_neuron']
|
Apical-to-somatic coupling weight for top-down drive. |
1.0
|
trainable_param
|
set[str] | None
|
Names of parameters to optimize. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for soma spikes.
Default: None, which builds a fresh |
None
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
StepMode
|
|
's'
|
backend
|
Backend
|
Backend label for consistency with other neuron modules. |
'torch'
|
device
|
device | None
|
Optional torch device. |
None
|
dtype
|
dtype | None
|
Optional torch dtype. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Somatic membrane voltage memory. |
i_a |
Tensor
|
Slow apical current memory. |
i_bap |
Tensor
|
Pending back-propagating current injected on the next step. |
theta_th |
Tensor
|
Adaptive threshold memory added to |
Examples:
>>> from btorch.models import environ, functional
>>> neuron = TwoCompartmentGLIF(n_neuron=1)
>>> functional.init_net_state(neuron, batch_size=2)
>>> with environ.context(dt=1.0):
... spike, voltage = neuron.single_step_forward(
... torch.zeros(2, 1),
... torch.zeros(2, 1),
... )
>>> tuple(spike.shape), tuple(voltage.shape)
((2, 1), (2, 1))
Notes
The integration uses a semi-implicit Euler update: the leak / decay term is treated implicitly while external and coupling currents are evaluated from the previous state. This keeps the update stable while preserving a simple differentiable recurrence.
Source code in btorch/models/neurons/two_compartment.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
Methods:¶
forward(i_soma, i_apical=None, *, return_state=False)
¶
Dispatch to single-step or multi-step forward.
Source code in btorch/models/neurons/two_compartment.py
multi_step_forward(i_soma_seq, i_apical_seq=None, *, return_state=False)
¶
Advance the neuron over a full input sequence.
Source code in btorch/models/neurons/two_compartment.py
single_step_forward(i_soma, i_apical=None, *, return_state=False)
¶
Advance the neuron by one timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i_soma
|
Float[Tensor | float, '*batch n_neuron']
|
Somatic current input. |
required |
i_apical
|
Float[Tensor | float, '*batch n_neuron'] | None
|
Optional apical current input. Defaults to zero. |
None
|
return_state
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
A tuple |
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
Source code in btorch/models/neurons/two_compartment.py
btorch.models.neurons.two_compartment
¶
Two-compartment current-based spiking neurons.
This module implements a compact soma-apical neuron with:
- a fast somatic GLIF-like membrane state,
- a slow apical current state with a nonlinear plateau,
- bidirectional soma-apical coupling via scalar currents.
The implementation follows btorch's stateful-module conventions so that the membrane voltage and apical state can be initialized, reset, detached, and tracked during truncated BPTT.
Attributes¶
Backend = Literal['torch']
module-attribute
¶
StepMode = Literal['s', 'm']
module-attribute
¶
TensorLike = np.ndarray | torch.Tensor
module-attribute
¶
__all__ = ['TwoCompartmentGLIF']
module-attribute
¶
Classes¶
ATan
¶
Bases: SurrogateFunctionBase
Arctangent (Cauchy) surrogate gradient.
Surrogate gradient: g(v) = 1 / (1 + (alpha·v)²)
alpha is the inverse half-width: HWHM = 1/alpha for any alpha.
Peak at threshold is 1.0 when damping=1.
Source code in btorch/models/surrogate/atan.py
MemoryModule
¶
Bases: StepModule, Module
Base class for all stateful modules with managed memory buffers.
MemoryModule provides infrastructure for managing stateful tensors (memories) in neuromorphic models. Unlike SpikingJelly's MemoryModule, this implementation:
- Stores all memories as torch.Tensor buffers (enables ONNX export)
- Does not support list/tuple memories (override reset/init for history)
- Uses fixed memory sizes, with variable batch size
Memories are registered via register_memory() and automatically initialized/reset via init_state() and reset(). Each memory has a ResetValue configuration controlling its initialization behavior.
Example
class MyNeuron(MemoryModule): ... def init(self, n_neuron): ... super().init() ... self.register_memory("v", 0.0, n_neuron) ... ... def forward(self, x): ... self.v = self.v + x # simple integration ... return self.v
neuron = MyNeuron(10) neuron.init_state(batch_size=2) # init with batch dim out = neuron(torch.randn(2, 10))
Source code in btorch/models/base.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | |
Attributes¶
memories_rv
property
¶
Registered reset values, keyed by memory name (read-only view).
Methods:¶
multi_step_forward(x_seq, *args, **kwargs)
¶
Run single_step_forward over the leading time dim of x_seq.
The generic *args, **kwargs are forwarded to every step. Subclasses
may override this with extra, subclass-specific optional arguments
(e.g. kernel_len for PSC convolution) as long as the sequence stays
the first positional argument.
Source code in btorch/models/base.py
reset(batch_size=None, dtype=None, device=None, skip_mem_name=(), inplace=False)
¶
Reset every memory to its registered value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int | tuple[int, ...] | None
|
Leading batch shape to allocate, e.g. |
None
|
dtype
|
dtype | None
|
Fallback dtype for memories that do not pin their own
|
None
|
device
|
device | str | None
|
Target device. If None, the buffer's current device is kept. |
None
|
skip_mem_name
|
tuple[str, ...]
|
Names of memories to leave untouched. |
()
|
inplace
|
bool
|
If True, write into the existing buffers instead of rebinding
them to freshly-allocated tensors. This keeps each buffer's identity
and address stable (required to reset state inside a captured CUDA
graph), and the common zero-init case stays on-device (no host copy).
Cannot change a buffer's shape -- call |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in btorch/models/base.py
set_memories_rv(value, strict=True)
¶
Update the reset value of several registered memories.
For a whole network use
:func:~btorch.models.functional.set_memories_rv, which takes
dotted names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
dict
|
Flat mapping |
required |
strict
|
bool
|
Passed through to :meth: |
True
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If a key is not a registered memory. |
Source code in btorch/models/base.py
ParamBufferMixin
¶
Bases: Module
Standard parameter/buffer definition and load-shape behavior.
This mixin allows defining parameters/buffers that can be stored in their
minimal broadcastable form (to save memory) or as full arrays. Supports:
- easy trainable definition via one argument: trainable_param
- optional trainable shape policy (trainable_shape="scalar"|"full"|"auto")
- extend load_state_dict for loading non-uniform full tensors
on uniform scalar buffer
Source code in btorch/models/base.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
Methods:¶
def_param(name, val, *, sizes=None, trainable_param=None, trainable_shape='auto', normalize_to_sizes=False, **kwargs)
¶
Define a trainable parameter or persistent buffer.
Convenience wrapper equivalent to:
- :meth:
def_param_prepare - :meth:
def_param_register
Source code in btorch/models/base.py
def_param_prepare(name, val, *, sizes=None, trainable_param=None, trainable_shape='auto', normalize_to_sizes=False, **kwargs)
¶
Build a parameter definition without registering it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Attribute name. |
required |
val
|
Any
|
Initial value. |
required |
sizes
|
tuple[int | None, ...] | None
|
Intended tensor shape. If None, uses |
None
|
trainable_param
|
bool | set[str] | None
|
Trainable selector:
- |
None
|
trainable_shape
|
str
|
Shape policy for trainable values:
- |
'auto'
|
normalize_to_sizes
|
bool
|
If True, broadcast and materialize value to
|
False
|
**kwargs
|
Any
|
Passed to :func: |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value shape is not broadcastable to |
Returns:
| Type | Description |
|---|---|
PreparedParam
|
Prepared parameter metadata and tensor value. |
Source code in btorch/models/base.py
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 | |
def_param_register(prepared)
¶
Register a parameter from :meth:def_param_prepare.
Source code in btorch/models/base.py
def_param_resolve_sizes(*vals, sizes=None)
¶
Resolve a concrete size tuple from one or more values.
If sizes contains one None axis, this method infers that axis
per value and returns the broadcast-compatible maximum across values.
Source code in btorch/models/base.py
TwoCompartmentGLIF
¶
Bases: ParamBufferMixin, MemoryModule
Two-compartment current-based neuron with apical plateaus.
The model combines a fast somatic compartment with a slow apical current:
.. math:: \tau_s \frac{dV_s}{dt} = -(V_s - E_L) + R_s \left(I_{soma} + w_{as} I_a\right) + \Delta_T \exp\left(\frac{V_s - V_{th,eff}}{\Delta_T}\right)
.. math:: \tau_a \frac{dI_a}{dt} = -I_a + I_{apical} + w_{Ca} \sigma(I_a - \theta_{Ca}) + I_{bap}
Somatic spikes are generated by a surrogate threshold on V_s and stored
as a pending back-propagating current for the next timestep.
A minimal adaptive threshold state can optionally be enabled:
.. math:: V_{th,eff} = V_{th} + \theta_h
.. math:: \tau_h \frac{d\theta_h}{dt} = -\theta_h
followed by an instantaneous post-spike increment
:math:\theta_h \leftarrow \theta_h + \Delta_h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neuron
|
int | Sequence[int]
|
Number of neurons, as an |
required |
v_threshold
|
float | Float[TensorLike, ' n_neuron']
|
Somatic spike threshold. |
-50.0
|
v_reset
|
float | Float[TensorLike, ' n_neuron']
|
Somatic reset voltage after a spike. |
-65.0
|
tau_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic membrane time constant. |
20.0
|
R_s
|
float | Float[TensorLike, ' n_neuron']
|
Somatic input resistance. |
1.0
|
E_L
|
float | Float[TensorLike, ' n_neuron']
|
Somatic leak reversal / resting voltage. |
-70.0
|
tau_a
|
float | Float[TensorLike, ' n_neuron']
|
Apical current time constant. |
100.0
|
tau_th
|
float | Float[TensorLike, ' n_neuron']
|
Adaptive-threshold decay time constant. |
50.0
|
delta_th
|
float | Float[TensorLike, ' n_neuron']
|
Post-spike threshold increment. |
0.0
|
delta_T
|
float | Float[TensorLike, ' n_neuron']
|
Exponential spike-initiation scale. Set to zero to disable. |
0.0
|
w_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Calcium plateau amplitude. |
0.0
|
theta_Ca
|
float | Float[TensorLike, ' n_neuron']
|
Apical plateau activation threshold. |
1.0
|
w_sa
|
float | Float[TensorLike, ' n_neuron']
|
Somatic-to-apical coupling weight for the delayed bAP current. |
0.5
|
w_as
|
float | Float[TensorLike, ' n_neuron']
|
Apical-to-somatic coupling weight for top-down drive. |
1.0
|
trainable_param
|
set[str] | None
|
Names of parameters to optimize. |
None
|
surrogate_function
|
Callable | None
|
Surrogate gradient function for soma spikes.
Default: None, which builds a fresh |
None
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
StepMode
|
|
's'
|
backend
|
Backend
|
Backend label for consistency with other neuron modules. |
'torch'
|
device
|
device | None
|
Optional torch device. |
None
|
dtype
|
dtype | None
|
Optional torch dtype. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
v |
Tensor
|
Somatic membrane voltage memory. |
i_a |
Tensor
|
Slow apical current memory. |
i_bap |
Tensor
|
Pending back-propagating current injected on the next step. |
theta_th |
Tensor
|
Adaptive threshold memory added to |
Examples:
>>> from btorch.models import environ, functional
>>> neuron = TwoCompartmentGLIF(n_neuron=1)
>>> functional.init_net_state(neuron, batch_size=2)
>>> with environ.context(dt=1.0):
... spike, voltage = neuron.single_step_forward(
... torch.zeros(2, 1),
... torch.zeros(2, 1),
... )
>>> tuple(spike.shape), tuple(voltage.shape)
((2, 1), (2, 1))
Notes
The integration uses a semi-implicit Euler update: the leak / decay term is treated implicitly while external and coupling currents are evaluated from the previous state. This keeps the update stable while preserving a simple differentiable recurrence.
Source code in btorch/models/neurons/two_compartment.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
Methods:¶
forward(i_soma, i_apical=None, *, return_state=False)
¶
Dispatch to single-step or multi-step forward.
Source code in btorch/models/neurons/two_compartment.py
multi_step_forward(i_soma_seq, i_apical_seq=None, *, return_state=False)
¶
Advance the neuron over a full input sequence.
Source code in btorch/models/neurons/two_compartment.py
single_step_forward(i_soma, i_apical=None, *, return_state=False)
¶
Advance the neuron by one timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i_soma
|
Float[Tensor | float, '*batch n_neuron']
|
Somatic current input. |
required |
i_apical
|
Float[Tensor | float, '*batch n_neuron'] | None
|
Optional apical current input. Defaults to zero. |
None
|
return_state
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
A tuple |
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
tuple[Tensor, Tensor] | tuple[Tensor, Tensor, dict[str, Tensor]]
|
|
Source code in btorch/models/neurons/two_compartment.py
Functions:¶
is_broadcastable(shape_from, shape_to)
¶
Return whether shape_from broadcasts to shape_to.
This is the one-directional check matching :func:torch.broadcast_to
(e.g. (5, 1) does not broadcast to (1, 4), although the two
broadcast together). Only shapes are inspected; no tensor is allocated.