forked from hujingwen6666/MMGCN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_GCN.py
543 lines (478 loc) · 21.1 KB
/
model_GCN.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.nn.utils.rnn import pad_sequence
from torch_geometric.nn import RGCNConv, GraphConv
from torch.nn.parameter import Parameter
import numpy as np, itertools, random, copy, math
import math
import scipy.sparse as sp
import ipdb
class GCNLayer1(nn.Module):
def __init__(self, in_feats, out_feats, use_topic=False, new_graph=True):
super(GCNLayer1, self).__init__()
self.linear = nn.Linear(in_feats, out_feats)
self.use_topic = use_topic
self.new_graph = new_graph
def forward(self, inputs, dia_len, topicLabel):
if self.new_graph:
pdb.set_trace()
adj = self.message_passing_directed_speaker(inputs,dia_len,topicLabel)
else:
adj = self.message_passing_wo_speaker(inputs,dia_len,topicLabel)
x = torch.matmul(adj, inputs)
x = self.linear(x)
return x
def cossim(self, x, y):
a = torch.matmul(x, y)
b = torch.sqrt(torch.matmul(x, x)) * torch.sqrt(torch.matmul(y, y))
if b == 0:
return 0
else:
return (a / b)
def atom_calculate_edge_weight(self, x, y):
f = self.cossim(x, y)
if f >1 and f <1.05:
f = 1
elif f< -1 and f>-1.05:
f = -1
elif f>=1.05 or f<=-1.05:
print('cos = {}'.format(f))
return f
def message_passing_wo_speaker(self, x,dia_len, topicLabel):
adj = torch.zeros((x.shape[0], x.shape[0]))+torch.eye(x.shape[0])
start = 0
for i in range(len(dia_len)): #
for j in range(dia_len[i]-1):
for pin in range(dia_len[i] - 1-j):
xz=start+j
yz=xz+pin+1
f = self.cossim(x[xz],x[yz])
if f > 1 and f < 1.05:
f = 1
elif f < -1 and f > -1.05:
f = -1
elif f >= 1.05 or f <= -1.05:
print('cos = {}'.format(f))
Aij = 1 - math.acos(f) / math.pi
adj[xz][yz] = Aij
adj[yz][xz] = Aij
start+=dia_len[i]
if self.use_topic:
for (index, topic_l) in enumerate(topicLabel):
xz = index
yz = x.shape[0] + topic_l - 7
f = self.cossim(x[xz],x[yz])
if f > 1 and f < 1.05:
f = 1
elif f < -1 and f > -1.05:
f = -1
elif f >= 1.05 or f <= -1.05:
print('cos = {}'.format(f))
Aij = 1 - math.acos(f) / math.pi
adj[xz][yz] = Aij
adj[yz][xz] = Aij
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj
def message_passing_directed_speaker(self, x, dia_len, qmask):
total_len = sum(dia_len)
adj = torch.zeros((total_len, total_len))+torch.eye(total_len)
start = 0
use_utterance_edge=False
for (i, len_) in enumerate(dia_len):
speaker0 = []
speaker1 = []
for (j, speaker) in enumerate(qmask[start:start+len_]):
if speaker[0] == 1:
speaker0.append(j)
else:
speaker1.append(j)
if use_utterance_edge:
for j in range(len_-1):
f = self.atom_calculate_edge_weight(x[start+j], x[start+j+1])
Aij = 1-math.acos(f) / math.pi
adj[start+j][start+j+1] = Aij
adj[start+j+1][start+j] = Aij
for k in range(len(speaker0)-1):
f = self.atom_calculate_edge_weight(x[start+speaker0[k]], x[start+speaker0[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker0[k]][start+speaker0[k+1]] = Aij
adj[start+speaker0[k+1]][start+speaker0[k]] = Aij
for k in range(len(speaker1)-1):
f = self.atom_calculate_edge_weight(x[start+speaker1[k]], x[start+speaker1[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker1[k]][start+speaker1[k+1]] = Aij
adj[start+speaker1[k+1]][start+speaker1[k]] = Aij
start+=dia_len[i]
return adj
class GCN_2Layers(nn.Module):
def __init__(self, lstm_hid_size, gcn_hid_dim, num_class, dropout, use_topic=False, use_residue=True, return_feature=False):
super(GCN_2Layers, self).__init__()
self.lstm_hid_size = lstm_hid_size
self.gcn_hid_dim = gcn_hid_dim
self.num_class = num_class
self.dropout = dropout
self.use_topic = use_topic
self.return_feature = return_feature
self.gcn1 = GCNLayer1(self.lstm_hid_size, self.gcn_hid_dim, self.use_topic)
self.use_residue = use_residue
if self.use_residue:
self.gcn2 = GCNLayer1(self.gcn_hid_dim, self.gcn_hid_dim, self.use_topic)
self.linear = nn.Linear(self.lstm_hid_size+self.gcn_hid_dim,self.num_class)
else:
self.gcn2 = GCNLayer1(self.gcn_hid_dim, self.num_class, self.use_topic)
def forward(self, x,dia_len,topicLabel):
x_graph = self.gcn1(x,dia_len,topicLabel)
if not self.use_residue:
x = self.gcn2(x_graph,dia_len,topicLabel)
if self.return_feature:
print("Error, you should change the state of use_residue")
else:
x_graph = self.gcn2(x_graph,dia_len,topicLabel)
x = torch.cat([x,x_graph],dim=-1)
if self.return_feature:
return x
x = self.linear(x)
log_prob = F.log_softmax(x, 1)
return log_prob
class GraphConvolution(nn.Module):
def __init__(self, in_features, out_features, residual=False, variant=False):
super(GraphConvolution, self).__init__()
self.variant = variant
if self.variant:
self.in_features = 2*in_features
else:
self.in_features = in_features
self.out_features = out_features
self.residual = residual
self.weight = Parameter(torch.FloatTensor(self.in_features,self.out_features))
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.out_features)
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input, adj , h0 , lamda, alpha, l):
theta = math.log(lamda/l+1)
hi = torch.spmm(adj, input)
if self.variant:
support = torch.cat([hi,h0],1)
r = (1-alpha)*hi+alpha*h0
else:
support = (1-alpha)*hi+alpha*h0
r = support
output = theta*torch.mm(support, self.weight)+(1-theta)*r
if self.residual:
output = output+input
return output
class TextCNN(nn.Module):
def __init__(self, input_dim, emb_size=128, in_channels=1, out_channels=128, kernel_heights=[3,4,5], dropout=0.5):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, (kernel_heights[0], input_dim), stride=1, padding=0)
self.conv2 = nn.Conv2d(in_channels, out_channels, (kernel_heights[1], input_dim), stride=1, padding=0)
self.conv3 = nn.Conv2d(in_channels, out_channels, (kernel_heights[2], input_dim), stride=1, padding=0)
self.dropout = nn.Dropout(dropout)
self.embd = nn.Sequential(
nn.Linear(len(kernel_heights)*out_channels, emb_size),
nn.ReLU(inplace=True),
)
def conv_block(self, input, conv_layer):
conv_out = conv_layer(input)# conv_out.size() = (batch_size, out_channels, dim, 1)
activation = F.relu(conv_out.squeeze(3))# activation.size() = (batch_size, out_channels, dim1)
max_out = F.max_pool1d(activation, activation.size()[2]).squeeze(2) # maxpool_out.size() = (batch_size, out_channels)
return max_out
def forward(self, frame_x):
batch_size, seq_len, feat_dim = frame_x.size() #dia_len, utt_len, batch_size, feat_dim
frame_x = frame_x.view(batch_size, 1, seq_len, feat_dim)
max_out1 = self.conv_block(frame_x, self.conv1)
max_out2 = self.conv_block(frame_x, self.conv2)
max_out3 = self.conv_block(frame_x, self.conv3)
all_out = torch.cat((max_out1, max_out2, max_out3), 1)
fc_in = self.dropout(all_out)
embd = self.embd(fc_in)
return embd
class GCNII(nn.Module):
def __init__(self, nfeat, nlayers,nhidden, nclass, dropout, lamda, alpha, variant, return_feature, use_residue, new_graph=False):
super(GCNII, self).__init__()
self.return_feature = return_feature
self.use_residue = use_residue
self.new_graph = new_graph
self.convs = nn.ModuleList()
for _ in range(nlayers):
self.convs.append(GraphConvolution(nhidden, nhidden,variant=variant))
self.fcs = nn.ModuleList()
self.fcs.append(nn.Linear(nfeat, nhidden))
if not return_feature:
self.fcs.append(nn.Linear(nfeat+nhidden, nclass))
self.params1 = list(self.convs.parameters())
self.params2 = list(self.fcs.parameters())
self.act_fn = nn.ReLU()
self.dropout = dropout
self.alpha = alpha
self.lamda = lamda
def cossim(self, x, y):
a = torch.matmul(x, y)
b = torch.sqrt(torch.matmul(x, x)) * torch.sqrt(torch.matmul(y, y))
if b == 0:
return 0
else:
return (a / b)
def forward(self, x, dia_len, topicLabel):
if self.new_graph:
adj = self.message_passing_directed_speaker(x, dia_len, topicLabel)
else:
adj = self.create_big_adj(x, dia_len)
_layers = []
x = F.dropout(x, self.dropout, training=self.training)
layer_inner = self.act_fn(self.fcs[0](x))
_layers.append(layer_inner)
for i,con in enumerate(self.convs):
layer_inner = F.dropout(layer_inner, self.dropout, training=self.training)
layer_inner = self.act_fn(con(layer_inner,adj,_layers[0],self.lamda,self.alpha,i+1))
layer_inner = F.dropout(layer_inner, self.dropout, training=self.training)
if self.use_residue:
layer_inner = torch.cat([x, layer_inner], dim=-1)
if not self.return_feature:
layer_inner = self.fcs[-1](layer_inner)
layer_inner = F.log_softmax(layer_inner, dim=1)
return layer_inner
def create_big_adj(self, x, dia_len):
adj = torch.zeros((x.shape[0], x.shape[0]))
start = 0
for i in range(len(dia_len)):
sub_adj = torch.zeros((dia_len[i], dia_len[i]))
temp = x[start:start + dia_len[i]]
temp_len = torch.sqrt(torch.bmm(temp.unsqueeze(1),temp.unsqueeze(2)).squeeze(-1).squeeze(-1))
temp_len_matrix = temp_len.unsqueeze(1)*temp_len.unsqueeze(0)
cos_sim_matrix = torch.matmul(temp,temp.permute(1,0))/temp_len_matrix
sim_matrix = torch.acos(cos_sim_matrix*0.99999)
sim_matrix = 1 - sim_matrix/math.pi
sub_adj[:dia_len[i], :dia_len[i]] = sim_matrix
m_start = start
n_start = start
adj[m_start:m_start+dia_len[i], n_start:n_start+dia_len[i]] = sub_adj
start += dia_len[i]
d = adj.sum(1)
D = torch.diag(torch.pow(d, -0.5))
adj = D.mm(adj).mm(D).cuda()
return adj
def message_passing_wo_speaker(self, x,dia_len, topicLabel):
adj = torch.zeros((x.shape[0], x.shape[0]))+torch.eye(x.shape[0])
start = 0
for i in range(len(dia_len)): #
for j in range(dia_len[i]-1):
for pin in range(dia_len[i] - 1-j):
xz=start+j
yz=xz+pin+1
f = self.cossim(x[xz],x[yz])
if f > 1 and f < 1.05:
f = 1
elif f < -1 and f > -1.05:
f = -1
elif f >= 1.05 or f <= -1.05:
print('cos = {}'.format(f))
Aij = 1 - math.acos(f) / math.pi
adj[xz][yz] = Aij
adj[yz][xz] = Aij
start+=dia_len[i]
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj
def atom_calculate_edge_weight(self, x, y):
f = self.cossim(x, y)
if f >1 and f <1.05:
f = 1
elif f< -1 and f>-1.05:
f = -1
elif f>=1.05 or f<=-1.05:
print('cos = {}'.format(f))
return f
def message_passing_directed_speaker(self, x, dia_len, qmask):
total_len = sum(dia_len)
adj = torch.zeros((total_len, total_len))+torch.eye(total_len)
start = 0
use_utterance_edge=False
for (i, len_) in enumerate(dia_len):
speaker0 = []
speaker1 = []
for (j, speaker) in enumerate(qmask[i][0:len_]):
if speaker[0] == 1:
speaker0.append(j)
else:
speaker1.append(j)
if use_utterance_edge:
for j in range(len_-1):
f = self.atom_calculate_edge_weight(x[start+j], x[start+j+1])
Aij = 1-math.acos(f) / math.pi
adj[start+j][start+j+1] = Aij
adj[start+j+1][start+j] = Aij
for k in range(len(speaker0)-1):
f = self.atom_calculate_edge_weight(x[start+speaker0[k]], x[start+speaker0[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker0[k]][start+speaker0[k+1]] = Aij
adj[start+speaker0[k+1]][start+speaker0[k]] = Aij
for k in range(len(speaker1)-1):
f = self.atom_calculate_edge_weight(x[start+speaker1[k]], x[start+speaker1[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker1[k]][start+speaker1[k+1]] = Aij
adj[start+speaker1[k+1]][start+speaker1[k]] = Aij
start+=dia_len[i]
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj.cuda()
def message_passing_relation_graph(self, x, dia_len):
total_len = sum(dia_len)
adj = torch.zeros((total_len, total_len))+torch.eye(total_len)
window_size = 10
start = 0
for (i, len_) in enumerate(dia_len):
edge_set = []
for k in range(len_):
left = max(0, k-window_size)
right = min(len_-1, k+window_size)
edge_set = edge_set + [str(i)+'_'+str(j) for i in range(left, right) for j in range(i+1, right+1)]
edge_set = [[start+int(str_.split('_')[0]),start+int(str_.split('_')[1])] for str_ in list(set(edge_set))]
for left, right in edge_set:
f = self.atom_calculate_edge_weight(x[left], x[right])
Aij = 1-math.acos(f) / math.pi
adj[left][right] = Aij
adj[right][left] = Aij
start+=dia_len[i]
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj.cuda()
class GCNII_lyc(nn.Module):
def __init__(self, nfeat, nlayers, nhidden, nclass, dropout, lamda, alpha, variant, return_feature, use_residue, new_graph=False):
super(GCNII_lyc, self).__init__()
self.return_feature = return_feature
self.use_residue = use_residue
self.new_graph = new_graph
self.convs = nn.ModuleList()
for _ in range(nlayers):
self.convs.append(GraphConvolution(nhidden, nhidden,variant=variant))
self.fcs = nn.ModuleList()
self.fcs.append(nn.Linear(nfeat, nhidden))
if not return_feature:
self.fcs.append(nn.Linear(nfeat+nhidden, nclass))
self.params1 = list(self.convs.parameters())
self.params2 = list(self.fcs.parameters())
self.act_fn = nn.ReLU()
self.dropout = dropout
self.alpha = alpha
self.lamda = lamda
def cossim(self, x, y):
a = torch.matmul(x, y)
b = torch.sqrt(torch.matmul(x, x)) * torch.sqrt(torch.matmul(y, y))
if b == 0:
return 0
else:
return (a / b)
def forward(self, x, dia_len, topicLabel, adj=None):
if adj is None:
if self.new_graph:
adj = self.message_passing_relation_graph(x, dia_len)
else:
adj = self.message_passing_wo_speaker(x, dia_len, topicLabel)
else:
adj = adj
_layers = []
x = F.dropout(x, self.dropout, training=self.training)
layer_inner = self.act_fn(self.fcs[0](x))
_layers.append(layer_inner)
for i,con in enumerate(self.convs):
layer_inner = F.dropout(layer_inner, self.dropout, training=self.training)
layer_inner = self.act_fn(con(layer_inner,adj,_layers[0],self.lamda,self.alpha,i+1))
layer_inner = F.dropout(layer_inner, self.dropout, training=self.training)
if self.use_residue:
layer_inner = torch.cat([x, layer_inner], dim=-1)
if not self.return_feature:
layer_inner = self.fcs[-1](layer_inner)
layer_inner = F.log_softmax(layer_inner, dim=1)
return layer_inner
def message_passing_wo_speaker(self, x,dia_len, topicLabel):
adj = torch.zeros((x.shape[0], x.shape[0]))
start = 0
for i in range(len(dia_len)):
sub_adj = torch.zeros((dia_len[i], dia_len[i]))
temp = x[start:start+dia_len[i]]
vec_length = torch.sqrt(torch.sum(temp.mul(temp), dim=1))
norm_temp = (temp.permute(1, 0) / vec_length)
cos_sim_matrix = torch.sum(torch.matmul(norm_temp.unsqueeze(2), norm_temp.unsqueeze(1)), dim=0)
cos_sim_matrix = cos_sim_matrix * 0.99999
sim_matrix = torch.acos(cos_sim_matrix)
d = sim_matrix.sum(1)
D = torch.diag(torch.pow(d, -0.5))
sub_adj[:dia_len[i], :dia_len[i]] = D.mm(sim_matrix).mm(D)
adj[start:start+dia_len[i], start:start+dia_len[i]] = sub_adj
start+=dia_len[i]
adj = adj.cuda()
return adj
def atom_calculate_edge_weight(self, x, y):
f = self.cossim(x, y)
if f >1 and f <1.05:
f = 1
elif f< -1 and f>-1.05:
f = -1
elif f>=1.05 or f<=-1.05:
print('cos = {}'.format(f))
return f
def message_passing_directed_speaker(self, x, dia_len, qmask):
total_len = sum(dia_len)
adj = torch.zeros((total_len, total_len))+torch.eye(total_len)
start = 0
use_utterance_edge=False
for (i, len_) in enumerate(dia_len):
speaker0 = []
speaker1 = []
for (j, speaker) in enumerate(qmask[i][0:len_]):
if speaker[0] == 1:
speaker0.append(j)
else:
speaker1.append(j)
if use_utterance_edge:
for j in range(len_-1):
f = self.atom_calculate_edge_weight(x[start+j], x[start+j+1])
Aij = 1-math.acos(f) / math.pi
adj[start+j][start+j+1] = Aij
adj[start+j+1][start+j] = Aij
for k in range(len(speaker0)-1):
f = self.atom_calculate_edge_weight(x[start+speaker0[k]], x[start+speaker0[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker0[k]][start+speaker0[k+1]] = Aij
adj[start+speaker0[k+1]][start+speaker0[k]] = Aij
for k in range(len(speaker1)-1):
f = self.atom_calculate_edge_weight(x[start+speaker1[k]], x[start+speaker1[k+1]])
Aij = 1-math.acos(f) / math.pi
adj[start+speaker1[k]][start+speaker1[k+1]] = Aij
adj[start+speaker1[k+1]][start+speaker1[k]] = Aij
start+=dia_len[i]
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj.cuda()
def message_passing_relation_graph(self, x, dia_len):
total_len = sum(dia_len)
adj = torch.zeros((total_len, total_len))+torch.eye(total_len)
window_size = 10
start = 0
for (i, len_) in enumerate(dia_len):
edge_set = []
for k in range(len_):
left = max(0, k-window_size)
right = min(len_-1, k+window_size)
edge_set = edge_set + [str(i)+'_'+str(j) for i in range(left, right) for j in range(i+1, right+1)]
edge_set = [[start+int(str_.split('_')[0]),start+int(str_.split('_')[1])] for str_ in list(set(edge_set))]
for left, right in edge_set:
f = self.atom_calculate_edge_weight(x[left], x[right])
Aij = 1-math.acos(f) / math.pi
adj[left][right] = Aij
adj[right][left] = Aij
start+=dia_len[i]
d = adj.sum(1)
D=torch.diag(torch.pow(d,-0.5))
adj = D.mm(adj).mm(D).cuda()
return adj.cuda()