-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_hyper.py
More file actions
357 lines (329 loc) · 14.3 KB
/
model_hyper.py
File metadata and controls
357 lines (329 loc) · 14.3 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
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
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, FAConv
from torch.nn import Parameter
import numpy as np, itertools, random, copy, math
import math
import scipy.sparse as sp
from model_GCN import GCNII_lyc
import ipdb
from HypergraphConv import HypergraphConv
from torch_geometric.nn import GCNConv
from itertools import permutations
from torch_geometric.nn.pool.topk_pool import topk
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp, global_add_pool as gsp
from torch_geometric.nn.inits import glorot
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from torch_geometric.utils import add_self_loops
from high_fre_conv import highConv
from torch_geometric.nn import TransformerConv
class STEFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
return (input > 0).float()
@staticmethod
def backward(ctx, grad_output):
return F.hardtanh(grad_output)
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 SGCN(nn.Module):
def __init__(self, g_dim, h2_dim):
super(SGCN, self).__init__()
self.conv1 = TransformerConv(g_dim, h2_dim)
def forward(self, node_features, edge_index):
x = self.conv1(node_features, edge_index)
return x
class emoAgent(nn.Module):
def __init__(self, a_dim, v_dim, l_dim, n_dim, nlayers, nhidden, nclass, dropout, lamda, alpha, variant, return_feature, use_residue,
new_graph='full',n_speakers=2, modals=['a','v','l'], use_speaker=True, use_modal=False, num_L=3, num_K=4):
super(emoAgent, self).__init__()
self.return_feature = return_feature #True
self.use_residue = use_residue
self.new_graph = new_graph
self.nhidden = nhidden
self.act_fn = nn.ReLU()
self.dropout = dropout
self.alpha = alpha
self.lamda = lamda
self.modals = modals
self.modal_embeddings = nn.Embedding(3, n_dim)
self.speaker_embeddings = nn.Embedding(n_speakers, n_dim)
self.use_speaker = use_speaker
self.use_modal = use_modal
self.use_position = False
#------------------------------------
self.fc1 = nn.Linear(n_dim, nhidden)
self.fc2 = nn.Linear(nhidden * 3, nhidden)
self.num_L = num_L
self.num_K = num_K
for ll in range(num_L):
setattr(self,'hyperconv%d' %(ll+1), HypergraphConv(nhidden, nhidden))
self.act_fn = nn.ReLU()
self.hyperedge_weight = nn.Parameter(torch.ones(10000))
self.EW_weight = nn.Parameter(torch.ones(20000))
self.hyperedge_attr1 = nn.Parameter(torch.rand(nhidden))
self.hyperedge_attr2 = nn.Parameter(torch.rand(nhidden))
self.hyperedge_attr3 = nn.Parameter(torch.rand(nhidden))
def forward(self, a, v, l, agent_gnn, dia_len, qmask, epoch):
hyperedge_index, edge_index, features, batch, hyperedge_type1 = self.create_hyper_index(a, v, l, agent_gnn, dia_len, self.modals)
x1 = self.fc1(features)
weight = self.hyperedge_weight[0:hyperedge_index[1].max().item()+1]
EW_weight = self.EW_weight[0:hyperedge_index.size(1)]
edge_attr = self.hyperedge_attr1*hyperedge_type1 + self.hyperedge_attr2*((1+hyperedge_type1)//3) + self.hyperedge_attr3*(2-hyperedge_type1)
out = x1
for ll in range(self.num_L): # num_L
out = getattr(self,'hyperconv%d' %(ll+1))(out, hyperedge_index, weight, edge_attr, EW_weight, dia_len)
out = out + x1
if self.use_residue:
out = torch.cat([features, out], dim=-1)
out = self.reverse_features(dia_len, out)
return out
def create_hyper_index(self, a, v, l, agent, dia_len, modals):
self_loop = False
num_modality = len(modals) + 1
node_count = 0
edge_count = 0
batch_count = 0
index1 =[]
index2 =[]
tmp = []
batch = []
hyperedge_type1 = []
for i in dia_len:
nodes = list(range(i*num_modality))
nodes = [j + node_count for j in nodes]
nodes_l = nodes[0:i*num_modality//4]
nodes_a = nodes[i*num_modality//4:i*num_modality*2//4]
nodes_v = nodes[i*num_modality*2//4:i*num_modality*3//4]
nodes_agent = nodes[i*num_modality*3//4:]
index1 = index1 + nodes_l + nodes_a + nodes_v + nodes_agent
for _ in range(i):
index1 = index1 + [nodes_l[_]] + [nodes_a[_]] + [nodes_agent[_]]
index1 = index1 + [nodes_l[_]] + [nodes_v[_]] + [nodes_agent[_]]
index1 = index1 + [nodes_a[_]] + [nodes_v[_]] + [nodes_agent[_]]
index1 = index1 + [nodes_a[_]] + [nodes_agent[_]]
index1 = index1 + [nodes_v[_]] + [nodes_agent[_]]
index1 = index1 + [nodes_l[_]] + [nodes_agent[_]]
for _ in range(i+4):
if _ < 4:
index2 = index2 + [edge_count]*i
else:
index2 = index2 + [edge_count]*3
edge_count = edge_count + 1
index2 = index2 + [edge_count]*3
edge_count = edge_count + 1
index2 = index2 + [edge_count]*3
edge_count = edge_count + 1
index2 = index2 + [edge_count]*2
edge_count = edge_count + 1
index2 = index2 + [edge_count]*2
edge_count = edge_count + 1
index2 = index2 + [edge_count]*2
edge_count = edge_count + 1
if node_count == 0:
ll = l[0:0+i]
aa = a[0:0+i]
vv = v[0:0+i]
gg = agent[0:0+i]
features = torch.cat([ll,aa,vv,gg],dim=0)
temp = 0+i
else:
ll = l[temp:temp+i]
aa = a[temp:temp+i]
vv = v[temp:temp+i]
gg = agent[temp:temp+i]
features_temp = torch.cat([ll,aa,vv,gg],dim=0)
features = torch.cat([features,features_temp],dim=0)
temp = temp + i
Gnodes=[]
Gnodes.append(nodes_l)
Gnodes.append(nodes_a)
Gnodes.append(nodes_v)
Gnodes.append(nodes_agent)
for _ in range(i):
Gnodes.append([nodes_l[_]] + [nodes_agent[_]] )
for _ in range(i):
Gnodes.append([nodes_a[_]] + [nodes_agent[_]] )
for _ in range(i):
Gnodes.append([nodes_v[_]] + [nodes_agent[_]] )
for _ in range(i):
Gnodes.append([nodes_l[_]] + [nodes_a[_]] + [nodes_agent[_]] )
for _ in range(i):
Gnodes.append([nodes_l[_]] + [nodes_v[_]] + [nodes_agent[_]] )
for _ in range(i):
Gnodes.append([nodes_a[_]] + [nodes_v[_]] + [nodes_agent[_]] )
hyperedge_type1 = hyperedge_type1 + [2]*3*i + [1]*3*i + [0]*4
node_count = node_count + i*num_modality
index1 = torch.LongTensor(index1).view(1,-1)
index2 = torch.LongTensor(index2).view(1,-1)
hyperedge_index = torch.cat([index1,index2],dim=0).cuda()
if self_loop:
max_edge = hyperedge_index[1].max()
max_node = hyperedge_index[0].max()
loops = torch.cat([torch.arange(0,max_node+1,1).repeat_interleave(2).view(1,-1),
torch.arange(max_edge+1,max_edge+1+max_node+1,1).repeat_interleave(2).view(1,-1)],dim=0).cuda()
hyperedge_index = torch.cat([hyperedge_index, loops], dim=1)
edge_index = torch.LongTensor(tmp).T.cuda()
hyperedge_type1 = torch.LongTensor(hyperedge_type1).view(-1,1).cuda()
return hyperedge_index, edge_index, features, batch, hyperedge_type1
def reverse_features(self, dia_len, features):
l=[]
a=[]
v=[]
g=[]
for i in dia_len:
ll = features[0:1*i]
aa = features[1*i:2*i]
vv = features[2*i:3*i]
gg = features[3*i:4*i]
features = features[4*i:]
l.append(ll)
a.append(aa)
v.append(vv)
g.append(gg)
tmpl = torch.cat(l,dim=0)
tmpa = torch.cat(a,dim=0)
tmpv = torch.cat(v,dim=0)
tmpg = torch.cat(g,dim=0)
features = torch.cat([tmpl, tmpa, tmpv, tmpg], dim=-1)
return features
class emoGen(nn.Module):
def __init__(self, a_dim, v_dim, l_dim, n_dim, nlayers, nhidden, nclass, dropout, lamda, alpha, variant, return_feature, use_residue,
new_graph='full',n_speakers=2, modals=['a','v','l'], use_speaker=True, use_modal=False, num_L=3, num_K=4):
super(emoGen, self).__init__()
self.return_feature = return_feature #True
self.use_residue = use_residue
self.new_graph = new_graph
self.nhidden = nhidden
self.act_fn = nn.ReLU()
self.dropout = dropout
self.alpha = alpha
self.lamda = lamda
self.modals = modals
self.modal_embeddings = nn.Embedding(3, n_dim)
self.speaker_embeddings = nn.Embedding(n_speakers, n_dim)
self.use_speaker = use_speaker
self.use_modal = use_modal
self.use_position = False
self.fc1 = nn.Linear(n_dim, nhidden)
self.fc2 = nn.Linear(nhidden * 3, nhidden)
self.num_L = num_L
self.num_K = num_K
for kk in range(num_K):
setattr(self,'conv%d' %(kk+1), highConv(nhidden, nhidden))
def forward(self, a, v, l, dia_len, qmask, epoch):
qmask = torch.cat([qmask[:x,i,:] for i,x in enumerate(dia_len)],dim=0)
spk_idx = torch.argmax(qmask, dim=-1)
spk_emb_vector = self.speaker_embeddings(spk_idx)
if self.use_speaker:
if 'l' in self.modals:
l += spk_emb_vector
if self.use_position:
if 'l' in self.modals:
l = self.l_pos(l, dia_len)
if 'a' in self.modals:
a = self.a_pos(a, dia_len)
if 'v' in self.modals:
v = self.v_pos(v, dia_len)
if self.use_modal:
emb_idx = torch.LongTensor([0, 1, 2]).cuda()
emb_vector = self.modal_embeddings(emb_idx)
if 'a' in self.modals:
a += emb_vector[0].reshape(1, -1).expand(a.shape[0], a.shape[1])
if 'v' in self.modals:
v += emb_vector[1].reshape(1, -1).expand(v.shape[0], v.shape[1])
if 'l' in self.modals:
l += emb_vector[2].reshape(1, -1).expand(l.shape[0], l.shape[1])
gnn_edge_index, gnn_features = self.create_gnn_index(a, v, l, dia_len, self.modals)
x1 = self.fc1(gnn_features)
out = x1
gnn_out = x1
for kk in range(self.num_K):
gnn_out = gnn_out + getattr(self,'conv%d' %(kk+1))(gnn_out,gnn_edge_index)
out = out + gnn_out
if self.use_residue:
out = torch.cat([gnn_features, out], dim=-1)
agent = self.reverse_features(dia_len, out)
agent = self.fc2(agent)
return agent
def reverse_features(self, dia_len, features):
l=[]
a=[]
v=[]
for i in dia_len:
ll = features[0:1*i]
aa = features[1*i:2*i]
vv = features[2*i:3*i]
features = features[3*i:]
l.append(ll)
a.append(aa)
v.append(vv)
tmpl = torch.cat(l,dim=0)
tmpa = torch.cat(a,dim=0)
tmpv = torch.cat(v,dim=0)
features = torch.cat([tmpl, tmpa, tmpv], dim=-1)
return features
def create_gnn_index(self, a, v, l, dia_len, modals):
self_loop = False
num_modality = len(modals)
node_count = 0
batch_count = 0
index =[]
tmp = []
for i in dia_len:
nodes = list(range(i*num_modality))
nodes = [j + node_count for j in nodes]
nodes_l = nodes[0:i*num_modality//3]
nodes_a = nodes[i*num_modality//3:i*num_modality*2//3]
nodes_v = nodes[i*num_modality*2//3:]
index = index + list(permutations(nodes_l,2)) + list(permutations(nodes_a,2)) + list(permutations(nodes_v,2))
Gnodes=[]
for _ in range(i):
Gnodes.append([nodes_l[_]] + [nodes_a[_]] + [nodes_v[_]])
for ii, _ in enumerate(Gnodes):
tmp = tmp + list(permutations(_,2))
if node_count == 0:
ll = l[0:0+i]
aa = a[0:0+i]
vv = v[0:0+i]
features = torch.cat([ll,aa,vv],dim=0)
temp = 0+i
else:
ll = l[temp:temp+i]
aa = a[temp:temp+i]
vv = v[temp:temp+i]
features_temp = torch.cat([ll,aa,vv],dim=0)
features = torch.cat([features,features_temp],dim=0)
temp = temp+i
node_count = node_count + i*num_modality
edge_index = torch.cat([torch.LongTensor(index).T,torch.LongTensor(tmp).T],1).cuda()
return edge_index, features