-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspike_related.py
More file actions
136 lines (113 loc) · 4.35 KB
/
spike_related.py
File metadata and controls
136 lines (113 loc) · 4.35 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from training_utils import *
import tracemalloc
import gc
class ZIF(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
out = (input > 0).float()
# L = torch.tensor([gamma])
ctx.save_for_backward(input)
return out
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output.clone()
grad_input = grad_input * ((1.0 - torch.abs(input)).clamp(min=0))
# tmp = torch.ones_like(input)
# tmp = torch.where(input.abs() < 0.5, 1., 0.)
# grad_input = grad_input*torch.where(torch.abs(input-th)<1., 1, 0)
# grad_input = grad_input*torch.where(input.abs() < 1., 1., 0.)
return grad_input
class LIFSpike(nn.Module):
def __init__(self, thresh=0.5, leak=0.5, gamma=1.0, soft_reset=True, quant_u=False, num_bits_u=4):
"""
Implementing the LIF neurons.
@param thresh: firing threshold;
@param tau: membrane potential decay factor;
@param gamma: hyper-parameter for controlling the sharpness in surrogate gradient;
@param soft_reset: whether using soft-reset or hard-reset.
"""
super(LIFSpike, self).__init__()
# self.act = ZIF.apply
self.quant_u = quant_u
self.num_bits_u = num_bits_u
self.thresh = thresh
self.leak = leak
self.gamma = gamma
self.soft_reset = soft_reset
self.membrane_potential = 0
# print(self.thresh)
def reset_mem(self):
self.membrane_potential = 0
def forward(self, s, share, beta, bias):
# act = ZIF.apply
# if self.training:
# beta*
# x = gamma_*x + bias
H = s + self.membrane_potential
# s = act(H-self.thresh)
grad = ((1.0 - torch.abs(H-self.thresh)).clamp(min=0))
s = (((H-self.thresh) > 0).float() - H*grad).detach() + H*grad.detach()
# s = (H -(self.thresh/beta)>0).float()
if self.soft_reset:
U = (H - s*self.thresh)*self.leak
else:
U = H*self.leak*(1-s)
if self.quant_u:
if share:
self.membrane_potential = u_q(U,self.num_bits_u,beta)
else:
self.membrane_potential= b_q(U,self.num_bits_u)
else:
self.membrane_potential = U
# else:
# # print(torch.unique(s).shape)
# H = s + self.membrane_potential
# # print(torch.unique(H).shape)
# if share:
# # s = torch.zeros_like(H).cuda()
# # s[H >(self.thresh/beta-bias)] = 1.0
# s = (H -(self.thresh/beta-bias)>0).float()
# else:
# s = ((H-self.thresh) > 0).float()
# # print(torch.unique(H).shape)
# if self.soft_reset:
# U = (H - s*self.thresh)*self.leak
# else:
# U = H*self.leak*(1-s)
# # if self.quant_u:
# # if share:
# # self.membrane_potential = (U).round().clamp(min=2**(-(self.num_bits_u-1)),max=2**(self.num_bits_u-1)-1)
# # # if self.quant_u:
# # # if share:
# # # self.membrane_potential,_ = b_q_inference(U,self.num_bits_u)
# # # else:
# # # self.membrane_potential = b_q_inference(U,self.num_bits_u)
# # else:
# self.membrane_potential = U
return s
def direct_forward(self, s, share, beta):
# act = ZIF.apply
# if self.training:
# beta*
# x = gamma_*x + bias
H = s + self.membrane_potential
# s = act(H-self.thresh)
grad = ((1.0 - torch.abs(H-self.thresh)).clamp(min=0))
s = (((H-self.thresh) > 0).float() - H*grad).detach() + H*grad.detach()
if self.soft_reset:
U = (H - s*self.thresh)*self.leak
else:
U = H*self.leak*(1-s)
if self.quant_u:
if share:
self.membrane_potential = u_q(U,self.num_bits_u,beta)
else:
self.membrane_potential= b_q(U,self.num_bits_u)
else:
self.membrane_potential = U
return s