-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel.py
382 lines (336 loc) · 14.3 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
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
from torch import nn
import torch
import numpy as np
from Agent import Agent
import random
import math
class Model():
def __init__(self, FLAGS, vocab,embed,data_train=None):
self.num_relations = FLAGS.num_relations
self.num_units = FLAGS.num_units
self.dim_embed_relation = FLAGS.dim_embed_relation
self.max_edu_dist = FLAGS.max_edu_dist
self.dim_feature_bi = FLAGS.dim_feature_bi
self.use_structured = FLAGS.use_structured
self.use_speaker_attn = FLAGS.use_speaker_attn
self.use_shared_encoders = FLAGS.use_shared_encoders
self.use_random_structured = FLAGS.use_random_structured
self.use_traditional = FLAGS.use_traditional
self.agent_bi = Agent( FLAGS, vocab,embed, is_multi=False)
self.agent_multi = Agent( FLAGS, vocab,embed, is_multi=True)
self.params_all = []
self.params_all += self.agent_multi.parameters()
self.params_all += self.agent_bi.parameters()
self.optimizer = torch.optim.Adam(self.params_all,lr = FLAGS.learning_rate,betas=(0.9, 0.99))
self.grad_unclipped = [param for param in self.params_all]
self.grad_clipped = torch.nn.utils.clip_grad_norm_(self.grad_unclipped, 5.0)
def get_hs(self,batch):
self.max_num_edus = max([len(dialog["edus"]) for dialog in batch])
self.edus, self.num_posts = [], [] # edus是每个dialog中的词,num posts是每个对话中edu的数目
for dialog in batch:
self.edus.append([])
for edu in dialog["edus"]:
self.edus[-1].append(edu["tokens"])
self.num_posts.append(len(dialog["edus"]))
resc_0,ress_0 = self.agent_bi.ns_encoder(self.edus)
resc_1,ress_1 = self.agent_multi.ns_encoder(self.edus)
self.sentences = [] # 存储所有的句子
self.sentence_idx = [] # 存储句子的编号
for dialog in batch: # 获取句子及其编号
idx = []
for edu in dialog["edus"]:
self.sentences.append(edu["tokens"])
idx.append(len(self.sentences) - 1)
self.sentence_idx.append(idx)
self.hs_bi, self.hs_multi, self.hs_idp, self.hc_bi, self.hc_multi = [], [], [], [], []
for i, dialog in enumerate(batch):
for j in range(len(dialog["edus"])):
self.hs_bi.append(ress_0[i][j])
self.hs_multi.append(ress_1[i][j])
self.hc_bi.append(resc_0[i][j])
self.hc_multi.append(resc_1[i][j])
def build_relation_list(self, batch):
# relation list
cnt_golden = 0
self.relation_list = []
self.relation_types = []
self.parents = []
self.parents_relation = []
self.parents_hp = []
self.parents_relation_hp = []
for k, dialog in enumerate(batch):
self.parents.append([[] for i in range(len(dialog["edus"]))])
self.parents_relation.append([[] for i in range(len(dialog["edus"]))])
self.parents_hp.append([[] for i in range(len(dialog["edus"]))])
self.parents_relation_hp.append([[] for i in range(len(dialog["edus"]))])
self.relation_types.append(np.zeros((len(dialog["edus"]), len(dialog["edus"])), dtype=np.int32))
for relation in dialog["relations"]:
self.relation_types[k][relation["x"]][relation["y"]] = relation["type"] + 1
cnt_golden += 1
for j in range(len(dialog["edus"])):
r = []
for i in range(len(dialog["edus"])):
if self.relation_types[k][i][j] > 0 and \
(i < j and j - i <= self.max_edu_dist):
r.append(i)
self.relation_list.append(r)
return cnt_golden
def get_state(self, batch, hs, hc, hp, k, i, j):
idx_i = self.sentence_idx[k][i]
idx_j = self.sentence_idx[k][j]
speaker_i = batch[k]["edus"][i]["speaker"]
speaker_j = batch[k]["edus"][j]["speaker"]
h = torch.cat([
hc[idx_i],
hs[idx_j],
], dim=-1)
if self.use_structured:
h = torch.cat([
h,
hp[idx_i][speaker_j],
hc[idx_j],
], dim=-1)
else:
h = torch.cat([
h,
hs[idx_i],
hc[idx_j],
], dim=-1)
if self.use_traditional:
h = torch.cat([
h,
torch.Tensor([
j - i,
speaker_i == speaker_j,
batch[k]["edus"][i]["turn"] == batch[k]["edus"][j]["turn"],
(i in self.parents[k][j]) or (j in self.parents[k][i])
])
], dim=-1)
return h
def new_edge(self, batch, k, i, j, r):
# bp gradients of hp first before a new parent is added
self.parents[k][j].append(i)
self.parents_relation[k][j].append(r)
if self.use_random_structured:
i = random.randint(0, j - 1)
r = random.randint(0, self.num_relations - 1)
self.parents_hp[k][j].append(i)
self.parents_relation_hp[k][j].append(r)
idx_j = self.sentence_idx[k][j]
if self.use_structured:
if self.is_root[idx_j]:
self.is_root[idx_j] = 0
self.cntp[idx_j] = 1
else:
self.cntp[idx_j] += 1
for l in range(self.cnt_speakers[k]):
attn = bool(l == batch[k]["edus"][j]["speaker"])
if not self.use_speaker_attn:
attn = 0
def step(self, batch,is_train):
cnt_golden, cnt_pred, cnt_cor_bi, cnt_cor_multi = 0, 0, 0, 0
sum_loss_bi, cnt_loss_bi = torch.FloatTensor([0],),0
total_loss = torch.FloatTensor([0])
sum_loss_multi, cnt_loss_multi = torch.FloatTensor([0]),0
self.get_hs(batch)
if self.use_structured:
pass#获取parent路径编码
else:
self.hp_bi, self.hp_multi = None, None
cnt_golden = self.build_relation_list(batch) # 获得关系列表,cnt_golden为返回的数目
cur = [(1, 0)] * len(batch)
unfinished = np.ones(len(batch), dtype=np.int32)#记录当前batch中的对话的处理情况
for k, dialog in enumerate(batch):
if len(dialog["edus"]) <= 1:
unfinished[k] = False
while (np.sum(unfinished) > 0):
size = np.sum(unfinished) # 获取未完成的数目
golden = np.zeros(size, dtype=np.int32)
idx = 0
state = []
lower = []
for k, dialog in enumerate(batch):
state_k = []
if not unfinished[k]: continue
j = cur[k][0] # 存储了当前batch正在处理的edu的序号
idx_j = self.sentence_idx[k][j]
lower.append(0)
for i in range(j):
if j - i <= self.max_edu_dist:
if (i in self.parents[k][j]): continue
state_k.append(self.get_state(
batch,
self.hs_bi,
self.hc_bi,
self.hp_bi,
k, i, j
) ) # 获得2元组的向量表示
lower[idx] = max(0,j-self.max_edu_dist)
state.append(state_k)
golden[idx] = 0
flag = False
for i in self.relation_list[idx_j]:
if (i in self.parents[k][j]): continue
golden[idx] = i
flag = True
break
if not flag:
lower[idx] = 0
idx += 1
policy = self.agent_bi(state)
action = self.sample_action(policy)
if not is_train:
print('action',action)
print('golden',golden)
print()
idx = 0
for k, dialog in enumerate(batch):
if not unfinished[k]: continue
# predicted a new relation
if action[idx] != len(dialog["edus"]):
cnt_pred += 1
if self.relation_types[k][action[idx]][cur[k][0]] > 0: # 依存预测正确
cnt_cor_bi += 1
idx += 1
loss = self.Loss_multi(golden,policy,lower,False)
##########################上面代码存在out of range的问题#############################
cnt_loss_bi += 1
sum_loss_bi += loss
#predict labels
idx = 0
state_multi, golden_multi, idx_multi = [], [], []
state_multi_train, golden_multi_train, idx_multi_train = [], [], []
for k, dialog in enumerate(batch):
if not unfinished[k]: continue
j = cur[k][0]
if action[idx] != len(dialog["edus"]):
i = action[idx]
if self.use_shared_encoders:
state_multi.append(self.get_state(
batch,
self.hs_bi,
self.hc_bi,
self.hp_bi,
k, i, j
))
else:
state_multi.append(self.get_state(
batch,
self.hs_multi,
self.hc_multi,
self.hp_multi,
k, i, j
))
idx_multi.append((k, i, j))
for i in range(j):
if self.relation_types[k][i][j] > 0:
if i in self.parents[k][j]: continue
if self.use_shared_encoders:
state_multi_train.append(self.get_state(
batch,
self.hs_bi,
self.hc_bi,
self.hp_bi,
k, i, j)
)
else:
state_multi_train.append(self.get_state(
batch,
self.hs_multi,
self.hc_multi,
self.hp_multi,
k, i, j)
)
idx_multi_train.append((k, i, j))
golden_multi_train.append(self.relation_types[k][i][j] - 1)
idx += 1
if len(idx_multi) > 0:
with torch.no_grad():
policy = self.agent_multi(state_multi)
labels = self.sample_action(policy)
if len(idx_multi_train) > 0:
policy = self.agent_multi(state_multi_train)
loss = self.Loss_multi(golden_multi_train,policy,None,True)
sum_loss_multi += loss
cnt_loss_multi += 1
idx, idx_multi = 0, 0
for k, dialog in enumerate(batch):
if not unfinished[k]: continue
# predicted a new relation
if action[idx] != len(dialog["edus"]):
if labels[idx_multi] == self.relation_types[k][action[idx]][cur[k][0]] - 1:
cnt_cor_multi += 1
idx_multi += 1
idx += 1
idx, idx_multi, idx_multi_train = 0, 0, 0
for k, dialog in enumerate(batch):
if not unfinished[k]: continue
# valid prediction
if action[idx] != len(dialog["edus"]):
r = labels[idx_multi]
if self.relation_types[k][action[idx]][cur[k][0]] > 0:
idx_multi_train += 1
idx_multi += 1
self.new_edge(batch, k, action[idx], cur[k][0], r)
cur[k] = (cur[k][0] + 1, 0)
if cur[k][0] >= len(dialog["edus"]):
unfinished[k] = False
idx += 1
#print(sum_loss_multi.detach())
#print(sum_loss_bi.detach())
total_loss += sum_loss_multi
total_loss += sum_loss_bi
self.optimizer.zero_grad()
try:
total_loss.backward()
except:
print('here')
self.optimizer.step()
relations_pred = []
for k, dialog in enumerate(batch):
relations_pred.append([])
for i in range(len(dialog["edus"])):
for j in range(len(self.parents[k][i])):
relations_pred[k].append((self.parents[k][i][j], i, self.parents_relation[k][i][j]))
for dialog in batch:
cnt = [0] * len(dialog["edus"])
for r in dialog["relations"]:
cnt[r["y"]] += 1
for i in range(len(dialog["edus"])):
if cnt[i] == 0:
cnt_golden += 1
cnt_pred += 1
if cnt[0] == 0:
cnt_cor_bi += 1
cnt_cor_multi += 1
return [
sum_loss_bi.detach() / cnt_loss_bi if cnt_loss_bi > 0 else 0,
sum_loss_multi.detach() / cnt_loss_multi if cnt_loss_multi > 0 else 0,
cnt_golden, cnt_pred, cnt_cor_bi, cnt_cor_multi,
relations_pred,
]
def Loss_multi(self,golden,policy,lower,is_multi):
if is_multi:
loss = []
for i in range(len(policy)):
loss.append(policy[i][golden[i]].view(-1))
loss = torch.cat(loss,dim = 0)
loss = torch.log(loss)
loss = torch.mean(loss)
return -loss
else:
try:
loss = []
for i in range(len(policy)):
loss.append(policy[i][golden[i]-lower[i]].view(-1))
loss = torch.cat(loss, dim=0)
except:
print('hello')
loss = torch.log(loss)
loss = torch.mean(loss)
return -loss
def sample_action(self, policy):
action = []
for p in policy:
action.append(torch.argmax(p.detach()))
return action