-
Notifications
You must be signed in to change notification settings - Fork 9
/
model.py
142 lines (110 loc) · 4.93 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
"""Hierarchical RNN implementation"""
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import torch.cuda
from torch.autograd import Variable
# TODO
# Init. GRU with orthogonal initializer.
class EncoderRNN(nn.Module):
"""Encoder RNN Building"""
def __init__(self, input_size, hidden_size, n_layers, dropout):
super(EncoderRNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.n_layers = n_layers
self.embedding = nn.Embedding(input_size, hidden_size)
self.gru = nn.GRU(hidden_size, hidden_size, n_layers, dropout=dropout)
self.is_cuda = torch.cuda.is_available()
self.init_weight()
def init_hidden(self):
hidden = Variable(torch.zeros(self.n_layers, 1, self.hidden_size))
if self.is_cuda:
hidden = hidden.cuda()
return hidden
def init_weight(self):
initrange = 0.1
self.embedding.weight.data.uniform_(-initrange, initrange)
def forward(self, input, hidden):
embedded = self.embedding(input).view(1, 1, -1)
output, hidden = self.gru(embedded, hidden)
return output, hidden
class ContextRNN(nn.Module):
"""Context RNN Building"""
def __init__(self, encoder_hidden_size, hidden_size, n_layers, dropout):
super(ContextRNN, self).__init__()
self.encoder_hidden_size = encoder_hidden_size
self.hidden_size = hidden_size
self.n_layers = n_layers
self.gru = nn.GRU(encoder_hidden_size, hidden_size, n_layers, dropout=dropout)
self.is_cuda = torch.cuda.is_available()
def init_hidden(self):
hidden = Variable(torch.zeros(self.n_layers, 1, self.hidden_size))
if self.is_cuda:
hidden = hidden.cuda()
return hidden
def forward(self, input, hidden):
input = input.view(1, 1, -1)
output, hidden = self.gru(input, hidden)
return output, hidden
class DecoderRNN(nn.Module):
"""Decoder RNN Building"""
def __init__(self, context_output_size, hidden_size, output_size, n_layers, dropout):
super(DecoderRNN, self).__init__()
self.context_output_size = context_output_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.embedding = nn.Embedding(output_size, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
self.gru = nn.GRU(context_output_size + hidden_size, hidden_size, n_layers, dropout=dropout)
self.is_cuda = torch.cuda.is_available()
self.init_weight()
def init_hidden(self):
hidden = Variable(torch.zeros(self.n_layers, 1, self.hidden_size))
if self.is_cuda:
hidden = hidden.cuda()
return hidden
def init_weight(self):
initrange = 0.1
self.out.weight.data.uniform_(-initrange, initrange)
self.embedding.weight.data.uniform_(-initrange, initrange)
def forward(self, context_output, input, hidden):
context_output = context_output.view(1, 1, -1)
input_cat = torch.cat([context_output, self.embedding(input)], 2)
output, hidden = self.gru(input_cat, hidden)
output = F.log_softmax(self.out(output[0]))
return output, hidden
class DecoderRNNSeq(nn.Module):
"""Seq2seq's Attention Decoder RNN Building"""
def __init__(self, hidden_size, output_size, n_layers, dropout, max_length):
super(DecoderRNNSeq, self).__init__()
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.max_length = max_length
self.embedding = nn.Embedding(output_size, hidden_size)
self.attn = nn.Linear(hidden_size * 2, max_length)
self.attn_combine = nn.Linear(hidden_size * 2, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
self.gru = nn.GRU(hidden_size, hidden_size, n_layers, dropout=dropout)
self.is_cuda = torch.cuda.is_available()
self.init_weight()
def init_hidden(self):
hidden = Variable(torch.zeros(self.n_layers, 1, self.hidden_size))
if self.is_cuda:
hidden = hidden.cuda()
return hidden
def init_weight(self):
initrange = 0.1
self.out.weight.data.uniform_(-initrange, initrange)
self.embedding.weight.data.uniform_(-initrange, initrange)
def forward(self, input, hidden, encoder_outputs):
embedded = self.embedding(input).view(1, 1, -1)
attn_weights = F.softmax(self.attn(torch.cat((embedded[0], hidden[0]), 1)))
attn_applied = torch.bmm(attn_weights.unsqueeze(0), encoder_outputs.unsqueeze(0))
output = torch.cat((embedded[0], attn_applied[0]), 1)
output = self.attn_combine(output).unsqueeze(0)
output, hidden = self.gru(embedded, hidden)
output = F.log_softmax(self.out(output[0]))
return output, hidden, attn_weights