forked from vansky/neural-complexity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
109 lines (95 loc) · 4.46 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
""" Model data structures """
import numpy as np
import torch.nn as nn
import torch
class RNNModel(nn.Module):
"""Container module with an encoder, a recurrent module, and a decoder."""
def __init__(self, rnn_type, ntoken, ninp, nhid, nlayers,
embedding_file=None, dropout=0.5, tie_weights=False, freeze_embedding=False):
super(RNNModel, self).__init__()
self.drop = nn.Dropout(dropout)
if embedding_file:
# Use pre-trained embeddings
embed_weights = self.load_embeddings(embedding_file, ntoken, ninp)
self.encoder = nn.Embedding.from_pretrained(embed_weights)
else:
self.encoder = nn.Embedding(ntoken, ninp)
if rnn_type in ['LSTM', 'GRU']:
self.rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
else:
try:
nonlinearity = {'RNN_TANH': 'tanh', 'RNN_RELU': 'relu'}[rnn_type]
except KeyError:
raise ValueError("""An invalid option for `--model` was supplied,
options are ['LSTM', 'GRU', 'RNN_TANH' or 'RNN_RELU']""")
self.rnn = nn.RNN(ninp, nhid, nlayers, nonlinearity=nonlinearity, dropout=dropout)
self.decoder = nn.Linear(nhid, ntoken)
self.init_weights(freeze_embedding)
if freeze_embedding:
for param in self.encoder.parameters():
param.requires_grad = False
# Optionally tie weights as in:
# "Using the Output Embedding to Improve Language Models" (Press & Wolf 2017)
# https://arxiv.org/abs/1608.05859
# and
# "Tying Word Vectors and Word Classifiers:
# A Loss Framework for Language Modeling" (Inan et al. 2017)
# https://arxiv.org/abs/1611.01462
if tie_weights:
if nhid != ninp:
raise ValueError('When using the tied flag, nhid must be equal to emsize')
self.decoder.weight = self.encoder.weight
self.rnn_type = rnn_type
self.nhid = nhid
self.nlayers = nlayers
def init_weights(self, freeze_embedding):
""" Initialize encoder and decoder weights """
initrange = 0.1
if not freeze_embedding:
self.encoder.weight.data.uniform_(-initrange, initrange)
self.decoder.bias.data.fill_(0)
self.decoder.weight.data.uniform_(-initrange, initrange)
def zero_parameters(self):
""" Set all parameters to zero (likely as a baseline) """
self.encoder.weight.data.fill_(0)
self.decoder.bias.data.fill_(0)
self.decoder.weight.data.fill_(0)
for weight in self.rnn.parameters():
weight.data.fill_(0)
def random_parameters(self):
""" Randomly initialize all RNN parameters but not the encoder or decoder """
initrange = 0.1
for weight in self.rnn.parameters():
weight.data.uniform_(-initrange, initrange)
def load_embeddings(self, embedding_file, ntoken, ninp):
""" Load pre-trained embedding weights """
weights = np.empty((ntoken, ninp))
with open(embedding_file, 'r') as in_file:
ctr = 0
for line in in_file:
weights[ctr, :] = np.array([float(w) for w in line.strip().split()[1:]])
ctr += 1
return(torch.tensor(weights).float())
def forward(self, observation, hidden):
emb = self.drop(self.encoder(observation))
output, hidden = self.rnn(emb, hidden)
output = self.drop(output)
decoded = self.decoder(output.view(output.size(0)*output.size(1), output.size(2)))
return decoded.view(output.size(0), output.size(1), decoded.size(1)), hidden
def init_hidden(self, bsz):
""" Initialize a fresh hidden state """
weight = next(self.parameters()).data
if self.rnn_type == 'LSTM':
return (torch.tensor(weight.new(self.nlayers, bsz, self.nhid).zero_()),
torch.tensor(weight.new(self.nlayers, bsz, self.nhid).zero_()))
else:
return torch.tensor(weight.new(self.nlayers, bsz, self.nhid).zero_())
def set_parameters(self,init_val):
for weight in self.rnn.parameters():
weight.data.fill_(init_val)
self.encoder.weight.data.fill_(init_val)
self.decoder.weight.data.fill_(init_val)
def randomize_parameters(self):
initrange = 0.1
for weight in self.rnn.parameters():
weight.data.uniform_(-initrange, initrange)