-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
104 lines (92 loc) · 3.28 KB
/
modules.py
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
from torch import nn
import torch.nn.functional as F
import numpy as np
import torch
from torch.autograd import Function
from spikingjelly.clock_driven import neuron
class StraightThrough(nn.Module):
def __init__(self, channel_num: int = 1):
super().__init__()
def forward(self, input):
return input
class ScaledNeuron(nn.Module):#utils中replace_activation_by_neurond调用,作为SNN的神经元
def __init__(self, scale=1.):
super(ScaledNeuron, self).__init__()
self.scale = scale
self.t = 0 #当前模拟的时间步长t
self.neuron = neuron.IFNode(v_reset=None)#调用的spikingjelly中的IF模型神经元
def forward(self, x):
x = x / self.scale
if self.t == 0:
self.neuron(torch.ones_like(x)*0.5)
x = self.neuron(x)
self.t += 1
return x * self.scale
def reset(self):#重置神经元初始电位
self.t = 0
self.neuron.reset()
class GradFloor(Function):
@staticmethod
def forward(ctx, input):
return input.floor()
@staticmethod
def backward(ctx, grad_output):
return grad_output
myfloor = GradFloor.apply
class ShiftNeuron(nn.Module):
def __init__(self, scale=1., alpha=1/50000):
super().__init__()
self.alpha = alpha
self.vt = 0.
self.scale = scale
self.neuron = neuron.IFNode(v_reset=None)
def forward(self, x):
x = x / self.scale
x = self.neuron(x)
return x * self.scale
def reset(self):#还没看懂
if self.training:
self.vt = self.vt + self.neuron.v.reshape(-1).mean().item()*self.alpha
self.neuron.reset()
if self.training == False:
self.neuron.v = self.vt
class MyFloor(nn.Module):#utils中replace_activation_by_floor调用
def __init__(self, up=8., t=32):#up就是QCFS公式中的阈值\theta,t就是公式中的离散的取值数T
super().__init__()
self.up = nn.Parameter(torch.tensor([up]), requires_grad=True)#第一个参数是初始值
self.t = t
def forward(self, x):
x = x / self.up
x = myfloor(x*self.t+0.5)/self.t
x = torch.clamp(x, 0, 1)
x = x * self.up
return x
class TCL(nn.Module):#TCL方法中增加的一个限制上下界限的函数
def __init__(self):
super().__init__()
self.up = nn.Parameter(torch.Tensor([4.]), requires_grad=True)
def forward(self, x):
x = F.relu(x, inplace='True')
x = self.up - x
x = F.relu(x, inplace='True')
x = self.up - x
return x
class LabelSmoothing(nn.Module):
"""
NLL loss with label smoothing.
"""
def __init__(self, smoothing=0.1):
"""
Constructor for the LabelSmoothing module.
:param smoothing: label smoothing factor
"""
super(LabelSmoothing, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
logprobs = torch.nn.functional.log_softmax(x, dim=-1)
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
nll_loss = nll_loss.squeeze(1)
smooth_loss = -logprobs.mean(dim=-1)
loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()