-
Notifications
You must be signed in to change notification settings - Fork 13
/
functions.py
153 lines (117 loc) · 3.68 KB
/
functions.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
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
"""
Activation Functions
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.autograd import Function
class BinarizeSigF(Function):
@staticmethod
def forward(cxt, input):
output = input.new(input.size())
output[input >= 0.5] = 1
output[input < 0.5] = 0
return output
@staticmethod
def backward(cxt, grad_output):
grad_input = grad_output.clone()
return grad_input
class BinarizeTanhF(Function):
@staticmethod
def forward(cxt, input):
output = input.new(input.size())
output[input >= 0] = 1
output[input < 0] = -1
return output
@staticmethod
def backward(cxt, grad_output):
grad_input = grad_output.clone()
return grad_input
class TernarizeTanhF(Function):
@staticmethod
def forward(cxt, input):
output = input.new(input.size())
output.data = input.data
output.round_()
return output
@staticmethod
def backward(cxt, grad_output):
grad_input = grad_output.clone()
return grad_input
class BernolliSampleBinarizeF(Function):
@staticmethod
def forward(cxt, input):
output = input.new(input.size())
output = torch.bernoulli(output)
return output
@staticmethod
def backward(cxt, grad_output):
grad_input = grad_output.clone()
return grad_input
class BinarySigmoid(nn.Module):
def __init__(self):
super(BinarySigmoid, self).__init__()
self.hardsigmoid = nn.Sigmoid()
def forward(self, input, stochastic=False):
output = self.hardsigmoid(input)
if not stochastic:
output = binarizeSig(output)
else:
output = bernolliSample(output)
return output
class BinaryTanh(nn.Module):
"""
reference: https://github.com/DingKe/pytorch_workplace/blob/master/binary/modules.py#L10
"""
def __init__(self):
super(BinaryTanh, self).__init__()
self.hardtanh = nn.Tanh()
def forward(self, input):
output = self.hardtanh(input)
output = binarizeTanh(output)
return output
class TernaryTanh(nn.Module):
"""
reference: https://r2rt.com/beyond-binary-ternary-and-one-hot-neurons.html
"""
def __init__(self):
super(TernaryTanh, self).__init__()
def forward(self, input):
output = 1.5 * F.tanh(input) + 0.5 * F.tanh(-3 * input)
output = ternarizeTanh(output)
return output
def _sample_gumbel(input):
noise = torch.rand(input.size())
eps = 1e-20
noise.add_(eps).log_().neg_()
noise.add_(eps).log_().neg_()
return Variable(noise)
def gumbel_softmax_sample(input, hard=False):
temperature = 0.8
noise = _sample_gumbel(input)
x = (input + noise) / temperature
x = F.softmax(x, dim=1)
if hard:
max_val, _ = torch.max(x, x.dim() - 1)
x = x == max_val.expand_as(x)
return x.view_as(input)
class DynamicGNoise(nn.Module):
def __init__(self, shape, mean=0, std=0.05, cuda=False):
super(DynamicGNoise).__init__()
self.noise = torch.zeros(shape, shape)
self.noise = self.noise.cuda() if cuda else self.noise
self.noise = Variable(self.noise)
self.std = std
self.mean = mean
def forward(self, x):
if self.training:
self.noise.data.normal_(mean=self.mean, std=self.std)
print(x.size(), self.noise.size())
x += self.noise.expand()
return x
# aliases
binarizeSig = BinarizeSigF.apply
binarizeTanh = BinarizeTanhF.apply
ternarizeTanh = TernarizeTanhF.apply
bernolliSample = BernolliSampleBinarizeF.apply