-
Notifications
You must be signed in to change notification settings - Fork 18
/
model.py
332 lines (267 loc) · 11.5 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np, itertools, random, copy, math
from transformers import BertModel, BertConfig
from transformers import AutoTokenizer, AutoModelWithLMHead
from model_utils import *
class BertERC(nn.Module):
def __init__(self, args, num_class):
super().__init__()
self.args = args
# gcn layer
self.dropout = nn.Dropout(args.dropout)
# bert_encoder
self.bert_config = BertConfig.from_json_file(args.bert_model_dir + 'config.json')
self.bert = BertModel.from_pretrained(args.home_dir + args.bert_model_dir, config = self.bert_config)
in_dim = args.bert_dim
# output mlp layers
layers = [nn.Linear(in_dim, args.hidden_dim), nn.ReLU()]
for _ in range(args.mlp_layers- 1):
layers += [nn.Linear(args.hidden_dim, args.hidden_dim), nn.ReLU()]
layers += [nn.Linear(args.hidden_dim, num_class)]
self.out_mlp = nn.Sequential(*layers)
def forward(self, content_ids, token_types,utterance_len,seq_len):
# the embeddings for bert
# if len(content_ids)>512:
# print('ll')
#
## w token_type_ids
# lastHidden = self.bert(content_ids, token_type_ids = token_types)[1] #(N , D)
## w/t token_type_ids
lastHidden = self.bert(content_ids)[1] #(N , D)
final_feature = self.dropout(lastHidden)
# pooling
outputs = self.out_mlp(final_feature) #(N, D)
return outputs
class DAGERC(nn.Module):
def __init__(self, args, num_class):
super().__init__()
self.args = args
# gcn layer
self.dropout = nn.Dropout(args.dropout)
self.gnn_layers = args.gnn_layers
if not args.no_rel_attn:
self.rel_emb = nn.Embedding(2,args.hidden_dim)
self.rel_attn = True
else:
self.rel_attn = False
if self.args.attn_type == 'linear':
gats = []
for _ in range(args.gnn_layers):
gats += [GatLinear(args.hidden_dim) if args.no_rel_attn else GatLinear_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
else:
gats = []
for _ in range(args.gnn_layers):
gats += [Gatdot(args.hidden_dim) if args.no_rel_attn else Gatdot_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
grus = []
for _ in range(args.gnn_layers):
grus += [nn.GRUCell(args.hidden_dim, args.hidden_dim)]
self.grus = nn.ModuleList(grus)
self.fc1 = nn.Linear(args.emb_dim, args.hidden_dim)
in_dim = args.hidden_dim * (args.gnn_layers + 1) + args.emb_dim
# output mlp layers
layers = [nn.Linear(in_dim, args.hidden_dim), nn.ReLU()]
for _ in range(args.mlp_layers - 1):
layers += [nn.Linear(args.hidden_dim, args.hidden_dim), nn.ReLU()]
layers += [nn.Linear(args.hidden_dim, num_class)]
self.out_mlp = nn.Sequential(*layers)
def forward(self, features, adj,s_mask):
'''
:param features: (B, N, D)
:param adj: (B, N, N)
:param s_mask: (B, N, N)
:return:
'''
num_utter = features.size()[1]
if self.rel_attn:
rel_ft = self.rel_emb(s_mask) # (B, N, N, D)
H0 = F.relu(self.fc1(features)) # (B, N, D)
H = [H0]
for l in range(self.args.gnn_layers):
H1 = self.grus[l](H[l][:,0,:]).unsqueeze(1) # (B, 1, D)
for i in range(1, num_utter):
if not self.rel_attn:
_, M = self.gather[l](H[l][:,i,:], H1, H1, adj[:,i,:i])
else:
_, M = self.gather[l](H[l][:, i, :], H1, H1, adj[:, i, :i], rel_ft[:, i, :i, :])
H1 = torch.cat((H1 , self.grus[l](H[l][:,i,:], M).unsqueeze(1)), dim = 1)
# print('H1', H1.size())
# print('----------------------------------------------------')
H.append(H1)
H0 = H1
H.append(features)
H = torch.cat(H, dim = 2) #(B, N, l*D)
logits = self.out_mlp(H)
return logits
class DAGERC_fushion(nn.Module):
def __init__(self, args, num_class):
super().__init__()
self.args = args
# gcn layer
self.dropout = nn.Dropout(args.dropout)
self.gnn_layers = args.gnn_layers
if not args.no_rel_attn:
self.rel_attn = True
else:
self.rel_attn = False
if self.args.attn_type == 'linear':
gats = []
for _ in range(args.gnn_layers):
gats += [GatLinear(args.hidden_dim) if args.no_rel_attn else GatLinear_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
elif self.args.attn_type == 'dotprod':
gats = []
for _ in range(args.gnn_layers):
gats += [GatDot(args.hidden_dim) if args.no_rel_attn else GatDot_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
elif self.args.attn_type == 'rgcn':
gats = []
for _ in range(args.gnn_layers):
# gats += [GAT_dialoggcn(args.hidden_dim)]
gats += [GAT_dialoggcn_v1(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
grus_c = []
for _ in range(args.gnn_layers):
grus_c += [nn.GRUCell(args.hidden_dim, args.hidden_dim)]
self.grus_c = nn.ModuleList(grus_c)
grus_p = []
for _ in range(args.gnn_layers):
grus_p += [nn.GRUCell(args.hidden_dim, args.hidden_dim)]
self.grus_p = nn.ModuleList(grus_p)
fcs = []
for _ in range(args.gnn_layers):
fcs += [nn.Linear(args.hidden_dim * 2, args.hidden_dim)]
self.fcs = nn.ModuleList(fcs)
self.fc1 = nn.Linear(args.emb_dim, args.hidden_dim)
self.nodal_att_type = args.nodal_att_type
in_dim = args.hidden_dim * (args.gnn_layers + 1) + args.emb_dim
# output mlp layers
layers = [nn.Linear(in_dim, args.hidden_dim), nn.ReLU()]
for _ in range(args.mlp_layers - 1):
layers += [nn.Linear(args.hidden_dim, args.hidden_dim), nn.ReLU()]
layers += [self.dropout]
layers += [nn.Linear(args.hidden_dim, num_class)]
self.out_mlp = nn.Sequential(*layers)
self.attentive_node_features = attentive_node_features(in_dim)
def forward(self, features, adj,s_mask,s_mask_onehot, lengths):
'''
:param features: (B, N, D)
:param adj: (B, N, N)
:param s_mask: (B, N, N)
:param s_mask_onehot: (B, N, N, 2)
:return:
'''
num_utter = features.size()[1]
H0 = F.relu(self.fc1(features))
# H0 = self.dropout(H0)
H = [H0]
for l in range(self.args.gnn_layers):
C = self.grus_c[l](H[l][:,0,:]).unsqueeze(1)
M = torch.zeros_like(C).squeeze(1)
# P = M.unsqueeze(1)
P = self.grus_p[l](M, H[l][:,0,:]).unsqueeze(1)
#H1 = F.relu(self.fcs[l](torch.cat((C,P) , dim = 2)))
#H1 = F.relu(C+P)
H1 = C+P
for i in range(1, num_utter):
# print(i,num_utter)
if self.args.attn_type == 'rgcn':
_, M = self.gather[l](H[l][:,i,:], H1, H1, adj[:,i,:i], s_mask[:,i,:i])
# _, M = self.gather[l](H[l][:,i,:], H1, H1, adj[:,i,:i], s_mask_onehot[:,i,:i,:])
else:
if not self.rel_attn:
_, M = self.gather[l](H[l][:,i,:], H1, H1, adj[:,i,:i])
else:
_, M = self.gather[l](H[l][:,i,:], H1, H1, adj[:,i,:i], s_mask[:, i, :i])
C = self.grus_c[l](H[l][:,i,:], M).unsqueeze(1)
P = self.grus_p[l](M, H[l][:,i,:]).unsqueeze(1)
# P = M.unsqueeze(1)
#H_temp = F.relu(self.fcs[l](torch.cat((C,P) , dim = 2)))
#H_temp = F.relu(C+P)
H_temp = C+P
H1 = torch.cat((H1 , H_temp), dim = 1)
# print('H1', H1.size())
# print('----------------------------------------------------')
H.append(H1)
H.append(features)
H = torch.cat(H, dim = 2)
H = self.attentive_node_features(H,lengths,self.nodal_att_type)
logits = self.out_mlp(H)
return logits
class DAGERC_v2(nn.Module):
def __init__(self, args, num_class):
super().__init__()
self.args = args
# gcn layer
self.dropout = nn.Dropout(args.dropout)
self.gnn_layers = args.gnn_layers
if not args.no_rel_attn:
self.rel_attn = True
else:
self.rel_attn = False
if self.args.attn_type == 'linear':
gats = []
for _ in range(args.gnn_layers):
gats += [GatLinear(args.hidden_dim) if args.no_rel_attn else GatLinear_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
else:
gats = []
for _ in range(args.gnn_layers):
gats += [GatDot(args.hidden_dim) if args.no_rel_attn else GatDot_rel(args.hidden_dim)]
self.gather = nn.ModuleList(gats)
grus_c = []
for _ in range(args.gnn_layers):
grus_c += [nn.GRUCell(args.hidden_dim, args.hidden_dim)]
self.grus_c = nn.ModuleList(grus_c)
grus_p = []
for _ in range(args.gnn_layers):
grus_p += [nn.GRUCell(args.hidden_dim, args.hidden_dim)]
self.grus_p = nn.ModuleList(grus_p)
self.fc1 = nn.Linear(args.emb_dim, args.hidden_dim)
in_dim = args.hidden_dim * (args.gnn_layers * 2 + 1) + args.emb_dim
# output mlp layers
layers = [nn.Linear(in_dim, args.hidden_dim), nn.ReLU()]
for _ in range(args.mlp_layers - 1):
layers += [nn.Linear(args.hidden_dim, args.hidden_dim), nn.ReLU()]
layers += [nn.Linear(args.hidden_dim, num_class)]
self.out_mlp = nn.Sequential(*layers)
def forward(self, features, adj,s_mask):
'''
:param features: (B, N, D)
:param adj: (B, N, N)
:param s_mask: (B, N, N)
:return:
'''
num_utter = features.size()[1]
if self.rel_attn:
rel_ft = self.rel_emb(s_mask) # (B, N, N, D)
H0 = F.relu(self.fc1(features)) # (B, N, D)
H = [H0]
C = [H0]
for l in range(self.args.gnn_layers):
CL = self.grus_c[l](C[l][:,0,:]).unsqueeze(1) # (B, 1, D)
M = torch.zeros_like(CL).squeeze(1)
# P = M.unsqueeze(1)
P = self.grus_p[l](M, C[l][:,0,:]).unsqueeze(1) # (B, 1, D)
for i in range(1, num_utter):
if not self.rel_attn:
_, M = self.gather[l](C[l][:,i,:], P, P, adj[:,i,:i])
else:
_, M = self.gather[l](C[l][:, i, :], P, P, adj[:, i, :i], rel_ft[:, i, :i, :])
C_ = self.grus_c[l](C[l][:,i,:], M).unsqueeze(1)# (B, 1, D)
P_ = self.grus_p[l](M, H[l][:,i,:]).unsqueeze(1)# (B, 1, D)
# P = M.unsqueeze(1)
CL = torch.cat((CL, C_), dim = 1) # (B, i, D)
P = torch.cat((P, P_), dim = 1) # (B, i, D)
# print('H1', H1.size())
# print('----------------------------------------------------')
C.append(CL)
H.append(CL)
H.append(P)
H.append(features)
H = torch.cat(H, dim = 2) #(B, N, l*D)
logits = self.out_mlp(H)
return logits