-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuron.py
More file actions
70 lines (45 loc) · 2.08 KB
/
Copy pathNeuron.py
File metadata and controls
70 lines (45 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
import torch
import torch.nn as nn
from spikingjelly.activation_based import base, neuron
from abc import abstractmethod
class ActiveIFNode(neuron.IFNode):
def __init__(self, sgn_dims, n_dendrites):
super().__init__()
self.sgn_dims = sgn_dims
self.active_dendrites = nn.Parameter(torch.randn(sgn_dims, n_dendrites))
def forward(self, x, context):
dendritic_activations = torch.matmul(context, self.active_dendrites)
maximal_dendritic_activation = torch.max(dendritic_activations, dim = -1).values
x_shape = x.shape
new_shape = x_shape[:1] + (1,) * (len(x_shape) - 1)
maximal_dendritic_activation = maximal_dendritic_activation.view(new_shape)
return super().forward(x * nn.functional.sigmoid(maximal_dendritic_activation))
class ActiveLIFNode(neuron.LIFNode):
def __init__(self, sgn_dims, n_dendrites):
super().__init__()
self.sgn_dims = sgn_dims
self.active_dendrites = nn.Parameter(torch.randn(sgn_dims, n_dendrites))
def forward(self, x, context):
dendritic_activations = torch.matmul(context, self.active_dendrites)
maximal_dendritic_activation = torch.max(dendritic_activations, dim = -1).values
x_shape = x.shape
new_shape = x_shape[:1] + (1,) * (len(x_shape) - 1)
maximal_dendritic_activation = maximal_dendritic_activation.view(new_shape)
return super().forward(x) * nn.functional.sigmoid(maximal_dendritic_activation)
class NonSpikingBaseNode(nn.Module, base.MultiStepModule):
def __init__(self):
super().__init__()
@abstractmethod
def neuronal_charge(self, x: torch.Tensor):
raise NotImplementedError
def forward(self, x_seq: torch.Tensor):
self.v = torch.full_like(x_seq.data, fill_value=0.0)
v_seq = []
self.neuronal_charge(x_seq)
v_seq.append(self.v)
return v_seq[-1]
class NonSpikingIFNode(NonSpikingBaseNode):
def __init__(self):
super().__init__()
def neuronal_charge(self, x: torch.Tensor):
self.v = self.v + x