Neurons¶
btorch.models.neurons
¶
Attributes¶
__all__ = ['LIF', 'ALIF', 'ELIF', 'GLIF3', 'Izhikevich', 'MixedNeuronPopulation', 'TwoCompartmentGLIF']
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]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
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). Default: 0.0. |
0.0
|
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]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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}']
|
ASC decay rates (ms^-1), can be list for multiple ASC components. Default: [0.2]. |
[0.2]
|
asc_amps
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}']
|
ASC amplitudes (pA) added at each spike. Default: [0.0]. |
[0.0]
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). Default: 0.0. |
0.0
|
trainable_param
|
set[str]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function.
Default: |
Erf(alpha=4.0, damping_factor=0.5)
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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 > 0). |
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
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 | |
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. |
Functions¶
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
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]
|
Trainable parameters. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
Functions¶
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.
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]
|
Set of parameter names to make trainable. Default: empty set. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function for backpropagation. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode, currently only "s" (single step) supported. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
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
|
str
|
|
'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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Functions¶
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]
|
Names of parameters to optimize. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function for soma spikes. |
ATan()
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
Literal['s', 'm']
|
|
's'
|
backend
|
Literal['torch']
|
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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 | |
Functions¶
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¶
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]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
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]
|
Trainable parameter names. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
str
|
"s" or "m". Default: "s". |
's'
|
backend
|
str
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Source code in btorch/models/base.py
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 1031 1032 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 | |
Functions¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=set(), surrogate_function=Sigmoid(), 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
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
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). Default: 0.0. |
0.0
|
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]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
Sigmoid
¶
Bases: SurrogateFunctionBase
Logistic surrogate gradient.
Surrogate gradient: g(v) = 4·σ(k·alpha·v)·(1−σ(k·alpha·v))
where k = 2·ln(√2+1) ≈ 1.763.
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/sigmoid.py
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.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¶
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]
|
Trainable parameter names. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
str
|
"s" or "m". Default: "s". |
's'
|
backend
|
str
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Source code in btorch/models/base.py
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 1031 1032 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 | |
Functions¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=set(), surrogate_function=Sigmoid(), 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
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
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}']
|
ASC decay rates (ms^-1), can be list for multiple ASC components. Default: [0.2]. |
[0.2]
|
asc_amps
|
float | Sequence[float] | Float[TensorLike, 'n_neuron {self.n_Iasc}']
|
ASC amplitudes (pA) added at each spike. Default: [0.0]. |
[0.0]
|
tau_ref
|
float | Float[TensorLike, ' n_neuron'] | None
|
Refractory period (ms). Default: 0.0. |
0.0
|
trainable_param
|
set[str]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function.
Default: |
Erf(alpha=4.0, damping_factor=0.5)
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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 > 0). |
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
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 | |
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. |
Functions¶
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
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
expand_trailing_dims(tensor, target_trailing_shape, match_full_shape=False, broadcast_only=False, view=True)
¶
Source code in btorch/models/shape.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¶
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]
|
Trainable parameter names. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
str
|
"s" or "m". Default: "s". |
's'
|
backend
|
str
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Source code in btorch/models/base.py
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 1031 1032 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 | |
Functions¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=set(), surrogate_function=Sigmoid(), 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
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
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]
|
Trainable parameters. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
Functions¶
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.
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
Sigmoid
¶
Bases: SurrogateFunctionBase
Logistic surrogate gradient.
Surrogate gradient: g(v) = 4·σ(k·alpha·v)·(1−σ(k·alpha·v))
where k = 2·ln(√2+1) ≈ 1.763.
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/sigmoid.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¶
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]
|
Trainable parameter names. Default: (). |
set()
|
surrogate_function
|
Callable
|
Surrogate for backprop. Default: Sigmoid(). |
Sigmoid()
|
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
|
str
|
"s" or "m". Default: "s". |
's'
|
backend
|
str
|
Compute backend. Default: "torch". |
'torch'
|
device
|
device | str | None
|
Tensor device. Default: None. |
None
|
dtype
|
dtype | None
|
Tensor dtype. Default: None. |
None
|
Source code in btorch/models/base.py
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 1031 1032 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 | |
Functions¶
__init__(n_neuron, v_threshold=1.0, v_reset=0.0, trainable_param=set(), surrogate_function=Sigmoid(), 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
neuronal_charge(x)
abstractmethod
¶
Define the charge difference equation.
Subclasses must implement this.
neuronal_fire()
¶
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]
|
Set of parameter names to make trainable. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
Functions¶
dV(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]
|
Set of parameter names to make trainable. Default: empty set. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function for backpropagation. Default: Sigmoid(). |
Sigmoid()
|
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
|
Literal['s']
|
Step mode, currently only "s" (single step) supported. Default: "s". |
's'
|
backend
|
Literal['torch']
|
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
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 | |
Sigmoid
¶
Bases: SurrogateFunctionBase
Logistic surrogate gradient.
Surrogate gradient: g(v) = 4·σ(k·alpha·v)·(1−σ(k·alpha·v))
where k = 2·ln(√2+1) ≈ 1.763.
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/sigmoid.py
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.
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
|
str
|
|
'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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Functions¶
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]
|
Names of parameters to optimize. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function for soma spikes. |
ATan()
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
Literal['s', 'm']
|
|
's'
|
backend
|
Literal['torch']
|
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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 | |
Functions¶
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¶
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
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 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 | |
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
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 | |
Functions¶
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
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 | |
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]
|
Names of parameters to optimize. |
set()
|
surrogate_function
|
Callable
|
Surrogate gradient function for soma spikes. |
ATan()
|
detach_reset
|
bool
|
If |
False
|
step_mode
|
Literal['s', 'm']
|
|
's'
|
backend
|
Literal['torch']
|
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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 | |
Functions¶
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]]
|
|