-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
170 lines (132 loc) · 4.8 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class EncoderModule(nn.Module):
def __init__(self, input_size, hidden_size, batch_first = True):
super(EncoderModule, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.batch_first = batch_first
self.embedding = nn.Embedding(input_size, hidden_size)
self.rnn = nn.GRU(hidden_size, hidden_size, batch_first = batch_first)
def forward(self, x, hidden = None):
if hidden is None:
hidden = self.init_hidden(x.shape)
x = self.embedding(x)
x, hidden = self.rnn(x)
return x, hidden
def init_hidden(self, input_shape):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if self.batch_first:
return torch.zeros(input_shape[0], 1, self.hidden_size, device = device)
else:
return torch.zeros(input_shape[1], 1, self.hidden_size, device = device)
class AttnDecoderModule(nn.Module):
def __init__(self,
hidden_size,
output_size,
batch_first = True,
dropout_pb = 0.1,
max_input_length = 50,
max_output_length = 50):
super(AttnDecoderModule, self).__init__()
self.hidden_size = hidden_size
self.output_size = output_size
self.batch_first = True
self.dropout_pb = dropout_pb
self.max_input_length = max_input_length
self.max_output_length = max_output_length
self.embedding = nn.Embedding(output_size, hidden_size)
self.attn_fc = nn.Linear(hidden_size, 1)
self.attn_fc1 = nn.Linear(hidden_size * 2, hidden_size, bias = False)
self.attn_fc2 = nn.Linear(hidden_size, hidden_size, bias = False)
# self.attn = nn.Linear(hidden_size * 2, max_input_length)
self.attn_combine = nn.Linear(hidden_size * 2, hidden_size)
self.dropout = nn.Dropout(dropout_pb)
self.rnn = nn.GRU(hidden_size, hidden_size, batch_first = True)
self.out = nn.Linear(hidden_size, output_size)
self.mode = 'train'
def set_mode(self, mode = 'train'):
self.mode = mode
def set_wenc(self, encoder_out):
self.wenc = self.attn_fc2(encoder_out)
def step(self, x, hidden, encoder_out):
"""
x = decoder output at step (t - 1)
hidden = hidden state of the decoder
encoder_out = output sequence from encoder
"""
x = self.embedding(x)
x = self.dropout(x)
x_h = hidden.permute(1, 0, 2)
wh = self.attn_fc1(torch.cat([x, x_h], 2))
attn_wts = F.softmax(self.attn_fc(wh + self.wenc), dim = 2)
attn_wts = attn_wts.repeat(1, 1, encoder_out.shape[-1])
attn_x = torch.sum(torch.mul(encoder_out, attn_wts), dim = 1, keepdim = True)
# attn_wts = F.softmax(self.attn(torch.cat([x, x_h], 2)), dim = 2)
# attn_x = torch.bmm(attn_wts, encoder_out)
x = torch.cat((x, attn_x), 2)
x = self.attn_combine(x)
x = F.relu(x)
x, hidden = self.rnn(x, hidden)
x = F.log_softmax(self.out(x), dim = 2)
return x, hidden, attn_wts
def forward(self, encoder_out, hidden, y, teacher_forcing = True, ratio = 0.5):
if hidden is None:
hidden = self.init_hidden(encoder_out.shape)
if self.mode in ['train', 'val']:
step_size = self.max_output_length
else:
step_size = 1
self.set_wenc(encoder_out)
x = y[:, 0].unsqueeze(1)
out = []
attn = []
for idx in range(step_size):
if teacher_forcing == True and np.random.rand() < ratio:
x = y[:, idx]
else:
x = x.detach()
x = x.view(-1, 1)
x, hidden, attn_wts = self.step(x, hidden, encoder_out)
x = x.permute(0, 2, 1)
out.append(x)
attn.append(attn_wts)
top_v, top_i = x.squeeze().topk(1)
x = top_i
out = torch.cat(out, dim = 2)
attn = torch.cat(attn, dim = 1)
return out, hidden, attn
def init_hidden(self, input_shape):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if self.batch_first:
return torch.zeros(input_shape[0], 1, self.hidden_size, device = device)
else:
return torch.zeros(input_shape[1], 1, self.hidden_size, device = device)
class Seq2SeqAttnNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size, input_length, output_length):
super(Seq2SeqAttnNet, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.input_length = input_length
self.output_length = output_length
self.encoder = EncoderModule(input_size, hidden_size)
self.decoder = AttnDecoderModule(hidden_size,
output_size,
max_input_length = self.input_length,
max_output_length = self.output_length)
self.decoder_inp = None
self.mode = 'train'
self.teacher_forcing = True
def set_mode(self, mode):
self.mode = mode
if self.mode != 'train':
self.teacher_forcing = False
self.decoder.set_mode(self.mode)
def set_decoder_inp(self, decoder_inp):
self.decoder_inp = decoder_inp
def forward(self, x):
x, hidden = self.encoder(x)
x, _, attn_wts = self.decoder(x, hidden, self.decoder_inp, self.teacher_forcing)
return x, attn_wts