-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
113 lines (92 loc) · 4.2 KB
/
models.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
import torch.nn as nn
import torch
class MainRNNModel(nn.Module):
"""Container module with an encoder, and a recurrent module for main classification network."""
def __init__(self, ntoken, ninp, nout, nhid, nhid_ffn, nlayers, dropconnect=0.5):
super(MainRNNModel, self).__init__()
self.encoder = nn.Embedding(ntoken, ninp)
self.linear = nn.Linear(1, ninp)
self.rnn = nn.LSTM(ninp, nhid, nlayers)
self.pre_decoder = nn.Linear(nhid, nhid_ffn)
self.decoder = nn.Linear(nhid_ffn, nout)
self.init_weights()
self.ninp = ninp
self.nhid = nhid
self.nlayers = nlayers
self.dropconnect = dropconnect
def init_weights(self):
initrange = 0.1
self.encoder.weight.data.uniform_(-initrange, initrange)
self.pre_decoder.weight.data.uniform_(-initrange, initrange)
self.pre_decoder.bias.data.zero_()
self.decoder.weight.data.uniform_(-initrange, initrange)
self.decoder.bias.data.zero_()
def forward(self, input, hidden):
if self.ninp == 1:
emb = input.float().unsqueeze(-1)
else:
emb = self.encoder(input)
_, hidden = self.rnn(emb, hidden)
return hidden
def init_hidden(self, bsz, hidden=None):
weight = next(self.parameters())
new_h = weight.new_zeros(self.nlayers, bsz, self.nhid)
new_c = weight.new_zeros(self.nlayers, bsz, self.nhid)
if hidden:
h, c = hidden
new_h[0], new_c[0] = h[-1], c[-1]
return (new_h, new_c)
def out(self, hidden):
h, _ = hidden
h = h[-1] # hidden state of last layer
pre_decoded = self.pre_decoder(h)
if self.training == True:
mask = self.decoder.weight.data.new().resize_(self.decoder.weight.size()).bernoulli_(1- self.dropconnect) / (1 - self.dropconnect)
weight = mask * self.decoder.weight
else:
weight = self.decoder.weight
decoded = torch.mm(pre_decoded, weight.t()) + self.decoder.bias.data
return decoded
class AuxRNNModel(nn.Module):
"""Container module with an encoder, a recurrent module, and a decoder for auxiliary network."""
def __init__(self, ntoken, ninp, nout, nhid, nhid_ffn, nlayers, dropconnect=0.5):
super(AuxRNNModel, self).__init__()
self.linear = nn.Linear(1, ninp)
self.encoder = nn.Embedding(ntoken, ninp)
self.rnn = nn.LSTM(ninp, nhid, nlayers)
self.pre_decoder = nn.Linear(nhid, nhid_ffn)
self.decoder = nn.Linear(nhid_ffn, nout)
self.init_weights()
self.ninp = ninp
self.nhid = nhid
self.nlayers = nlayers
self.dropconnect = dropconnect
def init_weights(self):
initrange = 0.1
self.encoder.weight.data.uniform_(-initrange, initrange)
self.pre_decoder.weight.data.uniform_(-initrange, initrange)
self.pre_decoder.bias.data.zero_()
self.decoder.weight.data.uniform_(-initrange, initrange)
self.decoder.bias.data.zero_()
def forward(self, input, hidden):
if self.ninp == 1:
emb = input.float().unsqueeze(-1)
else:
emb = self.encoder(input)
output, hidden = self.rnn(emb, hidden)
pre_decoded = self.pre_decoder(output.view(output.size(0)*output.size(1), output.size(2)))
if self.training:
mask = self.decoder.weight.data.new().resize_(self.decoder.weight.size()).bernoulli_(1- self.dropconnect) / (1 - self.dropconnect)
weight = mask * self.decoder.weight
else:
weight = self.decoder.weight
decoded = torch.mm(pre_decoded, weight.t()) + self.decoder.bias.data
return decoded.view(output.size(0), output.size(1), decoded.size(1)), hidden
def init_hidden(self, bsz, hidden=None):
weight = next(self.parameters())
new_h = weight.new_zeros(self.nlayers, bsz, self.nhid)
new_c = weight.new_zeros(self.nlayers, bsz, self.nhid)
if hidden:
h, c = hidden
new_h[0], new_c[0] = h[-1], c[-1]
return (new_h, new_c)