-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules_mine.py
More file actions
316 lines (239 loc) · 10 KB
/
modules_mine.py
File metadata and controls
316 lines (239 loc) · 10 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import torch
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.nn import functional as F
import math
def init_weights(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
nn.init.xavier_normal_(m.weight)
nn.init.zeros_(m.bias)
elif classname.find('BatchNorm') != -1:
# nn.init.normal_(m.weight, 1.0, 0.02)
nn.init.normal_(m.weight, 0.0, 0.02)
# nn.init.normal_(m.weight, 1.5, 0.5)
nn.init.zeros_(m.bias)
class MLP(nn.Module):
def __init__(self, cfg, dropout=0.):
"""cfg: [(input_size, output_size, act), ...]"""
super(MLP, self).__init__()
density_blocks = []
# todo: add batchnorm, dropout
for layer_cfg in cfg:
density_blocks.append(nn.Linear(in_features=layer_cfg[0], out_features=layer_cfg[1]))
density_blocks.append(nn.Dropout(dropout))
if layer_cfg[2] == "relu":
density_blocks.append(nn.ReLU(inplace=True))
elif layer_cfg[2] == "leakyrelu":
density_blocks.append(nn.LeakyReLU(0.2, inplace=True))
elif layer_cfg[2] == "sigmoid":
density_blocks.append(nn.Sigmoid())
elif layer_cfg[2] == "softmax":
density_blocks.append(nn.Softmax(dim=1))
else:
# todo:add more activate function
pass
self.mlp = nn.Sequential(*density_blocks)
self.mlp.apply(init_weights)
def forward(self, x):
out = self.mlp(x)
return out
class Sparse_ps_nn(nn.Module):
"""https://debuggercafe.com/sparse-autoencoders-using-kl-divergence-with-pytorch/"""
def __init__(self, encoder_model, decoder_model, beta=0.01, type="cla", rho=0.05):
"""
:param encoder_cfg: param_config of encoder
:param decoder_cfg: param_config of decoder
:param z_dim: hidden_size of z
:param beta: weight of kl_loss
"""
super(Sparse_ps_nn, self).__init__()
self.beta = beta
self.type = type
self.rho = rho
self.encoder = encoder_model
self.decoder = decoder_model
self.model_children = list(encoder_model.children())[0]
def forward(self, x, g=None):
z = self.encoder(x)
if self.type == "cla":
y_pred = self.decoder(z)
else:
assert g is not None
y_pred = self.decoder(g=g, x=z)
return y_pred
def kl_divergence(self, rho, rho_hat):
rho_hat = torch.mean(rho_hat, 1) # sigmoid because we need the probability distributions
rho = torch.tensor([rho] * len(rho_hat)).cuda()
return torch.sum(rho * torch.log(rho / rho_hat) + (1 - rho) * torch.log((1 - rho) / (1 - rho_hat)))
# define the sparse loss function
def sparse_loss(self, x):
values = x
loss = 0
for i in range(len(self.model_children) // 3):
values = self.model_children[i*3+0](values)
values = self.model_children[i*3+1](values)
values = self.model_children[i*3+2](values)
loss += self.kl_divergence(self.rho, values)
return loss
def loss_function(self, y, y_pred, x):
if self.type == "cla":
y = y.reshape(-1, 1)
pred_loss = F.binary_cross_entropy(y_pred, y)
# pred_loss = F.cross_entropy(y_pred, y, reduction="mean")
elif self.type == "reg":
pred_loss = - torch.log(y_pred + 1e-5).mean()
# sparsity
kl = self.sparse_loss(x)
loss = pred_loss + self.beta * kl
return loss, pred_loss, self.beta * kl
class DeepVIB(nn.Module):
"""https://github.com/udeepam/vib/blob/master/vib.ipynb"""
def __init__(self, encoder_model, decoder_model, z_dim, beta=0.01, type="cla"):
"""
:param encoder_cfg: param_config of encoder
:param decoder_cfg: param_config of decoder
:param z_dim: hidden_size of z
:param beta: weight of kl_loss
"""
super(DeepVIB, self).__init__()
self.z_dim = z_dim
self.beta = beta
self.type = type
self.encoder = encoder_model
self.decoder = decoder_model
# self.bn = nn.BatchNorm1d(25)
# self.bce_loss = nn.BCELoss()
def reparameter_trick(self, mu, std):
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x, g=None):
z_param = self.encoder(x)
mu = z_param[:, :self.z_dim]
std = z_param[:, self.z_dim:].exp()
# std = F.softplus(z_param[:, self.z_dim:] - 5, beta=1)
z = self.reparameter_trick(mu, std)
if self.type == "cla":
# z = self.bn(z)
y_pred = self.decoder(z)
else:
assert g is not None
y_pred = self.decoder(g=g, x=z)
return y_pred, mu, std
def loss_function(self, y, y_pred, mu, std):
if self.type == "cla":
y = y.reshape(-1, 1)
pred_loss = F.binary_cross_entropy(y_pred, y)
# pred_loss = F.cross_entropy(y_pred, y, reduction="mean")
elif self.type == "reg":
pred_loss = - torch.log(y_pred + 1e-5).mean()
# warn: std为0取log导致RuntimeError: CUDA error: device-side assert triggered
std = std + 1e-5
kl = (0.5 * torch.sum(mu.pow(2) + std.pow(2) - 2*std.log() - 1)) / y.shape[0]
loss = pred_loss + self.beta * kl
return loss, pred_loss, self.beta * kl
class GraphConvolution(nn.Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, bias=True):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
self.register_parameter("weight", self.weight)
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
self.register_parameter('bias', self.bias)
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
#
# self.weight.data.normal_(0, 0.02)
# if self.bias is not None:
# self.bias.data.normal_(0, 0.02)
def forward(self, input, adj):
support = torch.mm(input, self.weight)
output = torch.spmm(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class GCN_Mine(nn.Module):
def __init__(self, nfeat, nclass, dropout):
super(GCN_Mine, self).__init__()
self.gc1 = GraphConvolution(nfeat, nclass)
self.dropout = dropout
def forward(self, x, adj):
num = adj.shape[0]
# diag = torch.diag(torch.cuda.FloatTensor([1 for _ in range(num)]))
# x = F.relu(self.gc1(x, adj+diag))
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout)
return x
def comp_grid(y, num_grid):
# L gives the lower index
# U gives the upper index
# inter gives the distance to the lower int
U = torch.ceil(y * num_grid)
inter = 1 - (U - y * num_grid) # 与L的距离
L = U - 1
L += (L < 0).int()
return L.int().tolist(), U.int().tolist(), inter
class Density_Block(nn.Module):
def __init__(self, num_grid, ind, isbias=1):
super(Density_Block, self).__init__()
"""
Assume the variable is bounded by [0,1]
the output grid: 0, 1/B, 2/B, ..., B/B; output dim = B + 1; num_grid = B
"""
self.ind = ind
self.num_grid = num_grid
self.outd = num_grid + 1
self.isbias = isbias
self.weight = nn.Parameter(torch.rand(self.ind, self.outd), requires_grad=True) # 拼接t
if self.isbias:
self.bias = nn.Parameter(torch.rand(self.outd), requires_grad=True)
else:
self.bias = None
self.softmax = nn.Softmax(dim=1)
def forward(self, g, x):
out = torch.matmul(x, self.weight)
if self.isbias:
out += self.bias
out = self.softmax(out)
L, U, inter = comp_grid(g, self.num_grid)
# L_out = out[x1, L]
# U_out = out[x1, U]
L_out = out.gather(1, torch.cuda.LongTensor(L))
U_out = out.gather(1, torch.cuda.LongTensor(U))
out = L_out + (U_out - L_out) * inter
return out
if __name__ == "__main__":
# ====================== deep vib ===========================
x = torch.randn([256, 64])
z_dim = 16
# regression test
reg_y = torch.randn([256, 1])
encoder_cfg = [(64, 32, "relu"), (32, 32, "relu"), (32, z_dim * 2, None)]
reg_decoder_cfg = [(z_dim, 32, "relu"), (32, 32, "relu"), (32, 1, None)]
dvib_reg = DeepVIB(encoder_cfg, reg_decoder_cfg, z_dim)
y_pred, mu, std = dvib_reg(x)
toal_loss, pred_loss, kl_loss = dvib_reg.loss_function(reg_y, y_pred, mu, std, type="reg")
print(toal_loss.item(), pred_loss.item(), kl_loss.item())
# classification test
cla_y = torch.randint(0, 2, [256,]).long()
cla_decoder_cfg = [(z_dim, 32, "relu"), (32, 32, "relu"), (32, 2, None)]
dvib_cla = DeepVIB(encoder_cfg, cla_decoder_cfg, z_dim)
y_pred, mu, std = dvib_cla(x)
toal_loss, pred_loss, kl_loss = dvib_cla.loss_function(cla_y, y_pred, mu, std, type="cla")
print(toal_loss.item(), pred_loss.item(), kl_loss.item())