-
Notifications
You must be signed in to change notification settings - Fork 23
/
WSeq.py
264 lines (214 loc) · 9.83 KB
/
WSeq.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
#!/usr/bin/python3
# Author: GMFTBY
# Time: 2019.9.14
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import random
import numpy as np
import ipdb
from .layers import *
'''
WSeq is a HRED-based model which uses cosine similarity as the attention weight
'''
class Utterance_encoder(nn.Module):
'''
Bidirectional GRU
'''
def __init__(self, input_size, embedding_size,
hidden_size, dropout=0.5, n_layer=1, pretrained=None):
super(Utterance_encoder, self).__init__()
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.input_size = input_size
self.n_layer = n_layer
self.embed = nn.Embedding(input_size, self.embedding_size)
self.gru = nn.GRU(self.embedding_size, self.hidden_size, num_layers=n_layer,
dropout=dropout, bidirectional=True)
# hidden_project
# self.hidden_proj = nn.Linear(n_layer * 2 * self.hidden_size, hidden_size)
# self.bn = nn.BatchNorm1d(num_features=hidden_size)
self.init_weight()
def init_weight(self):
init.xavier_normal_(self.gru.weight_hh_l0)
init.xavier_normal_(self.gru.weight_ih_l0)
self.gru.bias_ih_l0.data.fill_(0.0)
self.gru.bias_hh_l0.data.fill_(0.0)
def forward(self, inpt, lengths, hidden=None):
# use pack_padded
# inpt: [seq_len, batch], lengths: [batch_size]
embedded = self.embed(inpt) # [seq_len, batch, input_size]
if not hidden:
hidden = torch.randn(self.n_layer * 2, len(lengths),
self.hidden_size)
if torch.cuda.is_available():
hidden = hidden.cuda()
embedded = nn.utils.rnn.pack_padded_sequence(embedded, lengths, enforce_sorted=False)
_, hidden = self.gru(embedded, hidden)
hidden = hidden.sum(axis=0)
hidden = torch.tanh(hidden)
# [n_layer * bidirection, batch, hidden_size]
# hidden = hidden.reshape(hidden.shape[1], -1)
# ipdb.set_trace()
# hidden = hidden.permute(1, 0, 2) # [batch, n_layer * bidirectional, hidden_size]
# hidden = hidden.reshape(hidden.size(0), -1) # [batch, *]
# hidden = self.bn(self.hidden_proj(hidden))
# hidden = torch.tanh(hidden) # [batch, hidden]
return hidden
class Context_encoder(nn.Module):
'''
input_size is 2 * utterance_hidden_size
'''
def __init__(self, input_size, hidden_size, dropout=0.5):
super(Context_encoder, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.gru = nn.GRU(self.input_size, self.hidden_size, bidirectional=True)
# self.drop = nn.Dropout(p=dropout)
self.init_weight()
def init_weight(self):
init.xavier_normal_(self.gru.weight_hh_l0)
init.xavier_normal_(self.gru.weight_ih_l0)
self.gru.bias_ih_l0.data.fill_(0.0)
self.gru.bias_hh_l0.data.fill_(0.0)
def forward(self, inpt, hidden=None):
# inpt: [turn_len, batch, input_size]
# hidden
if not hidden:
hidden = torch.randn(2, inpt.shape[1], self.hidden_size)
if torch.cuda.is_available():
hidden = hidden.cuda()
# inpt = self.drop(inpt)
output, hidden = self.gru(inpt, hidden)
output = output[:, :, :self.hidden_size] + output[:, :, self.hidden_size:]
# hidden: [2, batch, hidden_size]
hidden = torch.tanh(hidden) # [batch, hidden_size]
return output, hidden
class Decoder(nn.Module):
'''
Max likelyhood for decoding the utterance
input_size is the size of the input vocabulary
Attention module should satisfy that the decoder_hidden size is the same as
the Context encoder hidden size
WSeq attention in the decoder part
smaller model size than HRED-attn but has the attention part
'''
def __init__(self, output_size, embed_size, hidden_size, n_layer=2, dropout=0.5, pretrained=None):
super(Decoder, self).__init__()
self.output_size = output_size
self.hidden_size = hidden_size
self.embed_size = embed_size
self.embed = nn.Embedding(self.output_size, self.embed_size)
self.gru = nn.GRU(self.embed_size + self.hidden_size, self.hidden_size,
num_layers=n_layer,
dropout=(0 if n_layer == 1 else dropout))
self.out = nn.Linear(hidden_size, output_size)
# attention on context encoder
self.attn = WSeq_attention(hidden_size)
self.init_weight()
def init_weight(self):
init.xavier_normal_(self.gru.weight_hh_l0)
init.xavier_normal_(self.gru.weight_ih_l0)
self.gru.bias_ih_l0.data.fill_(0.0)
self.gru.bias_hh_l0.data.fill_(0.0)
def forward(self, inpt, last_hidden, encoder_outputs):
# inpt: [batch_size], last_hidden: [1, batch, hidden_size]
# encoder_outputs: [turn_len, batch, hidden_size]
embedded = self.embed(inpt).unsqueeze(0) # [1, batch_size, embed_size]
# [batch, hidden_size]
if len(encoder_outputs) == 1:
context = self.attn(encoder_outputs[0], encoder_outputs)
else:
context = self.attn(encoder_outputs[-1], encoder_outputs[:-1])
context = context.unsqueeze(0) # [1, batch, hidden]
rnn_input = torch.cat([embedded, context], 2) # [1, batch, 2 * hidden]
# output: [1, batch, hidden_size], hidden: [1, batch, hidden_size]
output, hidden = self.gru(rnn_input, last_hidden)
output = output.squeeze(0) # [batch, hidden_size]
# context = context.squeeze(0) # [batch, hidden]
# output = torch.cat([output, context], 1) # [batch, 2 * hidden]
output = self.out(output) # [batch, output_size]
output = F.log_softmax(output, dim=1)
return output, hidden
class WSeq(nn.Module):
def __init__(self, embed_size, input_size, output_size,
utter_hidden, context_hidden, decoder_hidden,
teach_force=0.5, pad=24745, sos=24742, dropout=0.5,
utter_n_layer=1, pretrained=None):
super(WSeq, self).__init__()
self.teach_force = teach_force
self.output_size = output_size
self.pad, self.sos = pad, sos
self.utter_encoder = Utterance_encoder(input_size, embed_size, utter_hidden,
dropout=dropout, n_layer=utter_n_layer,
pretrained=pretrained)
self.context_encoder = Context_encoder(utter_hidden, context_hidden,
dropout=dropout)
self.decoder = Decoder(output_size, embed_size, decoder_hidden,
n_layer=utter_n_layer, dropout=dropout, pretrained=pretrained)
def forward(self, src, tgt, lengths):
# src: [turns, lengths, batch], tgt: [lengths, batch]
# lengths: [turns, batch]
turn_size, batch_size, maxlen = len(src), tgt.size(1), tgt.size(0)
outputs = torch.zeros(maxlen, batch_size, self.output_size)
if torch.cuda.is_available():
outputs = outputs.cuda()
# utterance encoding
turns = []
for i in range(turn_size):
# sbatch = src[i].transpose(0, 1) # [seq_len, batch]
hidden = self.utter_encoder(src[i], lengths[i]) # utter_hidden
turns.append(hidden)
turns = torch.stack(turns) # [turn_len, batch, utter_hidden]
# context encoding
# output: [seq, batch, hidden], [batch, hidden]
context_output, hidden = self.context_encoder(turns)
# decoding
# tgt = tgt.transpose(0, 1) # [seq_len, batch]
# hidden = hidden.unsqueeze(0) # [1, batch, hidden_size]
output = tgt[0, :] # [batch]
use_teacher = random.random() < self.teach_force
if use_teacher:
for t in range(1, maxlen):
output, hidden = self.decoder(output, hidden, context_output)
outputs[t] = output
output = tgt[t]
else:
for t in range(1, maxlen):
output, hidden = self.decoder(output, hidden, context_output)
outputs[t] = output
output = output.topk(1)[1].squeeze().detach()
return outputs # [maxlen, batch, vocab_size]
def predict(self, src, maxlen, lengths, loss=False):
# predict for test dataset, return outputs: [maxlen, batch_size]
# src: [turn, max_len, batch_size], lengths: [turn, batch_size]
with torch.no_grad():
turn_size, batch_size = len(src), src[0].size(1)
outputs = torch.zeros(maxlen, batch_size)
floss = torch.zeros(maxlen, batch_size, self.output_size)
if torch.cuda.is_available():
outputs = outputs.cuda()
floss = floss.cuda()
turns = []
for i in range(turn_size):
# sbatch = src[i].transpose(0, 1)
hidden = self.utter_encoder(src[i], lengths[i])
turns.append(hidden)
turns = torch.stack(turns)
context_output, hidden = self.context_encoder(turns)
# hidden = hidden.unsqueeze(0)
output = torch.zeros(batch_size, dtype=torch.long).fill_(self.sos)
if torch.cuda.is_available():
output = output.cuda()
for i in range(1, maxlen):
output, hidden = self.decoder(output, hidden, context_output)
floss[i] = output
output = output.max(1)[1]
outputs[i] = output
if loss:
return outputs, floss
else:
return outputs
if __name__ == "__main__":
pass