-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
executable file
·303 lines (264 loc) · 11.6 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.utils.data as data
import torch.utils.data.sampler as sampler
import torchvision
from torchvision import datasets, transforms
import numpy as np
import sys
import os
import random
import json
class Encoder(nn.Module):
def __init__(self, 華_vocab_size, emb_dim, hid_dim, n_layers, dropout):
super().__init__()
self.embedding = nn.Embedding(華_vocab_size, emb_dim)
self.hid_dim = hid_dim
self.n_layers = n_layers
self.rnn = nn.GRU(emb_dim, hid_dim, n_layers,
dropout=dropout, batch_first=True, bidirectional=True)
# (rnn): GRU(256, 1024, num_layers=3, batch_first=True, dropout=0.5)
self.dropout = nn.Dropout(dropout)
def forward(self, input):
# input = [batch size, max_sequence len]
"""
input[0] looks like this :
torch.Size([60, 72])
tensor([ 1, 554, 711, 386, 1287, 133, 1511, 3049, 4, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
device='cuda:0')
"""
embedding = self.embedding(input)
outputs, hidden = self.rnn(self.dropout(embedding))
"""
outputs.shape :
torch.Size([60, 72, 1024])
hidden.shape:
torch.Size([6, 60, 512])
# 6 表示 n_layer(3) * 2 共 6 層, 最後一個 timestep 的 hidden state
"""
# outputs = [batch size, sequence len, hid dim * directions]
# hidden = [num_layers * directions, batch size , hid dim]
# 幹太神奇了吧, batch_first=True 不會讓 hidden 的 batch_size 在最前面
# outputs 是最上層RNN的輸出
return outputs, hidden
class Attention(nn.Module):
def __init__(self, hid_dim):
super(Attention, self).__init__()
self.hid_dim = hid_dim
def forward(self, encoder_outputs, decoder_hidden):
# encoder_outputs = [batch size, sequence len, hid_dim * directions]
# decoder_hidden = [num_layers, batch size, hid dim]
# 一般來說是取 Encoder 最後一層的 hidden state 來做 attention
"""
Decoder 的 forward :
attn = self.attention(encoder_outputs, hidden)
hidden 為 Encoder最後 / Decoder 的 hidden state
encoder_outputs.shape:
torch.Size([60, 72, 1024])
hidden.shape:
torch.Size([3, 60, 1024])
"""
decoder_hidden = decoder_hidden.permute(1, 2, 0)# (60, 1024, 3)
matrix = torch.matmul(encoder_outputs, decoder_hidden) #(60, 72, 3)
matrix = torch.mean(matrix, dim=2) #(60, 72)
attention_weights = F.softmax(matrix ,dim=1) #(60, 72)
attention = torch.matmul(encoder_outputs.transpose(1,2), attention_weights.unsqueeze(2)).transpose(1, 2)
"""
print(attention.shape)
torch.Size([60, 1, 1024])
"""
#return attentino vector
return attention
class Decoder(nn.Module):
def __init__(self, 閩_vocab_size, emb_dim, hid_dim, n_layers, dropout, isatt):
super().__init__()
self.閩_vocab_size = 閩_vocab_size
self.hid_dim = hid_dim * 2
self.n_layers = n_layers
self.embedding = nn.Embedding(閩_vocab_size, emb_dim)
self.isatt = isatt
self.attention = Attention(hid_dim)
# 如果使用 Attention Mechanism 會使得輸入維度變化,請在這裡修改
# e.g. Attention 接在輸入後面會使得維度變化,所以輸入維度改為
self.input_dim = emb_dim + hid_dim * 2 if isatt else emb_dim
#self.input_dim = emb_dim
self.rnn = nn.GRU(self.input_dim, self.hid_dim,
self.n_layers, dropout=dropout, batch_first=True)
self.embedding2vocab1 = nn.Linear(self.hid_dim, self.hid_dim * 2)
self.embedding2vocab2 = nn.Linear(self.hid_dim * 2, self.hid_dim * 4)
self.embedding2vocab3 = nn.Linear(self.hid_dim * 4, self.閩_vocab_size)
self.dropout = nn.Dropout(dropout)
def forward(self, input, hidden, encoder_outputs):
# input = [batch size, vocab size]
# hidden = [n_layer, batch_size, hid_dim*2] (modified by kaiwei)
# Decoder 只會是單向,所以 directions=1
"""
input.shape:
torch.Size([60])
hidden.shape:
torch.Size([3, 60, 1024])
"""
input = input.unsqueeze(1)
"""
input.shape:
torch.Size([60, 1])
"""
embedded = self.dropout(self.embedding(input))
# embedded = [batch size, 1, emb dim]
"""
print(embedded.shape)
embedded.shape:
torch.Size([60, 1, 256])
"""
if self.isatt:
"""
print(encoder_outputs.shape)
print(hidden.shape)
encoder_outputs.shape:
torch.Size([60, 72, 1024])
hidden.shape:
torch.Size([3, 60, 1024])
"""
attn = self.attention(encoder_outputs, hidden)
embedded = torch.cat((embedded, attn), dim=2)
"""
print(embedded.shape)
embedded.shape:
torch.Size([60, 1, 1280])
"""
# TODO: 在這裡決定如何使用 Attention,e.g. 相加 或是 接在後面, 請注意維度變化
output, hidden = self.rnn(embedded, hidden)
# output = [batch size, 1, hid dim]
# hidden = [num_layers, batch size, hid dim]
# 將 RNN 的輸出轉為每個詞出現的機率
output = self.embedding2vocab1(output.squeeze(1))
output = self.embedding2vocab2(output)
prediction = self.embedding2vocab3(output)
# prediction = [batch size, vocab size]
return prediction, hidden
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder, device):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
assert encoder.n_layers == decoder.n_layers, \
"Encoder and decoder must have equal number of layers!"
def forward(self, input, target, teacher_forcing_ratio):
# input = [batch size, input len, vocab size]
# target = [batch size, target len, vocab size]
# teacher_forcing_ratio 是有多少機率使用正確答案來訓練
batch_size = target.shape[0]
target_len = target.shape[1]
vocab_size = self.decoder.閩_vocab_size
# 準備一個儲存空間來儲存輸出
outputs = torch.zeros(batch_size, target_len,
vocab_size).to(self.device)
# 將輸入放入 Encoder
encoder_outputs, hidden = self.encoder(input)
# Encoder 最後的隱藏層(hidden state) 用來初始化 Decoder
# encoder_outputs 主要是使用在 Attention
# 因為 Encoder 是雙向的RNN,所以需要將同一層兩個方向的 hidden state 接在一起
# hidden = [num_layers * directions, batch size , hid dim] --> [num_layers, directions, batch size , hid dim]
"""
hidden.shape:
torch.Size([6, 60,512])
"""
hidden = hidden.view(self.encoder.n_layers, 2, batch_size, -1)
hidden = torch.cat((hidden[:, -2, :, :], hidden[:, -1, :, :]), dim=2)
"""
hidden.shape:
torch.Size([3, 60,1024])
"""
# 取得 <BOS> token
input = target[:, 0]
preds = []
for t in range(1, target_len):
"""
input.shape:
torch.Size([60])
hidden.shape:
torch.Size([3, 60, 1024])
encoder_outputs.shape:
torch.Size([60, 72, 1024])
"""
output, hidden = self.decoder(input, hidden, encoder_outputs)
outputs[:, t] = output
# 決定是否用正確答案來做訓練
teacher_force = random.random() <= teacher_forcing_ratio
# 取出機率最大的單詞
top1 = output.argmax(1)
# 如果是 teacher force 則用正解訓練,反之用自己預測的單詞做預測
input = target[:, t] if teacher_force and t < target_len else top1
preds.append(top1.unsqueeze(1))
preds = torch.cat(preds, 1)
return outputs, preds
def inference(self, source):
########
# TODO #
########
# 在這裡實施 Beam Search
# 此函式的 batch size = 1
# source = [batch size, source len, vocab size]
# target = [batch size, target len, vocab size]
batch_size = source.shape[0]
source_len = source.shape[1] # 取得最大字數
vocab_size = self.decoder.閩_vocab_size
# 準備一個儲存空間來儲存輸出
outputs = torch.zeros(batch_size, source_len,
vocab_size).to(self.device)
# 將輸入放入 Encoder
encoder_outputs, hidden = self.encoder(source)
# Encoder 最後的隱藏層(hidden state) 用來初始化 Decoder
# encoder_outputs 主要是使用在 Attention
# 因為 Encoder 是雙向的RNN,所以需要將同一層兩個方向的 hidden state 接在一起
# hidden = [num_layers * directions, batch size , hid dim] --> [num_layers, directions, batch size , hid dim]
hidden = hidden.view(self.encoder.n_layers, 2, batch_size, -1)
hidden = torch.cat((hidden[:, -2, :, :], hidden[:, -1, :, :]), dim=2)
# 取的 <BOS> token
# input is just tensor([1], device='cuda:0')
# input = target[:, 0]
d_input = source[:, 0]
preds = []
for t in range(1, source_len): #time step
output, hidden = self.decoder(d_input, hidden, encoder_outputs)
# 將預測結果存起來
outputs[:, t] = output
# 取出機率最大的單詞
top1 = output.argmax(1)
d_input = top1
preds.append(top1.unsqueeze(1))
preds = torch.cat(preds, 1)
return outputs, preds
#=============================================================================#
def save_model(model, optimizer, store_model_path, step):
torch.save(model.state_dict(), f'{store_model_path}/model_{step}.ckpt')
return
def load_model(model, load_model_path):
print(f'Load model from {load_model_path}')
model.load_state_dict(torch.load(f'{load_model_path}'))
return model
def build_model(config, 華_vocab_size, 閩_vocab_size, device):
# 建構模型
encoder = Encoder(華_vocab_size, config.emb_dim,
config.hid_dim, config.n_layers, config.dropout)
decoder = Decoder(閩_vocab_size, config.emb_dim, config.hid_dim,
config.n_layers, config.dropout, config.attention)
model = Seq2Seq(encoder, decoder, device)
#print(model)
# 建構 optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=config.learning_rate)
#print(optimizer)
if config.load_model:
model = load_model(model, config.load_model_path)
model = model.to(device)
return model, optimizer