-
Notifications
You must be signed in to change notification settings - Fork 1
/
persona.py
317 lines (266 loc) · 10.6 KB
/
persona.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
from data import data
from os import path
from io import open
import string
import numpy as np
import pickle
import linecache
import math
import torch
from torch import tensor
import torch.nn as nn
from torch.nn.utils.rnn import pack_padded_sequence,pad_packed_sequence
class attention_feed(nn.Module):
def __init__(self):
super(attention_feed, self).__init__()
def forward(self,target_t,context):
atten=torch.bmm(context,target_t.unsqueeze(2)).sum(2)
mask=((atten!=0).float()-1)*1e9
atten=atten+mask
atten=nn.Softmax(dim=1)(atten)
atten=atten.unsqueeze(1)
context_combined=torch.bmm(atten,context).sum(1)
return context_combined
class softattention(nn.Module):
def __init__(self,params):
super(softattention, self).__init__()
dim = params.dimension
self.attlinear=nn.Linear(dim*2,dim,False) #1024 to 512
def forward(self,target_t,context):
atten=torch.bmm(context,target_t.unsqueeze(2)).sum(2)
mask=((atten!=0).float()-1)*1e9
atten=atten+mask
atten=nn.Softmax(dim=1)(atten)
atten=atten.unsqueeze(1)
context_combined=torch.bmm(atten,context).sum(1)
output=self.attlinear(torch.cat((context_combined,target_t),-1))
output=nn.Tanh()(output)
return output
class lstm_source(nn.Module):
def __init__(self,params):
super(lstm_source, self).__init__()
dim = params.dimension #512
layer = params.layers #4
self.dropout = nn.Dropout(p=params.dropout) #dropout = 0.2
self.lstms=nn.LSTM(dim,dim,num_layers=layer,batch_first=True,bias=False,dropout=params.dropout)
#batch_first – If True, then the input and output tensors are provided as (batch, seq, feature)
def forward(self,embedding,length):
embedding = self.dropout(embedding)
packed=pack_padded_sequence(embedding,length,batch_first=True,enforce_sorted=False)
packed_output,(h,c)=self.lstms(packed)
context,_= pad_packed_sequence(packed_output,batch_first=True)
return context,h,c
class lstm_target(nn.Module):
def __init__(self,params):
super(lstm_target, self).__init__()
dim = params.dimension #512
speaker_dim = params.speakerDimension
layer = params.layers #4
self.dropout = nn.Dropout(p=params.dropout) #0.2
self.speaker = params.SpeakerMode #default False
self.addressee = params.AddresseeMode #default False
persona_num = params.PersonaNum #2
if self.speaker:
self.persona_embedding=nn.Embedding(persona_num,speaker_dim)
self.lstmt=nn.LSTM(dim*2+speaker_dim,dim,num_layers=layer,batch_first=True,bias=False,dropout=params.dropout)
elif self.addressee:
self.persona_embedding=nn.Embedding(persona_num,dim)
self.speaker_linear = nn.Linear(dim,dim)
self.addressee_linear = nn.Linear(dim,dim)
self.lstmt=nn.LSTM(dim*3,dim,num_layers=layer,batch_first=True,bias=False,dropout=params.dropout)
else:
self.lstmt=nn.LSTM(dim*2,dim,num_layers=layer,batch_first=True,bias=False,dropout=params.dropout)
self.atten_feed=attention_feed()
self.soft_atten=softattention(params)
def forward(self,context,h,c,embedding,speaker_label,addressee_label):
embedding = self.dropout(embedding)
h = self.dropout(h)
context1=self.atten_feed(h[-1],context)
context1 = self.dropout(context1)
lstm_input=torch.cat((embedding,context1),-1)
if self.speaker:
speaker_embed=self.persona_embedding(addressee_label)
speaker_embed = self.dropout(speaker_embed)
lstm_input=torch.cat((lstm_input,speaker_embed),-1)
elif self.addressee:
speaker_embed=self.persona_embedding(speaker_label)
speaker_embed = self.dropout(speaker_embed)
addressee_embed=self.persona_embedding(addressee_label)
addressee_embed = self.dropout(addressee_embed)
combined_embed = self.speaker_linear(speaker_embed) + self.addressee_linear(addressee_embed)
combined_embed = nn.Tanh()(combined_embed)
lstm_input=torch.cat((lstm_input,combined_embed),-1)
_,(h,c)=self.lstmt(lstm_input.unsqueeze(1),(h,c))
pred=self.soft_atten(h[-1],context)
return pred,h,c
class lstm(nn.Module):
def __init__(self,params,vocab_num,EOT):
super(lstm, self).__init__()
dim = params.dimension #512
init_weight = params.init_weight #0.1
vocab_num = vocab_num + params.special_word #len(vocabulary) + 3
self.UNK = params.UNK+params.special_word # 3
self.EOT = EOT #2
self.params=params
self.encoder=lstm_source(params)
self.decoder=lstm_target(params)
self.sembed=nn.Embedding(vocab_num,dim,padding_idx=0) #(len(vocabulary) + 3) * 512
self.sembed.weight.data[1:].uniform_(-init_weight,init_weight) #init_weight = 0.1
self.tembed=nn.Embedding(vocab_num,dim,padding_idx=0) #(len(vocabulary) + 3) * 512
self.tembed.weight.data[1:].uniform_(-init_weight,init_weight)
self.softlinear=nn.Linear(dim,vocab_num,False) #512 * (len(vocabulary) + 3)
w=torch.ones(vocab_num) #(len(vocabulary) + 3)
if not params.cpu:
w = w.cuda()
w[:self.EOT]=0 #till 2
w[self.UNK]=0 #3
self.loss_function=torch.nn.CrossEntropyLoss(w, ignore_index=0, reduction='sum')
def forward(self,sources,targets,length,speaker_label,addressee_label):
source_embed=self.sembed(sources)
context,h,c=self.encoder(source_embed,length) #LSTM Source
loss=0
for i in range(targets.size(1)-1): #max_l_t - 1
target_embed=self.tembed(targets[:,i])
pred,h,c=self.decoder(context,h,c,target_embed,speaker_label,addressee_label)
pred=self.softlinear(pred)
loss+=self.loss_function(pred,targets[:,i+1])
return loss
class persona:
def __init__(self, params):
self.params=params
self.ReadDict()
self.Data=data(params,self.voc)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if params.cpu:
self.device="cpu"
self.Model = lstm(params,len(self.voc),self.Data.EOT)
self.Model.encoder.apply(self.weights_init)
self.Model.decoder.apply(self.weights_init)
self.Model.softlinear.apply(self.weights_init)
self.Model.to(self.device)
print("Device being used:",self.device)
self.output=path.join(params.save_folder,params.output_file) #save/testing/log
if self.output!="":
with open(self.output,"w") as selfoutput:
selfoutput.write("")
if self.params.SpeakerMode:
print("training in speaker mode")
elif self.params.AddresseeMode:
print("training in speaker-addressee mode")
else:
print("training in non persona mode")
def weights_init(self,module):
classname=module.__class__.__name__
try:
module.weight.data.uniform_(-self.params.init_weight,self.params.init_weight)
except:
pass
def ReadDict(self):
self.voc = dict()
with open(path.join(self.params.data_folder,self.params.dictPath),'r') as doc:
for line in doc:
self.voc[line.strip()] = len(self.voc)
def test(self):
open_train_file=path.join(self.params.data_folder,self.params.dev_file) #data/testing/valid.txt
total_loss = 0
total_tokens = 0
END=0
batch_n=0
while END==0:
END,sources,targets,speaker_label,addressee_label,length,token_num,origin = self.Data.read_batch(open_train_file,batch_n)
batch_n+=1
if sources is None:
continue
sources=sources.to(self.device)
targets=targets.to(self.device)
speaker_label=speaker_label.to(self.device)
addressee_label=addressee_label.to(self.device)
length=length.to(self.device)
total_tokens+=token_num # adding total number of words in Target for each batch
self.Model.eval() #setting eval mode
with torch.no_grad():
loss = self.Model(sources,targets,length,speaker_label,addressee_label)
total_loss+=loss.item()
print("perp "+str((1/math.exp(-total_loss/total_tokens))))
if self.output!="":
with open(self.output,"a") as selfoutput:
selfoutput.write("standard perp "+str((1/math.exp(-total_loss/total_tokens)))+"\n")
def update(self):
lr=self.params.alpha
grad_norm=0
for m in list(self.Model.parameters()):
m.grad.data = m.grad.data*(1/self.source_size)
grad_norm+=m.grad.data.norm()**2
grad_norm=grad_norm**0.5
if grad_norm>self.params.thres:
lr=lr*self.params.thres/grad_norm
for f in self.Model.parameters():
f.data.sub_(f.grad.data * lr)
def save(self):
save_path = path.join(self.params.save_folder,self.params.save_prefix)
torch.save(self.Model.state_dict(),save_path+str(self.iter))
print("finished saving")
def saveParams(self):
save_params_path = path.join(self.params.save_folder,self.params.save_params) #save/testing/params
with open(save_params_path,"wb") as file:
pickle.dump(self.params,file)
def readModel(self,save_folder,model_name,re_random_weights=None):
target_model = torch.load(path.join(save_folder,model_name)) #save/testing/model
if re_random_weights is not None:
for weight_name in re_random_weights:
random_weight = self.Model.state_dict()[weight_name]
target_model[weight_name] = random_weight
if model_name == "transfer_model":
for name in target_model:
if name == "decoder.lstmt.weight_ih_l0":
target_model[name] = torch.cat((target_model[name],self.Model.state_dict()[name][:,1024:]),-1)
target_model['decoder.persona_embedding.weight'] = self.Model.state_dict()['decoder.persona_embedding.weight']
self.Model.load_state_dict(target_model)
print("read model done")
print("Loaded Model")
def train(self):
if not self.params.no_save: #default False
self.saveParams()
if self.params.fine_tuning: #default False
'''if self.params.SpeakerMode or self.params.AddresseeMode:
re_random_weights = ['decoder.persona_embedding.weight'] # Also have to include some layers of the LSTM module...
else:
re_random_weights = None'''
self.readModel(self.params.save_folder,self.params.fine_tuning_model) #re_random_weights
self.iter=0
start_halving=False
self.lr=self.params.alpha
print("iter "+str(self.iter))
self.test()
while True:
self.iter+=1
print("iter "+str(self.iter))
if self.output!="":
with open(self.output,"a") as selfoutput:
selfoutput.write("iter "+str(self.iter)+"\n")
if self.iter>self.params.start_halve:
self.lr=self.lr*0.5
open_train_file=path.join(self.params.data_folder,self.params.train_file) #data/testing/train.txt
END=0
batch_n=0
while END==0:
self.Model.zero_grad()
END,sources,targets,speaker_label,addressee_label,length,_,_ = self.Data.read_batch(open_train_file,batch_n)
batch_n+=1
if sources is None:
continue
sources=sources.to(self.device)
targets=targets.to(self.device)
speaker_label=speaker_label.to(self.device)
addressee_label=addressee_label.to(self.device)
length=length.to(self.device)
self.source_size = sources.size(0)
self.Model.train() #train mode
loss = self.Model(sources,targets,length,speaker_label,addressee_label)
loss.backward()
self.update()
self.test()
if not self.params.no_save:
self.save()
if self.iter==self.params.max_iter:
break