spikinggamma 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spikinggamma/__init__.py +0 -0
- spikinggamma/dendrite/__init__.py +0 -0
- spikinggamma/dendrite/base.py +38 -0
- spikinggamma/dendrite/split.py +51 -0
- spikinggamma/dendrite/synapse/__init__.py +0 -0
- spikinggamma/dendrite/synapse/base.py +3 -0
- spikinggamma/dendrite/synapse/conv2d.py +41 -0
- spikinggamma/dendrite/synapse/linear.py +23 -0
- spikinggamma/neuron.py +43 -0
- spikinggamma/signal/__init__.py +0 -0
- spikinggamma/signal/activation.py +19 -0
- spikinggamma/signal/base.py +26 -0
- spikinggamma/signal/default.py +27 -0
- spikinggamma/signal/dropout.py +54 -0
- spikinggamma/spike/__init__.py +0 -0
- spikinggamma/spike/base.py +59 -0
- spikinggamma/spike/identity.py +64 -0
- spikinggamma/spike/kernel/base.py +92 -0
- spikinggamma/spike/kernel/gamma.py +84 -0
- spikinggamma/spike/saturation.py +60 -0
- spikinggamma/spike/sigma_delta.py +196 -0
- spikinggamma/utils/__init__.py +0 -0
- spikinggamma/utils/datasets/__init__.py +0 -0
- spikinggamma/utils/datasets/common.py +24 -0
- spikinggamma/utils/datasets/dvsgesture.py +29 -0
- spikinggamma/utils/datasets/shd.py +45 -0
- spikinggamma/utils/datasets/ssc.py +57 -0
- spikinggamma/utils/enums.py +7 -0
- spikinggamma/utils/helpers.py +89 -0
- spikinggamma-0.1.0.dist-info/METADATA +54 -0
- spikinggamma-0.1.0.dist-info/RECORD +34 -0
- spikinggamma-0.1.0.dist-info/WHEEL +5 -0
- spikinggamma-0.1.0.dist-info/licenses/LICENSE +7 -0
- spikinggamma-0.1.0.dist-info/top_level.txt +1 -0
spikinggamma/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import torch.nn as nn
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
from spikinggamma.utils.enums import StepMode
|
|
5
|
+
|
|
6
|
+
class BaseDendriticFunction(nn.Module):
|
|
7
|
+
def __init__(self, merge_TB=True):
|
|
8
|
+
super().__init__()
|
|
9
|
+
self.merge_TB = merge_TB
|
|
10
|
+
|
|
11
|
+
def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
|
|
12
|
+
return True
|
|
13
|
+
|
|
14
|
+
def reset(self):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
def detach(self):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
def forward(self, y_hat_k: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
|
|
21
|
+
if not self.is_step_mode_compatible(step_mode):
|
|
22
|
+
raise ValueError(f"Dendritic function {self.__class__.__name__} is not compatible with step mode {step_mode}.")
|
|
23
|
+
|
|
24
|
+
if (step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS) and self.merge_TB:
|
|
25
|
+
T, B = y_hat_k.shape[:2]
|
|
26
|
+
# Spike encoders may return non-contiguous views (for example after
|
|
27
|
+
# broadcasting a threshold). ``reshape`` preserves the inexpensive
|
|
28
|
+
# view when possible and makes a contiguous copy when necessary.
|
|
29
|
+
y_hat_k = y_hat_k.reshape(-1, *y_hat_k.shape[2:])
|
|
30
|
+
|
|
31
|
+
out = self._forward(y_hat_k, step_mode)
|
|
32
|
+
|
|
33
|
+
if (step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS) and self.merge_TB:
|
|
34
|
+
out = out.reshape(T, B, *out.shape[1:])
|
|
35
|
+
return out
|
|
36
|
+
|
|
37
|
+
def _forward(self, y_hat_k: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
|
|
38
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
from typing import Tuple
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
from spikinggamma.dendrite.base import BaseDendriticFunction
|
|
6
|
+
from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
|
|
7
|
+
from spikinggamma.utils.enums import StepMode
|
|
8
|
+
|
|
9
|
+
class SplitDendriticFunction(BaseDendriticFunction):
|
|
10
|
+
def __init__(self, shape_out: Tuple[int, ...],
|
|
11
|
+
n_kernels: int,
|
|
12
|
+
synaptic_function: BaseSynapticFunction,
|
|
13
|
+
bias: bool = False,):
|
|
14
|
+
super().__init__()
|
|
15
|
+
self.synaptic_function = synaptic_function
|
|
16
|
+
if bias:
|
|
17
|
+
self.bias = nn.Parameter(torch.zeros(*shape_out))
|
|
18
|
+
else:
|
|
19
|
+
self.register_parameter("bias", None)
|
|
20
|
+
self.kernel_weights = nn.Parameter(
|
|
21
|
+
torch.empty(*shape_out, n_kernels)
|
|
22
|
+
)
|
|
23
|
+
self.reset_parameters()
|
|
24
|
+
|
|
25
|
+
def reset_parameters(self):
|
|
26
|
+
if self.kernel_weights is not None:
|
|
27
|
+
raw_weights = torch.randn_like(self.kernel_weights) * 0.1
|
|
28
|
+
with torch.no_grad():
|
|
29
|
+
self.kernel_weights.copy_(raw_weights)
|
|
30
|
+
if getattr(self, "bias", None) is not None:
|
|
31
|
+
with torch.no_grad():
|
|
32
|
+
self.bias.zero_()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get_kernel_weights(self) -> torch.Tensor:
|
|
36
|
+
return self.kernel_weights
|
|
37
|
+
|
|
38
|
+
def compute_xkw(self, xk: torch.Tensor) -> torch.Tensor:
|
|
39
|
+
if self.kernel_weights is not None:
|
|
40
|
+
xkw = xk * self.get_kernel_weights()
|
|
41
|
+
else:
|
|
42
|
+
xkw = xk
|
|
43
|
+
return xkw
|
|
44
|
+
|
|
45
|
+
def _forward(self, y_hat_k: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
|
|
46
|
+
xk = self.synaptic_function(y_hat_k)
|
|
47
|
+
xkw = self.compute_xkw(xk)
|
|
48
|
+
x = xkw.sum(dim=-1)
|
|
49
|
+
if self.bias is not None:
|
|
50
|
+
x = x + self.bias
|
|
51
|
+
return x
|
|
File without changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
|
|
2
|
+
import torch
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
import torch.nn.functional as F
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Conv2dPerNeuron(nn.Conv2d, BaseSynapticFunction): # same filters for all buckets
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
in_channels,
|
|
11
|
+
out_channels,
|
|
12
|
+
kernel_size,
|
|
13
|
+
stride=1,
|
|
14
|
+
padding=0,
|
|
15
|
+
dilation=1,
|
|
16
|
+
groups=1,
|
|
17
|
+
bias=False,
|
|
18
|
+
):
|
|
19
|
+
super().__init__(
|
|
20
|
+
in_channels,
|
|
21
|
+
out_channels,
|
|
22
|
+
kernel_size,
|
|
23
|
+
stride,
|
|
24
|
+
padding,
|
|
25
|
+
dilation,
|
|
26
|
+
groups,
|
|
27
|
+
bias,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
31
|
+
B, C_in, H, W, n_kernels = x.shape
|
|
32
|
+
x = x.permute(0, 4, 1, 2, 3).reshape(B * n_kernels, C_in, H, W)
|
|
33
|
+
weight = self.weight
|
|
34
|
+
bias = self.bias if self.bias is not None else None
|
|
35
|
+
y = F.conv2d(
|
|
36
|
+
x, weight, bias, self.stride, self.padding, self.dilation, self.groups
|
|
37
|
+
)
|
|
38
|
+
_, C_out, H_out, W_out = y.shape
|
|
39
|
+
y = y.reshape(B, n_kernels, C_out, H_out, W_out)
|
|
40
|
+
y = y.permute(0, 2, 3, 4, 1)
|
|
41
|
+
return y.contiguous()
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.nn.functional as F
|
|
4
|
+
from spikinggamma.dendrite.synapse.base import BaseSynapticFunction
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LinearPerNeuron(nn.Linear, BaseSynapticFunction):
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
in_features,
|
|
12
|
+
out_features,
|
|
13
|
+
bias=False,
|
|
14
|
+
):
|
|
15
|
+
super().__init__(in_features, out_features, bias=bias)
|
|
16
|
+
|
|
17
|
+
def forward(self, inp: torch.Tensor) -> torch.Tensor:
|
|
18
|
+
inp = inp.transpose(1, 2)
|
|
19
|
+
weight = self.weight
|
|
20
|
+
bias = self.bias if self.bias is not None else None
|
|
21
|
+
out = F.linear(inp, weight, bias)
|
|
22
|
+
return out.transpose(1, 2)
|
|
23
|
+
|
spikinggamma/neuron.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
import torch
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
from spikinggamma.dendrite.base import BaseDendriticFunction
|
|
5
|
+
from spikinggamma.signal.base import BaseSignalFunction
|
|
6
|
+
from spikinggamma.utils.enums import StepMode
|
|
7
|
+
from spikinggamma.spike.base import BaseSpikeFunction
|
|
8
|
+
|
|
9
|
+
class SpikingGammaNeuron(nn.Module):
|
|
10
|
+
def __init__(self, dendritic_fn: BaseDendriticFunction, signal_fn: Optional[BaseSignalFunction] = None, spike_fn: Optional[BaseSpikeFunction] = None, return_all=False):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.dendritic_fn = dendritic_fn
|
|
13
|
+
self.signal_fn = signal_fn
|
|
14
|
+
self.spike_fn = spike_fn
|
|
15
|
+
self.return_all = return_all
|
|
16
|
+
|
|
17
|
+
def detach(self):
|
|
18
|
+
self.dendritic_fn.detach()
|
|
19
|
+
if self.signal_fn is not None:
|
|
20
|
+
self.signal_fn.detach()
|
|
21
|
+
if self.spike_fn is not None:
|
|
22
|
+
self.spike_fn.detach()
|
|
23
|
+
|
|
24
|
+
def reset(self):
|
|
25
|
+
self.dendritic_fn.reset()
|
|
26
|
+
if self.signal_fn is not None:
|
|
27
|
+
self.signal_fn.reset()
|
|
28
|
+
if self.spike_fn is not None:
|
|
29
|
+
self.spike_fn.reset()
|
|
30
|
+
|
|
31
|
+
def forward(self, y_hat_k_in: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
|
|
32
|
+
x = self.dendritic_fn(y_hat_k_in, step_mode)
|
|
33
|
+
if self.signal_fn is None and self.spike_fn is None:
|
|
34
|
+
return x
|
|
35
|
+
y = x if self.signal_fn is None else self.signal_fn(y_hat_k_in, x, step_mode)
|
|
36
|
+
if self.spike_fn is None:
|
|
37
|
+
if self.return_all:
|
|
38
|
+
return y, {'x': x}
|
|
39
|
+
return y
|
|
40
|
+
y_hat_k_out, s_out = self.spike_fn(y, step_mode)
|
|
41
|
+
if self.return_all:
|
|
42
|
+
return y_hat_k_out, s_out, {'x': x, 'y': y}
|
|
43
|
+
return y_hat_k_out, s_out
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
|
|
4
|
+
class ZeroActivatedReLUFunction(torch.autograd.Function):
|
|
5
|
+
@staticmethod
|
|
6
|
+
def forward(ctx, input):
|
|
7
|
+
ctx.save_for_backward(input)
|
|
8
|
+
return torch.relu(input)
|
|
9
|
+
|
|
10
|
+
@staticmethod
|
|
11
|
+
def backward(ctx, grad_output):
|
|
12
|
+
input, = ctx.saved_tensors
|
|
13
|
+
grad_input = grad_output.clone()
|
|
14
|
+
grad_input[input < 0] = 0
|
|
15
|
+
return grad_input
|
|
16
|
+
|
|
17
|
+
class ZeroActivatedReLU(nn.Module):
|
|
18
|
+
def forward(self, x):
|
|
19
|
+
return ZeroActivatedReLUFunction.apply(x)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
from spikinggamma.utils.enums import StepMode
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
class BaseSignalFunction(nn.Module):
|
|
7
|
+
def __init__(self):
|
|
8
|
+
super().__init__()
|
|
9
|
+
|
|
10
|
+
def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
|
|
11
|
+
return True
|
|
12
|
+
|
|
13
|
+
def reset(self):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
def detach(self):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
def _forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
|
|
20
|
+
raise NotImplementedError
|
|
21
|
+
|
|
22
|
+
def forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> torch.Tensor:
|
|
23
|
+
if not self.is_step_mode_compatible(step_mode):
|
|
24
|
+
raise ValueError(f"Signal function {self.__class__.__name__} is not compatible with step mode {step_mode}.")
|
|
25
|
+
y = self._forward(y_hat_k, x, step_mode)
|
|
26
|
+
return y
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from spikinggamma.signal.base import BaseSignalFunction
|
|
4
|
+
from spikinggamma.signal.activation import ZeroActivatedReLU
|
|
5
|
+
from spikinggamma.utils.enums import StepMode
|
|
6
|
+
|
|
7
|
+
class DefaultSignalFunction(BaseSignalFunction):
|
|
8
|
+
# act_fn is ZeroActivatedReLU by default
|
|
9
|
+
def __init__(self,
|
|
10
|
+
pre_act_fn: nn.Module = nn.Identity(),
|
|
11
|
+
act_fn: nn.Module = ZeroActivatedReLU(),
|
|
12
|
+
post_act_fn: nn.Module = nn.Identity()):
|
|
13
|
+
super().__init__()
|
|
14
|
+
self.pre_act_fn = pre_act_fn
|
|
15
|
+
self.act_fn = act_fn
|
|
16
|
+
self.post_act_fn = post_act_fn
|
|
17
|
+
|
|
18
|
+
def reset(self):
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
def _forward(self, y_hat_k: torch.Tensor, x: torch.Tensor, step_mode: StepMode) -> torch.Tensor:
|
|
22
|
+
x = self.pre_act_fn(x)
|
|
23
|
+
y = self.act_fn(x)
|
|
24
|
+
y_out = (
|
|
25
|
+
self.post_act_fn(y)
|
|
26
|
+
)
|
|
27
|
+
return y_out
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
|
|
4
|
+
from spikinggamma.utils.enums import StepMode
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LockedDropout(nn.Module):
|
|
8
|
+
"""Drop feature channels with one mask shared across time.
|
|
9
|
+
|
|
10
|
+
Sequence inputs are expected to be shaped ``(T, B, ...)``. Each sample
|
|
11
|
+
receives its own mask, but that mask is held fixed for all ``T`` steps.
|
|
12
|
+
Explicit SINGLE or MULTI_STATEFUL calls reuse their mask until ``reset``.
|
|
13
|
+
Without an explicit step mode, non-sequence inputs retain ordinary
|
|
14
|
+
element-wise dropout semantics.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, p: float = 0.5) -> None:
|
|
18
|
+
super().__init__()
|
|
19
|
+
if not 0.0 <= p <= 1.0:
|
|
20
|
+
raise ValueError("dropout probability must be between 0 and 1")
|
|
21
|
+
self.p = float(p)
|
|
22
|
+
self.register_buffer("_mask", None, persistent=False)
|
|
23
|
+
|
|
24
|
+
def reset(self) -> None:
|
|
25
|
+
self._mask = None
|
|
26
|
+
|
|
27
|
+
def forward(
|
|
28
|
+
self, x: torch.Tensor, step_mode: StepMode = StepMode.MULTI_STATELESS
|
|
29
|
+
) -> torch.Tensor:
|
|
30
|
+
if not self.training or self.p == 0.0:
|
|
31
|
+
return x
|
|
32
|
+
if self.p == 1.0:
|
|
33
|
+
return torch.zeros_like(x)
|
|
34
|
+
|
|
35
|
+
mask_shape = (
|
|
36
|
+
x.shape[1:]
|
|
37
|
+
if step_mode != StepMode.SINGLE and x.ndim >= 3
|
|
38
|
+
else x.shape
|
|
39
|
+
)
|
|
40
|
+
mask = self._mask if step_mode != StepMode.MULTI_STATELESS else None
|
|
41
|
+
if (
|
|
42
|
+
mask is None
|
|
43
|
+
or mask.shape != mask_shape
|
|
44
|
+
or mask.device != x.device
|
|
45
|
+
or mask.dtype != x.dtype
|
|
46
|
+
):
|
|
47
|
+
mask = x.new_empty(mask_shape).bernoulli_(1.0 - self.p)
|
|
48
|
+
mask.div_(1.0 - self.p)
|
|
49
|
+
if step_mode != StepMode.MULTI_STATELESS:
|
|
50
|
+
self._mask = mask
|
|
51
|
+
return x * mask
|
|
52
|
+
|
|
53
|
+
def extra_repr(self) -> str:
|
|
54
|
+
return f"p={self.p}"
|
|
File without changes
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Optional
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
from spikinggamma.utils.enums import StepMode
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class SpikeConfig:
|
|
11
|
+
base_spike_threshold: float = 0.3
|
|
12
|
+
use_dynamic_threshold: bool = False # Set during inference, stateless in between samples
|
|
13
|
+
adaptation_mf: float = 0.3
|
|
14
|
+
spike_grad_multiplier: Optional[float] = None # will compute the multiplier during backward if None
|
|
15
|
+
saturation_gate_softness: float = 0. # can_saturate, disabled by default
|
|
16
|
+
saturation_gate_start_fraction: float = 0.9 # can_saturate
|
|
17
|
+
enable_saturation_gate: bool = True # can_saturate
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def can_saturate(self) -> bool:
|
|
21
|
+
return not (self.use_dynamic_threshold) and self.enable_saturation_gate
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class BaseSpikeFunction(nn.Module):
|
|
26
|
+
spike_threshold: torch.Tensor # may be shape () or neuron specific shape, note that this shape is of the input to the spike function, not the output!
|
|
27
|
+
|
|
28
|
+
def __init__(self, spike_config: SpikeConfig):
|
|
29
|
+
super().__init__()
|
|
30
|
+
self.spike_config = spike_config
|
|
31
|
+
self.register_buffer("spike_threshold", torch.tensor(spike_config.base_spike_threshold))
|
|
32
|
+
|
|
33
|
+
def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs):
|
|
34
|
+
# A checkpoint may hold one threshold per neuron, while a freshly constructed module holds a
|
|
35
|
+
# scalar. Torch silently copies only element 0 of a 1-d tensor into a 0-d buffer instead of
|
|
36
|
+
# raising, so match the stored shape first.
|
|
37
|
+
key = prefix + "spike_threshold"
|
|
38
|
+
if key in state_dict and state_dict[key].shape != self.spike_threshold.shape:
|
|
39
|
+
self.spike_threshold = self.spike_threshold.new_empty(state_dict[key].shape)
|
|
40
|
+
super()._load_from_state_dict(state_dict, prefix, *args, **kwargs)
|
|
41
|
+
|
|
42
|
+
def _forward(self, y: torch.Tensor, step_mode: StepMode) -> tuple[torch.Tensor, torch.Tensor]:
|
|
43
|
+
raise NotImplementedError
|
|
44
|
+
|
|
45
|
+
def forward(self, y: torch.Tensor, step_mode: StepMode = StepMode.SINGLE) -> tuple[torch.Tensor, torch.Tensor]:
|
|
46
|
+
if not self.is_step_mode_compatible(step_mode):
|
|
47
|
+
raise ValueError(f"Step mode {step_mode} not compatible with {self.__class__.__name__}")
|
|
48
|
+
# y can be (B, *shape) or (T, B, *shape)
|
|
49
|
+
y_hat_k, s = self._forward(y, step_mode)
|
|
50
|
+
return y_hat_k, s
|
|
51
|
+
|
|
52
|
+
def is_step_mode_compatible(self, step_mode: StepMode) -> bool:
|
|
53
|
+
return True
|
|
54
|
+
|
|
55
|
+
def reset(self):
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def detach(self):
|
|
59
|
+
pass
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
from spikinggamma.utils.enums import StepMode
|
|
5
|
+
from spikinggamma.spike.base import BaseSpikeFunction, SpikeConfig
|
|
6
|
+
from spikinggamma.spike.kernel.base import BaseKernelFunction, BaseKernelState, prototype_y_hat_k
|
|
7
|
+
|
|
8
|
+
# Takes input as spikes and passes them through the kernels.
|
|
9
|
+
|
|
10
|
+
class IdentitySpikeFunction(BaseSpikeFunction):
|
|
11
|
+
init_from_kernel_state: Optional[BaseKernelState] = None
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
kernel_function: BaseKernelFunction,
|
|
16
|
+
spike_config: SpikeConfig,
|
|
17
|
+
):
|
|
18
|
+
super().__init__(spike_config)
|
|
19
|
+
self.kernel_function = kernel_function
|
|
20
|
+
self.kernel_state = self.kernel_function.construct_kernel_state()
|
|
21
|
+
self.reset()
|
|
22
|
+
|
|
23
|
+
def reset(self):
|
|
24
|
+
self.kernel_state.reset()
|
|
25
|
+
self.init_from_kernel_state = None
|
|
26
|
+
|
|
27
|
+
def detach(self):
|
|
28
|
+
self.kernel_state.detach()
|
|
29
|
+
if self.init_from_kernel_state is not None:
|
|
30
|
+
self.init_from_kernel_state.detach()
|
|
31
|
+
|
|
32
|
+
def _forward(self, y: torch.Tensor, step_mode: StepMode) -> tuple[torch.Tensor, torch.Tensor]:
|
|
33
|
+
if step_mode == StepMode.SINGLE:
|
|
34
|
+
self.kernel_function.step(self.kernel_state)
|
|
35
|
+
self.kernel_function.add(self.kernel_state, y * 2 * self.spike_threshold)
|
|
36
|
+
y_hat_k = prototype_y_hat_k(self.kernel_state.compute_y_hat_k(), y.shape, self.kernel_function.n_kernels, y.device, y.dtype)
|
|
37
|
+
return y_hat_k, y
|
|
38
|
+
else:
|
|
39
|
+
init_state = self.init_from_kernel_state
|
|
40
|
+
if init_state is not None and not init_state.is_empty():
|
|
41
|
+
init_state.detach()
|
|
42
|
+
else:
|
|
43
|
+
init_state = None
|
|
44
|
+
|
|
45
|
+
kernel_state = self.kernel_function.construct_kernel_state()
|
|
46
|
+
kernel_state.initialize_like(y[0], self.kernel_function.n_kernels)
|
|
47
|
+
if init_state is not None:
|
|
48
|
+
kernel_state.init_from(init_state)
|
|
49
|
+
|
|
50
|
+
y_hat_k = torch.empty(
|
|
51
|
+
(*y.shape, self.kernel_function.n_kernels),
|
|
52
|
+
device=y.device,
|
|
53
|
+
dtype=y.dtype
|
|
54
|
+
)
|
|
55
|
+
for t in range(y.shape[0]):
|
|
56
|
+
self.kernel_function.step(kernel_state)
|
|
57
|
+
self.kernel_function.add(kernel_state, y[t] * 2 * self.spike_threshold)
|
|
58
|
+
y_hat_k[t] = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y[t].shape, self.kernel_function.n_kernels, y.device, y.dtype)
|
|
59
|
+
|
|
60
|
+
if step_mode == StepMode.MULTI_STATEFUL:
|
|
61
|
+
self.init_from_kernel_state = kernel_state
|
|
62
|
+
return y_hat_k, y
|
|
63
|
+
|
|
64
|
+
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Optional
|
|
3
|
+
import torch
|
|
4
|
+
|
|
5
|
+
@dataclass
|
|
6
|
+
class BaseKernelState:
|
|
7
|
+
def compute_y_hat_k(self) -> Optional[torch.Tensor]:
|
|
8
|
+
raise NotImplementedError
|
|
9
|
+
|
|
10
|
+
def is_empty(self) -> bool:
|
|
11
|
+
raise NotImplementedError
|
|
12
|
+
|
|
13
|
+
def is_matching_shape(self, x: torch.Tensor) -> bool:
|
|
14
|
+
raise NotImplementedError
|
|
15
|
+
|
|
16
|
+
def initialize_like(self, x: torch.Tensor, n_kernels: int):
|
|
17
|
+
raise NotImplementedError
|
|
18
|
+
|
|
19
|
+
def reset(self):
|
|
20
|
+
raise NotImplementedError
|
|
21
|
+
|
|
22
|
+
def detach(self):
|
|
23
|
+
raise NotImplementedError
|
|
24
|
+
|
|
25
|
+
def init_from(self, other: "BaseKernelState"):
|
|
26
|
+
raise NotImplementedError
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class YHatKernelState(BaseKernelState):
|
|
31
|
+
# A plain field: torch.compile (Dynamo) misses writes made through a property setter
|
|
32
|
+
# when methods read the field behind it, which made compiled encoders return zeros.
|
|
33
|
+
y_hat_k: Optional[torch.Tensor] = None
|
|
34
|
+
|
|
35
|
+
def compute_y_hat_k(self) -> Optional[torch.Tensor]:
|
|
36
|
+
if self.y_hat_k is None:
|
|
37
|
+
return None
|
|
38
|
+
return self.y_hat_k.clone()
|
|
39
|
+
|
|
40
|
+
def is_empty(self) -> bool:
|
|
41
|
+
return self.y_hat_k is None
|
|
42
|
+
|
|
43
|
+
def is_matching_shape(self, x: torch.Tensor) -> bool:
|
|
44
|
+
return self.y_hat_k is not None and self.y_hat_k.shape[:-1] == x.shape
|
|
45
|
+
|
|
46
|
+
def initialize_like(self, x: torch.Tensor, n_kernels: int):
|
|
47
|
+
self.y_hat_k = torch.zeros((*x.shape, n_kernels), device=x.device, dtype=x.dtype)
|
|
48
|
+
|
|
49
|
+
def reset(self):
|
|
50
|
+
self.y_hat_k = None
|
|
51
|
+
|
|
52
|
+
def detach(self):
|
|
53
|
+
if self.y_hat_k is not None:
|
|
54
|
+
self.y_hat_k = self.y_hat_k.detach()
|
|
55
|
+
|
|
56
|
+
def init_from(self, other: "YHatKernelState"):
|
|
57
|
+
if not isinstance(other, YHatKernelState):
|
|
58
|
+
raise TypeError("init_from expects an instance of YHatKernelState")
|
|
59
|
+
self.y_hat_k = other.y_hat_k.clone() if other.y_hat_k is not None else None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def prototype_y_hat_k(y_hat_k: Optional[torch.Tensor], prototype_shape: torch.Size, n_kernels: int, device: torch.device, dtype: torch.dtype) -> torch.Tensor:
|
|
63
|
+
if y_hat_k is not None:
|
|
64
|
+
return y_hat_k
|
|
65
|
+
else:
|
|
66
|
+
return torch.zeros((*prototype_shape, n_kernels), device=device, dtype=dtype)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class BaseKernelFunction:
|
|
70
|
+
n_kernels: int
|
|
71
|
+
|
|
72
|
+
def __init__(self, n_kernels: int):
|
|
73
|
+
super().__init__()
|
|
74
|
+
self.n_kernels = n_kernels
|
|
75
|
+
|
|
76
|
+
def construct_kernel_state(self) -> BaseKernelState:
|
|
77
|
+
raise NotImplementedError
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def normal_saturation_cutoff(self) -> torch.Tensor:
|
|
81
|
+
raise NotImplementedError
|
|
82
|
+
|
|
83
|
+
@torch.no_grad()
|
|
84
|
+
def step(
|
|
85
|
+
self,
|
|
86
|
+
state: BaseKernelState,
|
|
87
|
+
):
|
|
88
|
+
raise NotImplementedError
|
|
89
|
+
|
|
90
|
+
def add(self, state: BaseKernelState, x: torch.Tensor):
|
|
91
|
+
raise NotImplementedError
|
|
92
|
+
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from spikinggamma.spike.kernel.base import BaseKernelFunction, YHatKernelState
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def compute_linspace_bucket_decay_rates(
|
|
7
|
+
n_kernels: int,
|
|
8
|
+
decay_factor: float,
|
|
9
|
+
decay_start: float = 0.001,
|
|
10
|
+
decay_end: float = 0.86,
|
|
11
|
+
) -> torch.Tensor:
|
|
12
|
+
return torch.linspace(decay_start, decay_end, n_kernels) ** (decay_factor)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class GammaKernelFunction(BaseKernelFunction):
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
bucket_decay_rates: torch.Tensor,
|
|
19
|
+
sequential_buckets: bool = False,
|
|
20
|
+
):
|
|
21
|
+
super().__init__(n_kernels=bucket_decay_rates.shape[-1])
|
|
22
|
+
self.bucket_decay_rates = bucket_decay_rates
|
|
23
|
+
self.sequential_buckets = sequential_buckets
|
|
24
|
+
self.bucket_input_rates = 1.0 - self.bucket_decay_rates.clone()
|
|
25
|
+
if self.sequential_buckets and self.n_kernels > 1:
|
|
26
|
+
# Correct for already decayed input (one step)
|
|
27
|
+
self.bucket_input_rates[..., 1] = (
|
|
28
|
+
self.bucket_input_rates[..., 1] / self.bucket_decay_rates[..., 0]
|
|
29
|
+
)
|
|
30
|
+
self._normal_saturation_cutoff = self.n_kernels / (
|
|
31
|
+
1 - self.bucket_decay_rates[..., 0]
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def construct_kernel_state(self) -> YHatKernelState:
|
|
35
|
+
return YHatKernelState()
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def normal_saturation_cutoff(self) -> torch.Tensor:
|
|
39
|
+
return self._normal_saturation_cutoff
|
|
40
|
+
|
|
41
|
+
def step(
|
|
42
|
+
self,
|
|
43
|
+
state: YHatKernelState,
|
|
44
|
+
):
|
|
45
|
+
if state.is_empty():
|
|
46
|
+
return state
|
|
47
|
+
if self.bucket_decay_rates.device != state.y_hat_k.device:
|
|
48
|
+
self.bucket_decay_rates = self.bucket_decay_rates.to(state.y_hat_k.device)
|
|
49
|
+
self.bucket_input_rates = self.bucket_input_rates.to(state.y_hat_k.device)
|
|
50
|
+
|
|
51
|
+
feature_shape = state.y_hat_k.shape[:-1]
|
|
52
|
+
num_buckets = state.y_hat_k.shape[-1]
|
|
53
|
+
expanded_decay_rates = self.bucket_decay_rates.unsqueeze(0).expand(
|
|
54
|
+
feature_shape + (num_buckets,)
|
|
55
|
+
)
|
|
56
|
+
expanded_input_rates = self.bucket_input_rates.unsqueeze(0).expand(
|
|
57
|
+
feature_shape + (num_buckets,)
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
if self.sequential_buckets:
|
|
61
|
+
# In-place writes otherwise retain the previous tensor's autograd graph.
|
|
62
|
+
state.detach()
|
|
63
|
+
state.y_hat_k[..., 0] = state.y_hat_k[..., 0].detach() * expanded_decay_rates[..., 0]
|
|
64
|
+
|
|
65
|
+
for k in range(1, state.y_hat_k.shape[-1]):
|
|
66
|
+
state.y_hat_k[..., k] = (
|
|
67
|
+
state.y_hat_k[..., k].detach() * expanded_decay_rates[..., k]
|
|
68
|
+
+ state.y_hat_k[..., k - 1].detach() * expanded_input_rates[..., k]
|
|
69
|
+
)
|
|
70
|
+
else:
|
|
71
|
+
bucket_input = torch.zeros_like(state.y_hat_k)
|
|
72
|
+
bucket_input[..., 1:] = state.y_hat_k[..., :-1].detach() * expanded_input_rates[..., 1:]
|
|
73
|
+
|
|
74
|
+
state.y_hat_k = state.y_hat_k.detach() * expanded_decay_rates + bucket_input
|
|
75
|
+
|
|
76
|
+
def add(self, state: YHatKernelState, x: torch.Tensor):
|
|
77
|
+
if state.is_empty():
|
|
78
|
+
state.initialize_like(x, self.n_kernels)
|
|
79
|
+
if not state.is_matching_shape(x):
|
|
80
|
+
raise ValueError(
|
|
81
|
+
f"Shape mismatch: x has shape {x.shape}, but y_hat_k has shape {state.y_hat_k.shape[:-1]}."
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
state.y_hat_k[..., 0] = state.y_hat_k[..., 0] + x
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from typing import Optional, Union
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
@torch.no_grad()
|
|
5
|
+
def compute_saturation_gate(
|
|
6
|
+
y: torch.Tensor,
|
|
7
|
+
cutoff: Union[float, torch.Tensor],
|
|
8
|
+
softness: float,
|
|
9
|
+
start_fraction: float,
|
|
10
|
+
) -> torch.Tensor:
|
|
11
|
+
if torch.is_tensor(cutoff) and len(cutoff.shape) > 0:
|
|
12
|
+
if cutoff.shape == y.shape[1:]:
|
|
13
|
+
cutoff = cutoff.reshape((1, *cutoff.shape))
|
|
14
|
+
elif y.ndim >= 3 and cutoff.shape == y.shape[2:]:
|
|
15
|
+
cutoff = cutoff.reshape((1, 1, *cutoff.shape))
|
|
16
|
+
else:
|
|
17
|
+
expected_shapes = [tuple(y.shape[1:])]
|
|
18
|
+
if y.ndim >= 3:
|
|
19
|
+
expected_shapes.append(tuple(y.shape[2:]))
|
|
20
|
+
raise ValueError(
|
|
21
|
+
f"cutoff shape {cutoff.shape} does not match y feature shapes "
|
|
22
|
+
f"{expected_shapes}"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
if softness <= 0:
|
|
26
|
+
gate = (y < cutoff).to(y.dtype)
|
|
27
|
+
else:
|
|
28
|
+
start = start_fraction * cutoff
|
|
29
|
+
transition_width = softness * (-start + cutoff) + 1e-8
|
|
30
|
+
|
|
31
|
+
excess = (y - start) / transition_width
|
|
32
|
+
gate = torch.where(y <= start, torch.ones_like(y), torch.exp(-excess))
|
|
33
|
+
|
|
34
|
+
if gate.shape != y.shape:
|
|
35
|
+
gate = gate.expand_as(y)
|
|
36
|
+
return gate
|
|
37
|
+
|
|
38
|
+
@torch.no_grad()
|
|
39
|
+
def apply_saturation_cutoff(dL_dy: torch.Tensor, y: Optional[torch.Tensor], saturation_cutoff: Optional[Union[float, torch.Tensor]],
|
|
40
|
+
saturation_gate_softness: float, saturation_gate_start_fraction: float, can_saturate: bool) -> torch.Tensor:
|
|
41
|
+
if not can_saturate:
|
|
42
|
+
return dL_dy
|
|
43
|
+
elif y is None or saturation_cutoff is None:
|
|
44
|
+
raise ValueError("y and saturation_cutoff must be provided if can_saturate is True")
|
|
45
|
+
gate = compute_saturation_gate(
|
|
46
|
+
y,
|
|
47
|
+
cutoff=saturation_cutoff,
|
|
48
|
+
softness=saturation_gate_softness,
|
|
49
|
+
start_fraction=saturation_gate_start_fraction,
|
|
50
|
+
)
|
|
51
|
+
return dL_dy * gate
|
|
52
|
+
|
|
53
|
+
@torch.no_grad()
|
|
54
|
+
def y_hat_backward_saturated(dL_dy_hat_k: torch.Tensor, y: Optional[torch.Tensor], saturation_cutoff: Optional[Union[float, torch.Tensor]], n_kernels: int,
|
|
55
|
+
saturation_gate_softness: float, saturation_gate_start_fraction: float, can_saturate: bool, spike_grad_multiplier: Optional[float] = None) -> torch.Tensor:
|
|
56
|
+
if spike_grad_multiplier is None:
|
|
57
|
+
spike_grad_multiplier = 1.0/n_kernels
|
|
58
|
+
dL_dy = dL_dy_hat_k.sum(dim=-1) * spike_grad_multiplier
|
|
59
|
+
dL_dy_saturated = apply_saturation_cutoff(dL_dy, y, saturation_cutoff, saturation_gate_softness, saturation_gate_start_fraction, can_saturate)
|
|
60
|
+
return dL_dy_saturated
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
from typing import Optional, Union
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
from spikinggamma.spike.saturation import y_hat_backward_saturated
|
|
5
|
+
from spikinggamma.utils.enums import StepMode
|
|
6
|
+
from spikinggamma.spike.base import BaseSpikeFunction, SpikeConfig
|
|
7
|
+
from spikinggamma.spike.kernel.base import BaseKernelFunction, BaseKernelState, prototype_y_hat_k
|
|
8
|
+
|
|
9
|
+
@torch.no_grad()
|
|
10
|
+
def compute_spikes_from_signal_sequential(
|
|
11
|
+
y: torch.Tensor,
|
|
12
|
+
spike_config: SpikeConfig,
|
|
13
|
+
kernel_function: BaseKernelFunction,
|
|
14
|
+
spike_threshold: Union[float, torch.Tensor],
|
|
15
|
+
init_from_kernel_state: Optional[BaseKernelState]
|
|
16
|
+
):
|
|
17
|
+
T, B, *S = y.shape
|
|
18
|
+
n_kernels = kernel_function.n_kernels
|
|
19
|
+
dtype = y.dtype
|
|
20
|
+
kernel_state = kernel_function.construct_kernel_state()
|
|
21
|
+
kernel_state.initialize_like(y[0], n_kernels)
|
|
22
|
+
|
|
23
|
+
if init_from_kernel_state is not None and not init_from_kernel_state.is_empty():
|
|
24
|
+
init_y_hat_k = init_from_kernel_state.compute_y_hat_k()
|
|
25
|
+
if init_y_hat_k.shape != (B, *S, n_kernels):
|
|
26
|
+
raise ValueError(f"init_y_hat_k must have shape {(B, *S, n_kernels)}, but got {init_y_hat_k.shape}")
|
|
27
|
+
kernel_state.init_from(init_from_kernel_state)
|
|
28
|
+
|
|
29
|
+
y_hat_k = torch.empty((T, B, *S, n_kernels), device=y.device, dtype=dtype)
|
|
30
|
+
s_out = torch.empty((T, B, *S), device=y.device, dtype=dtype)
|
|
31
|
+
for t in range(T):
|
|
32
|
+
kernel_function.step(kernel_state)
|
|
33
|
+
y_hat_k_t = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y[t].shape, kernel_function.n_kernels, y.device, y.dtype)
|
|
34
|
+
y_hat_t = y_hat_k_t.sum(dim=-1)
|
|
35
|
+
z = y[t] - y_hat_t
|
|
36
|
+
if spike_config.use_dynamic_threshold:
|
|
37
|
+
threshold = spike_threshold + y_hat_t * (spike_config.adaptation_mf)
|
|
38
|
+
else:
|
|
39
|
+
threshold = spike_threshold
|
|
40
|
+
s_t = (z > threshold).to(dtype)
|
|
41
|
+
kernel_function.add(kernel_state, s_t * threshold * 2)
|
|
42
|
+
y_hat_k[t] = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y[t].shape, kernel_function.n_kernels, y.device, y.dtype)
|
|
43
|
+
s_out[t] = s_t
|
|
44
|
+
|
|
45
|
+
return y_hat_k, s_out, kernel_state
|
|
46
|
+
|
|
47
|
+
class MultiStepSigmaDeltaYHatFunction(torch.autograd.Function):
|
|
48
|
+
@staticmethod
|
|
49
|
+
def forward(
|
|
50
|
+
ctx,
|
|
51
|
+
y: torch.Tensor,
|
|
52
|
+
spike_config: SpikeConfig,
|
|
53
|
+
kernel_function: BaseKernelFunction,
|
|
54
|
+
spike_threshold: Union[float, torch.Tensor],
|
|
55
|
+
init_from_kernel_state: Optional[BaseKernelState],
|
|
56
|
+
saturation_cutoff: Optional[torch.Tensor],
|
|
57
|
+
):
|
|
58
|
+
if spike_config.can_saturate and saturation_cutoff is None:
|
|
59
|
+
raise ValueError("saturation_cutoff must be provided if spike_config.can_saturate is True")
|
|
60
|
+
if spike_config.can_saturate:
|
|
61
|
+
ctx.save_for_backward(y, saturation_cutoff)
|
|
62
|
+
ctx.n_kernels = kernel_function.n_kernels
|
|
63
|
+
ctx.spike_grad_multiplier = spike_config.spike_grad_multiplier
|
|
64
|
+
ctx.saturation_gate_softness = spike_config.saturation_gate_softness
|
|
65
|
+
ctx.saturation_gate_start_fraction = spike_config.saturation_gate_start_fraction
|
|
66
|
+
ctx.can_saturate = spike_config.can_saturate
|
|
67
|
+
return compute_spikes_from_signal_sequential(
|
|
68
|
+
y,
|
|
69
|
+
spike_config,
|
|
70
|
+
kernel_function,
|
|
71
|
+
spike_threshold,
|
|
72
|
+
init_from_kernel_state
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def backward(ctx, dL_dy_hat_k: torch.Tensor, *_):
|
|
77
|
+
if ctx.can_saturate:
|
|
78
|
+
y, saturation_cutoff = ctx.saved_tensors
|
|
79
|
+
else:
|
|
80
|
+
y = None
|
|
81
|
+
saturation_cutoff = None
|
|
82
|
+
dL_dy = y_hat_backward_saturated(dL_dy_hat_k, y, saturation_cutoff, ctx.n_kernels,
|
|
83
|
+
ctx.saturation_gate_softness, ctx.saturation_gate_start_fraction,
|
|
84
|
+
ctx.can_saturate, ctx.spike_grad_multiplier)
|
|
85
|
+
return dL_dy, None, None, None, None, None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class SingleStepSigmaDeltaYHatFunction(torch.autograd.Function):
|
|
89
|
+
|
|
90
|
+
@staticmethod
|
|
91
|
+
def forward(
|
|
92
|
+
ctx,
|
|
93
|
+
y: torch.Tensor,
|
|
94
|
+
spike_config: SpikeConfig,
|
|
95
|
+
kernel_function: BaseKernelFunction,
|
|
96
|
+
kernel_state: BaseKernelState,
|
|
97
|
+
spike_threshold: Union[float, torch.Tensor],
|
|
98
|
+
saturation_cutoff: Optional[torch.Tensor],
|
|
99
|
+
):
|
|
100
|
+
if spike_config.can_saturate and saturation_cutoff is None:
|
|
101
|
+
raise ValueError("saturation_cutoff must be provided if spike_config.can_saturate is True")
|
|
102
|
+
if spike_config.can_saturate:
|
|
103
|
+
ctx.save_for_backward(y, saturation_cutoff)
|
|
104
|
+
ctx.n_kernels = kernel_function.n_kernels
|
|
105
|
+
ctx.spike_grad_multiplier = spike_config.spike_grad_multiplier
|
|
106
|
+
ctx.saturation_gate_softness = spike_config.saturation_gate_softness
|
|
107
|
+
ctx.saturation_gate_start_fraction = spike_config.saturation_gate_start_fraction
|
|
108
|
+
ctx.can_saturate = spike_config.can_saturate
|
|
109
|
+
|
|
110
|
+
kernel_function.step(kernel_state)
|
|
111
|
+
y_hat_k = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y.shape, kernel_function.n_kernels, y.device, y.dtype)
|
|
112
|
+
y_hat = y_hat_k.sum(dim=-1)
|
|
113
|
+
z = y - y_hat
|
|
114
|
+
if spike_config.use_dynamic_threshold:
|
|
115
|
+
threshold = spike_threshold + y_hat * (spike_config.adaptation_mf)
|
|
116
|
+
else:
|
|
117
|
+
threshold = spike_threshold
|
|
118
|
+
s_out = (z > threshold).to(y.dtype)
|
|
119
|
+
kernel_function.add(kernel_state, s_out * threshold * 2)
|
|
120
|
+
y_hat_k = prototype_y_hat_k(kernel_state.compute_y_hat_k(), y.shape, kernel_function.n_kernels, y.device, y.dtype)
|
|
121
|
+
return y_hat_k, s_out
|
|
122
|
+
|
|
123
|
+
@staticmethod
|
|
124
|
+
def backward(ctx, dL_dy_hat_k: torch.Tensor, *_):
|
|
125
|
+
if ctx.can_saturate:
|
|
126
|
+
y, saturation_cutoff = ctx.saved_tensors
|
|
127
|
+
else:
|
|
128
|
+
y = None
|
|
129
|
+
saturation_cutoff = None
|
|
130
|
+
dL_dy = y_hat_backward_saturated(
|
|
131
|
+
dL_dy_hat_k,
|
|
132
|
+
y,
|
|
133
|
+
saturation_cutoff,
|
|
134
|
+
ctx.n_kernels,
|
|
135
|
+
ctx.saturation_gate_softness,
|
|
136
|
+
ctx.saturation_gate_start_fraction,
|
|
137
|
+
ctx.can_saturate,
|
|
138
|
+
ctx.spike_grad_multiplier
|
|
139
|
+
)
|
|
140
|
+
return dL_dy, None, None, None, None, None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class SigmaDeltaSpikeFunction(BaseSpikeFunction):
|
|
144
|
+
|
|
145
|
+
def __init__(
|
|
146
|
+
self,
|
|
147
|
+
kernel_function: BaseKernelFunction,
|
|
148
|
+
spike_config: SpikeConfig,
|
|
149
|
+
):
|
|
150
|
+
super().__init__(spike_config)
|
|
151
|
+
self.kernel_function = kernel_function
|
|
152
|
+
self.kernel_state = self.kernel_function.construct_kernel_state()
|
|
153
|
+
self.reset()
|
|
154
|
+
|
|
155
|
+
def reset(self):
|
|
156
|
+
self.kernel_state.reset()
|
|
157
|
+
self.init_from_kernel_state = None
|
|
158
|
+
|
|
159
|
+
def detach(self):
|
|
160
|
+
self.kernel_state.detach()
|
|
161
|
+
if self.init_from_kernel_state is not None:
|
|
162
|
+
self.init_from_kernel_state.detach()
|
|
163
|
+
|
|
164
|
+
def compute_saturation_cutoff(self, device: torch.device):
|
|
165
|
+
normal_cutoff = self.kernel_function.normal_saturation_cutoff.to(
|
|
166
|
+
device=device,
|
|
167
|
+
dtype=self.spike_threshold.dtype,
|
|
168
|
+
)
|
|
169
|
+
return normal_cutoff * self.spike_threshold.to(device) * 2.0
|
|
170
|
+
|
|
171
|
+
def _forward(self, y: torch.Tensor, step_mode: StepMode) -> tuple[torch.Tensor, torch.Tensor]:
|
|
172
|
+
saturation_cutoff = self.compute_saturation_cutoff(y.device)
|
|
173
|
+
if step_mode == StepMode.SINGLE:
|
|
174
|
+
y_hat_k, s_out = SingleStepSigmaDeltaYHatFunction.apply(
|
|
175
|
+
y,
|
|
176
|
+
self.spike_config,
|
|
177
|
+
self.kernel_function,
|
|
178
|
+
self.kernel_state,
|
|
179
|
+
self.spike_threshold,
|
|
180
|
+
saturation_cutoff
|
|
181
|
+
)
|
|
182
|
+
return y_hat_k, s_out
|
|
183
|
+
else:
|
|
184
|
+
if self.init_from_kernel_state is not None:
|
|
185
|
+
self.init_from_kernel_state.detach()
|
|
186
|
+
y_hat_k, s_out, kernel_state = MultiStepSigmaDeltaYHatFunction.apply(
|
|
187
|
+
y,
|
|
188
|
+
self.spike_config,
|
|
189
|
+
self.kernel_function,
|
|
190
|
+
self.spike_threshold,
|
|
191
|
+
self.init_from_kernel_state,
|
|
192
|
+
saturation_cutoff
|
|
193
|
+
)
|
|
194
|
+
if step_mode == StepMode.MULTI_STATEFUL:
|
|
195
|
+
self.init_from_kernel_state = kernel_state
|
|
196
|
+
return y_hat_k, s_out
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TimeSliceTransform:
|
|
5
|
+
def __init__(self, time_steps, time_dim=0):
|
|
6
|
+
self.time_steps = time_steps
|
|
7
|
+
self.time_dim = time_dim
|
|
8
|
+
|
|
9
|
+
def __call__(self, x: torch.Tensor):
|
|
10
|
+
tdim = self.time_dim
|
|
11
|
+
size = x.shape[tdim]
|
|
12
|
+
if size > self.time_steps:
|
|
13
|
+
return x.narrow(tdim, 0, self.time_steps)
|
|
14
|
+
if size < self.time_steps:
|
|
15
|
+
pad_shape = list(x.shape)
|
|
16
|
+
pad_shape[tdim] = self.time_steps - size
|
|
17
|
+
pad = torch.zeros(pad_shape, dtype=x.dtype, device=x.device)
|
|
18
|
+
return torch.cat([x, pad], dim=tdim)
|
|
19
|
+
return x
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class BinarizeSpikeCounts:
|
|
23
|
+
def __call__(self, x: torch.Tensor):
|
|
24
|
+
return (x > 0).to(x.dtype)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from spikinggamma.utils.datasets.common import TimeSliceTransform
|
|
2
|
+
import torch
|
|
3
|
+
import tonic
|
|
4
|
+
from tonic import transforms, DiskCachedDataset
|
|
5
|
+
|
|
6
|
+
DVSGESTURE_SAMPLE_LENGTH_US = 6000000
|
|
7
|
+
|
|
8
|
+
def get_dvsgesture_dataset(data_path, cache_path, timestep_size = 10000, sensor_width=32, cache=True):
|
|
9
|
+
data_path.mkdir(parents=True, exist_ok=True)
|
|
10
|
+
cache_path.mkdir(parents=True, exist_ok=True)
|
|
11
|
+
input_shape = (2, sensor_width, sensor_width)
|
|
12
|
+
transform = transforms.Compose([
|
|
13
|
+
transforms.Denoise(filter_time=10000),
|
|
14
|
+
tonic.transforms.Downsample(sensor_size=tonic.datasets.DVSGesture.sensor_size, target_size=(sensor_width, sensor_width)),
|
|
15
|
+
transforms.ToFrame(sensor_size=(sensor_width, sensor_width, 2), time_window=timestep_size),
|
|
16
|
+
torch.from_numpy,
|
|
17
|
+
TimeSliceTransform(time_steps=DVSGESTURE_SAMPLE_LENGTH_US // timestep_size)
|
|
18
|
+
])
|
|
19
|
+
train_dataset = tonic.datasets.DVSGesture(save_to=str(data_path), transform=transform, train=True)
|
|
20
|
+
test_dataset = tonic.datasets.DVSGesture(save_to=str(data_path), transform=transform, train=False)
|
|
21
|
+
if cache:
|
|
22
|
+
train_cache_path = cache_path / f"DVSGesture_t{timestep_size}_s{sensor_width}_train"
|
|
23
|
+
test_cache_path = cache_path / f"DVSGesture_t{timestep_size}_s{sensor_width}_test"
|
|
24
|
+
train_dataset = DiskCachedDataset(train_dataset, transform=torch.from_numpy, cache_path=str(train_cache_path))
|
|
25
|
+
test_dataset = DiskCachedDataset(test_dataset, transform=torch.from_numpy, cache_path=str(test_cache_path))
|
|
26
|
+
n_outputs = 11
|
|
27
|
+
T = DVSGESTURE_SAMPLE_LENGTH_US // timestep_size
|
|
28
|
+
return train_dataset, test_dataset, input_shape, n_outputs, T
|
|
29
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import tonic
|
|
3
|
+
from tonic import transforms, DiskCachedDataset
|
|
4
|
+
|
|
5
|
+
from spikinggamma.utils.datasets.common import BinarizeSpikeCounts, TimeSliceTransform
|
|
6
|
+
|
|
7
|
+
SHD_SAMPLE_LENGTH_US = 900000
|
|
8
|
+
SHD_SOURCE_SENSOR_WIDTH = 700
|
|
9
|
+
SHD_N_OUTPUTS = 20
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_shd_dataset(data_path, cache_path, timestep_size = 3600, sensor_width = 140, cache = True, one_spike_per_channel_per_timestep = False):
|
|
13
|
+
data_path.mkdir(parents=True, exist_ok=True)
|
|
14
|
+
cache_path.mkdir(parents=True, exist_ok=True)
|
|
15
|
+
|
|
16
|
+
sample_length = SHD_SAMPLE_LENGTH_US
|
|
17
|
+
T = sample_length // timestep_size
|
|
18
|
+
|
|
19
|
+
frame_transforms = [
|
|
20
|
+
transforms.Downsample(spatial_factor=sensor_width / SHD_SOURCE_SENSOR_WIDTH),
|
|
21
|
+
transforms.ToFrame(sensor_size=(sensor_width, 1, 1), time_window=timestep_size),
|
|
22
|
+
torch.from_numpy,
|
|
23
|
+
]
|
|
24
|
+
if one_spike_per_channel_per_timestep:
|
|
25
|
+
frame_transforms.append(BinarizeSpikeCounts())
|
|
26
|
+
frame_transforms.append(TimeSliceTransform(time_steps=T))
|
|
27
|
+
transform = transforms.Compose(frame_transforms)
|
|
28
|
+
|
|
29
|
+
train_dataset = tonic.datasets.SHD(save_to=str(data_path), transform=transform, train=True)
|
|
30
|
+
test_dataset = tonic.datasets.SHD(save_to=str(data_path), transform=transform, train=False)
|
|
31
|
+
if cache:
|
|
32
|
+
cache_suffix = "_binary" if one_spike_per_channel_per_timestep else ""
|
|
33
|
+
train_dataset = DiskCachedDataset(
|
|
34
|
+
train_dataset,
|
|
35
|
+
transform=torch.from_numpy,
|
|
36
|
+
cache_path=str(cache_path / f"SHD_t{timestep_size}_s{sensor_width}{cache_suffix}_train"),
|
|
37
|
+
)
|
|
38
|
+
test_dataset = DiskCachedDataset(
|
|
39
|
+
test_dataset,
|
|
40
|
+
transform=torch.from_numpy,
|
|
41
|
+
cache_path=str(cache_path / f"SHD_t{timestep_size}_s{sensor_width}{cache_suffix}_test"),
|
|
42
|
+
)
|
|
43
|
+
input_shape = (sensor_width, 1, 1)
|
|
44
|
+
n_outputs = SHD_N_OUTPUTS
|
|
45
|
+
return train_dataset, test_dataset, input_shape, n_outputs, T
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import tonic
|
|
3
|
+
from tonic import DiskCachedDataset, transforms
|
|
4
|
+
|
|
5
|
+
from spikinggamma.utils.datasets.common import (
|
|
6
|
+
BinarizeSpikeCounts,
|
|
7
|
+
TimeSliceTransform,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
SSC_SAMPLE_LENGTH_US = 1000000
|
|
11
|
+
SSC_SOURCE_SENSOR_WIDTH = 700
|
|
12
|
+
SSC_N_OUTPUTS = 35
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_ssc_dataset(
|
|
16
|
+
data_path,
|
|
17
|
+
cache_path,
|
|
18
|
+
timestep_size=3600,
|
|
19
|
+
sensor_width=140,
|
|
20
|
+
cache=True,
|
|
21
|
+
one_spike_per_channel_per_timestep=False,
|
|
22
|
+
):
|
|
23
|
+
"""Load SSC train/test splits with the same framing options as SHD."""
|
|
24
|
+
data_path.mkdir(parents=True, exist_ok=True)
|
|
25
|
+
cache_path.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
|
|
27
|
+
T = SSC_SAMPLE_LENGTH_US // timestep_size
|
|
28
|
+
frame_transforms = [
|
|
29
|
+
transforms.Downsample(spatial_factor=sensor_width / SSC_SOURCE_SENSOR_WIDTH),
|
|
30
|
+
transforms.ToFrame(sensor_size=(sensor_width, 1, 1), time_window=timestep_size),
|
|
31
|
+
torch.from_numpy,
|
|
32
|
+
]
|
|
33
|
+
if one_spike_per_channel_per_timestep:
|
|
34
|
+
frame_transforms.append(BinarizeSpikeCounts())
|
|
35
|
+
frame_transforms.append(TimeSliceTransform(time_steps=T))
|
|
36
|
+
transform = transforms.Compose(frame_transforms)
|
|
37
|
+
|
|
38
|
+
train_dataset = tonic.datasets.SSC(
|
|
39
|
+
save_to=str(data_path), transform=transform, split="train",
|
|
40
|
+
)
|
|
41
|
+
test_dataset = tonic.datasets.SSC(
|
|
42
|
+
save_to=str(data_path), transform=transform, split="test",
|
|
43
|
+
)
|
|
44
|
+
if cache:
|
|
45
|
+
cache_suffix = "_binary" if one_spike_per_channel_per_timestep else ""
|
|
46
|
+
train_dataset = DiskCachedDataset(
|
|
47
|
+
train_dataset,
|
|
48
|
+
transform=torch.from_numpy,
|
|
49
|
+
cache_path=str(cache_path / f"SSC_t{timestep_size}_s{sensor_width}{cache_suffix}_train"),
|
|
50
|
+
)
|
|
51
|
+
test_dataset = DiskCachedDataset(
|
|
52
|
+
test_dataset,
|
|
53
|
+
transform=torch.from_numpy,
|
|
54
|
+
cache_path=str(cache_path / f"SSC_t{timestep_size}_s{sensor_width}{cache_suffix}_test"),
|
|
55
|
+
)
|
|
56
|
+
input_shape = (sensor_width, 1, 1)
|
|
57
|
+
return train_dataset, test_dataset, input_shape, SSC_N_OUTPUTS, T
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
from typing import Optional
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
from spikinggamma.utils.enums import StepMode
|
|
7
|
+
|
|
8
|
+
class MultiStepLayerWrapper(nn.Module):
|
|
9
|
+
def __init__(self, layer: nn.Module):
|
|
10
|
+
super().__init__()
|
|
11
|
+
self.layer = layer
|
|
12
|
+
|
|
13
|
+
def forward(self, x, step_mode: StepMode = StepMode.MULTI_STATELESS):
|
|
14
|
+
if step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS:
|
|
15
|
+
T, B, *S = x.shape
|
|
16
|
+
x = x.reshape(T * B, *S) # Combine time and batch dimensions
|
|
17
|
+
x = self.layer(x)
|
|
18
|
+
if step_mode == StepMode.MULTI_STATEFUL or step_mode == StepMode.MULTI_STATELESS:
|
|
19
|
+
x = x.reshape(T, B, *x.shape[1:]) # Reshape back to (T, B, ...)
|
|
20
|
+
return x
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MultiStepCrossEntropyLoss(nn.Module):
|
|
24
|
+
"""
|
|
25
|
+
Args:
|
|
26
|
+
- y: (T, B, C) logits
|
|
27
|
+
- target: (B,) integer class labels
|
|
28
|
+
Optional:
|
|
29
|
+
- mask: (T, B) boolean to ignore padded time steps
|
|
30
|
+
"""
|
|
31
|
+
def __init__(self):
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.inner_loss = nn.CrossEntropyLoss()
|
|
34
|
+
|
|
35
|
+
def forward(self, y, target, mask: Optional[torch.Tensor] = None):
|
|
36
|
+
T, B, C = y.shape
|
|
37
|
+
y_flat = y.reshape(T * B, C)
|
|
38
|
+
tgt_TB = target[None, :].expand(T, B).reshape(T * B)
|
|
39
|
+
|
|
40
|
+
if mask is not None:
|
|
41
|
+
mask_flat = mask.reshape(T * B)
|
|
42
|
+
y_flat = y_flat[mask_flat]
|
|
43
|
+
tgt_TB = tgt_TB[mask_flat]
|
|
44
|
+
|
|
45
|
+
return self.inner_loss(y_flat, tgt_TB)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def multi_split_step_forward(data: torch.Tensor, labels: torch.Tensor, model: nn.Module, loss_fn: nn.Module, T_per_split: int,
|
|
49
|
+
detach_y = True, detach_y_hat = True, detach_s = True):
|
|
50
|
+
"""
|
|
51
|
+
Perform a forward and backward pass in multiple splits along the time dimension.
|
|
52
|
+
This is useful when the full sequence does not fit in memory.
|
|
53
|
+
Args:
|
|
54
|
+
- data: (T, B, ...) input data
|
|
55
|
+
- labels: what is passed to loss_fn
|
|
56
|
+
- model: the model
|
|
57
|
+
- loss_fn: loss function, should return a scalar
|
|
58
|
+
- T_per_split: number of timesteps per split, must be in [1, T] and ideally divide T evenly
|
|
59
|
+
- detach_y, detach_y_hat, detach_s: whether to detach these outputs from the computation graph to save memory
|
|
60
|
+
Returns:
|
|
61
|
+
- y: (T, B, ...) neuron signal
|
|
62
|
+
- y_hat: (T, B, ..., K) bucket outputs, for K buckets
|
|
63
|
+
- s: (T, B, ...) spike outputs
|
|
64
|
+
- total_loss: scalar loss value
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
# Assumes data is (T, B, *S)
|
|
68
|
+
if T_per_split < 1 or T_per_split > data.shape[0]:
|
|
69
|
+
raise ValueError("T_per_split must be in [1, T]")
|
|
70
|
+
if data.shape[0] % T_per_split != 0:
|
|
71
|
+
warnings.warn(f"T_per_split {T_per_split} does not evenly divide T {data.shape[0]}. The last split will be smaller. This may affect performance when using CUDA graphs.", stacklevel=2)
|
|
72
|
+
|
|
73
|
+
y_list, y_hat_list, s_list = [], [], []
|
|
74
|
+
total_loss = 0
|
|
75
|
+
for t_start in range(0, data.shape[0], T_per_split):
|
|
76
|
+
t_end = min(t_start + T_per_split, data.shape[0])
|
|
77
|
+
data_split = data[t_start:t_end]
|
|
78
|
+
y, y_hat, s = model(data_split, step_mode=StepMode.MULTI_STATEFUL)
|
|
79
|
+
loss = loss_fn(y, labels) * (data_split.shape[0] / data.shape[0])
|
|
80
|
+
loss.backward()
|
|
81
|
+
y_list.append(y.detach() if detach_y else y)
|
|
82
|
+
y_hat_list.append(y_hat.detach() if detach_y_hat else y_hat)
|
|
83
|
+
s_list.append(s.detach() if detach_s else s)
|
|
84
|
+
total_loss += loss.item()
|
|
85
|
+
|
|
86
|
+
y = torch.cat(y_list, dim=0)
|
|
87
|
+
y_hat = torch.cat(y_hat_list, dim=0)
|
|
88
|
+
s = torch.cat(s_list, dim=0)
|
|
89
|
+
return y, y_hat, s, total_loss
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spikinggamma
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Use SpikingGamma with PyTorch
|
|
5
|
+
Author-email: Roel Koopman <roel.koopman@cwi.nl>, Sebastian Otte <sebastian.otte@uni-luebeck.de>, Sander Bohte <s.m.bohte@cwi.nl>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RoelMK/SpikingGamma
|
|
8
|
+
Project-URL: Issues, https://github.com/RoelMK/SpikingGamma/issues
|
|
9
|
+
Keywords: spikinggamma,spiking neural networks,snn,pytorch
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: torch
|
|
18
|
+
Provides-Extra: datasets
|
|
19
|
+
Requires-Dist: tonic; extra == "datasets"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# Introduction
|
|
24
|
+
This package allows you to directly use the SpikingGamma neuron model in your neural networks. It works on top of PyTorch.
|
|
25
|
+
|
|
26
|
+
# Installation
|
|
27
|
+
SpikingGamma is not yet on PyPI. At this moment, only installation from the source is supported.
|
|
28
|
+
|
|
29
|
+
To install directly from the source:
|
|
30
|
+
```console
|
|
31
|
+
pip install git+https://github.com/RoelMK/SpikingGamma
|
|
32
|
+
```
|
|
33
|
+
or
|
|
34
|
+
```console
|
|
35
|
+
git clone https://github.com/RoelMK/SpikingGamma
|
|
36
|
+
cd SpikingGamma
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
PyTorch is installed automatically if it is missing; to use a specific CUDA build, install PyTorch first.
|
|
40
|
+
|
|
41
|
+
The dataset loaders in `spikinggamma.utils.datasets` and the example notebooks also need `tonic`, which the `datasets` extra installs:
|
|
42
|
+
```console
|
|
43
|
+
pip install "spikinggamma[datasets] @ git+https://github.com/RoelMK/SpikingGamma"
|
|
44
|
+
```
|
|
45
|
+
or `pip install -e ".[datasets]"` from a clone.
|
|
46
|
+
|
|
47
|
+
# Tutorials
|
|
48
|
+
You can start by exploring the notebooks in the `examples` folder. Each example is a Jupyter notebook that can be run directly in a Jupyter environment.
|
|
49
|
+
|
|
50
|
+
| Notebook | Description |
|
|
51
|
+
|---|---|
|
|
52
|
+
| 1_getting_started.ipynb | Learn the basics: train and test a simple model. |
|
|
53
|
+
| 2_scaling_up.ipynb | Build and train a larger CNN. |
|
|
54
|
+
| 3_speeding_up.ipynb | Accelerate training through parallelization. |
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
spikinggamma/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
spikinggamma/neuron.py,sha256=DybzoUyqkYrLlGBgB6bi3Qcj2iElYLW1W54CrrDZk1A,1700
|
|
3
|
+
spikinggamma/dendrite/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
spikinggamma/dendrite/base.py,sha256=RPCRXKybMCb081-N4MVwPBJqZtwf0ZOQh3HY74BRgFM,1464
|
|
5
|
+
spikinggamma/dendrite/split.py,sha256=37EWhYkNXt-dCedRVTABZTj0gErqwDtq_c2lk6HxL8U,1758
|
|
6
|
+
spikinggamma/dendrite/synapse/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
spikinggamma/dendrite/synapse/base.py,sha256=agUW2a85jHJN1676EIXJFU6A6CJCKLy7XgUp3AtmuoI,37
|
|
8
|
+
spikinggamma/dendrite/synapse/conv2d.py,sha256=qn0CG1v7zvFZxTFovBnqmI8PiFnNGawt9UKi38wNXig,1177
|
|
9
|
+
spikinggamma/dendrite/synapse/linear.py,sha256=fN1XSK030JlckJqJixEKymugn7AHjYaqu31YGFc25Ac,619
|
|
10
|
+
spikinggamma/signal/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
spikinggamma/signal/activation.py,sha256=TETHkbAzEQbrWhZiatF4p70MjpUMWwEmZDhY4gwKfrc,519
|
|
12
|
+
spikinggamma/signal/base.py,sha256=PlpLE8RYmdb5dDrHufkOXpwOY_O-HC0e87mJi_cc730,832
|
|
13
|
+
spikinggamma/signal/default.py,sha256=L-lVEYOav4_ytpEi3_gMzEADwP_rNfGBeArvdtyBhX4,900
|
|
14
|
+
spikinggamma/signal/dropout.py,sha256=MauY-zsZtRsvAvNi7PSqFuVU_3x0Dwlg-jg1g2zM0a8,1768
|
|
15
|
+
spikinggamma/spike/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
|
+
spikinggamma/spike/base.py,sha256=OKzF5pKj2AV6bbRAASLgZzBiWVaNEX8ExEtwn0tUX24,2489
|
|
17
|
+
spikinggamma/spike/identity.py,sha256=1MFYOYvw4nDmM5-3TsSI9wmPbnaLckWx69L4BA4WF8g,2550
|
|
18
|
+
spikinggamma/spike/saturation.py,sha256=WIPAN204JXTAM107NR4acCzj2xZ7hpJXOKD_jlRT6O0,2543
|
|
19
|
+
spikinggamma/spike/sigma_delta.py,sha256=v7-v4NpqjVhIriotnElh5UZ3eYdy0lKzkDf9ooEvz_E,8013
|
|
20
|
+
spikinggamma/spike/kernel/base.py,sha256=aWOQDJtul877UpawyFeg4c0OLCyDbz1xs0bjQEadfVc,2768
|
|
21
|
+
spikinggamma/spike/kernel/gamma.py,sha256=UwL8cR3V1anObI3KS86LLBjj7l-z6sGvkU7SRbOq_sk,3255
|
|
22
|
+
spikinggamma/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
|
+
spikinggamma/utils/enums.py,sha256=3QYULKZ3yOzbrry0xX0qrWdXKxsyOvswvgKkAtDZJUs,151
|
|
24
|
+
spikinggamma/utils/helpers.py,sha256=uNr1-l8m_NCDQMlOnWCZMXin6hrWNYkpvE_o_Dxkz3o,3537
|
|
25
|
+
spikinggamma/utils/datasets/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
|
+
spikinggamma/utils/datasets/common.py,sha256=3AAj7fgWpcqVIqmd7x6boH0trjfkxep41Lxk396Mu1M,718
|
|
27
|
+
spikinggamma/utils/datasets/dvsgesture.py,sha256=EjrBMA6a0eJjo-AFG5HwXQH9Gxs_0CdHm7u69u55sV4,1641
|
|
28
|
+
spikinggamma/utils/datasets/shd.py,sha256=koJpbwUTVCPEHP8Ysfg2clYhQFhyVWIiJXyBMNJ0rSQ,1876
|
|
29
|
+
spikinggamma/utils/datasets/ssc.py,sha256=4r2p86eIBFKNggCukK0pr5y_zKE_wmnN6D3KS-75vBA,1957
|
|
30
|
+
spikinggamma-0.1.0.dist-info/licenses/LICENSE,sha256=N_w_W5zdPurqHRACQv0iaPSQAg_RyluYy43zmq1aLhU,1093
|
|
31
|
+
spikinggamma-0.1.0.dist-info/METADATA,sha256=n28qy6OLGduBd4onsj_CysFIc8VCgIQyF4cuBT_mHJs,2103
|
|
32
|
+
spikinggamma-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
33
|
+
spikinggamma-0.1.0.dist-info/top_level.txt,sha256=C5oBg-2COrh64Nh61zCML3emIHt7bDD9pXDTMQgGMCs,13
|
|
34
|
+
spikinggamma-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 Roel Koopman, Sebastian Otte, Sander Bohte
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spikinggamma
|