-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
276 lines (239 loc) · 9.62 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
import torch
import torch.nn as nn
import torch.nn.init as I
import torch.nn.utils.rnn as R
import torch.nn.functional as F
import logging
logger = logging.getLogger(__name__)
class Linear(nn.Linear):
"""
Linear Layer.
Add the initialize function.
"""
def __init__(self,
in_features,
out_features,
bias=True):
super(Linear, self).__init__(in_features,
out_features,
bias=bias)
self.initialize()
def initialize(self):
for n, p in self.named_parameters():
if 'weight' in n:
I.orthogonal(p)
elif 'bias' in n:
I.uniform(p, b=.1)
class Embedding(nn.Embedding):
def __init__(self,
num_embeddings,
embedding_dim,
padding_idx=None,
max_norm=None,
norm_type=2,
scale_grad_by_freq=False,
sparse=False,
pretrain=None,
vocab=None,
trainable=False,
stats=True
):
super(Embedding, self).__init__(num_embeddings,
embedding_dim,
padding_idx,
max_norm,
norm_type,
scale_grad_by_freq,
sparse)
self.output_size = embedding_dim
self.num_embeddings = num_embeddings
self.pretrain = pretrain
self.vocab = vocab
self.stats = stats
if not trainable:
self.weight.requires_grad = False
if pretrain and vocab:
self.load(pretrain, vocab, stats=stats)
else:
I.xavier_normal(self.weight.data)
def load(self, path, vocab, stats=False):
"""Load pre-trained embeddings from file.
Only supports text format embedding and
:param path: Path to the embedding file.
:param vocab: Vocab dict.
:param stats: Is the first line stats info.
:return:
"""
logger.info('Loading word embeddings from {}'.format(path))
with open(path, 'r', encoding='utf-8', errors='ignore') as r:
if stats:
r.readline()
for line in r:
line = line.rstrip().split(' ')
token = line[0]
if token in vocab:
vector = self.weight.data.new([float(v) for v in line[1:]])
self.weight.data[vocab[token]] = vector
class LSTM(nn.LSTM):
def __init__(self,
input_size,
hidden_size,
num_layers=1,
bias=True,
batch_first=False,
dropout=0,
bidirectional=False,
forget_bias=0):
super(LSTM, self).__init__(input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
bias=bias,
batch_first=batch_first,
dropout=dropout,
bidirectional=bidirectional)
self.forget_bias = forget_bias
self.output_size = hidden_size * (2 if bidirectional else 1)
self.bidirectional = bidirectional
self.initialize()
def initialize(self):
for n, p in self.named_parameters():
if 'weight' in n:
# I.xavier_normal(p)
I.orthogonal(p)
elif 'bias' in n:
bias_size = p.size(0)
p.data[bias_size // 4:bias_size // 2].fill_(self.forget_bias)
def forward(self, inputs, lens, hx=None):
inputs_packed = R.pack_padded_sequence(inputs, lens.data.tolist(),
batch_first=True)
outputs, h = super(LSTM, self).forward(inputs_packed, hx)
outputs, _ = R.pad_packed_sequence(outputs, batch_first=True)
return outputs, h
class MoralClassifier(nn.Module):
def __init__(self,
word_embedding,
lstm,
linears,
embed_dropout_prob=.5,
lstm_dropout_prob=.5,
gpu=False):
super(MoralClassifier, self).__init__()
self.word_embedding = word_embedding
self.lstm = lstm
self.linears = nn.ModuleList(linears)
self.linear_num = len(linears)
self.embed_dropout = nn.Dropout(p=embed_dropout_prob)
self.lstm_dropout = nn.Dropout(p=lstm_dropout_prob)
self.gpu = gpu
def forward(self, tokens, lens):
# embedding lookup
tokens_embed = self.word_embedding.forward(tokens)
tokens_embed = self.embed_dropout.forward(tokens_embed)
# lstm layer
_lstm_outputs, (last_hidden, _last_cell) = self.lstm.forward(
tokens_embed, lens)
last_hidden = last_hidden.squeeze(0)
last_hidden = self.lstm_dropout.forward(last_hidden)
# linear layers
linear_input = last_hidden
for layer_idx, linear in enumerate(self.linears):
linear_input = linear.forward(linear_input)
# if layer_idx != self.linear_num - 1:
# linear_input = F.dropout(linear_input, p=.2)
return linear_input
class MoralClassifierExt(nn.Module):
def __init__(self,
word_embedding,
lstm,
linears,
ext_linears,
embed_dropout_prob=.5,
lstm_dropout_prob=.5,
el_dropout_prob=.5,
gpu=False):
super(MoralClassifierExt, self).__init__()
self.word_embedding = word_embedding
self.lstm = lstm
self.linears = nn.ModuleList(linears)
self.ext_linears = nn.ModuleList(ext_linears)
self.linear_num = len(linears)
self.ext_linear_num = len(ext_linears)
self.embed_dropout = nn.Dropout(p=embed_dropout_prob)
self.lstm_dropout = nn.Dropout(p=lstm_dropout_prob)
self.el_dropout = nn.Dropout(p=el_dropout_prob)
self.gpu = gpu
def forward(self, tokens, lens, exts):
# TODO: add non-linear functions
# embedding lookup
tokens_embed = self.word_embedding.forward(tokens)
tokens_embed = self.embed_dropout.forward(tokens_embed)
# lstm layer
_lstm_outputs, (last_hidden, _last_cell) = self.lstm.forward(
tokens_embed, lens)
last_hidden = last_hidden.squeeze(0)
last_hidden = self.lstm_dropout.forward(last_hidden)
# ext linear layers
ext_linear_input = exts
for layer_idx, linear in enumerate(self.ext_linears):
ext_linear_input = linear.forward(ext_linear_input)
ext_linear_input = F.relu(ext_linear_input)
ext_linear_input = self.el_dropout.forward(ext_linear_input)
# linear layers
linear_input = torch.cat([last_hidden, ext_linear_input], dim=1)
for layer_idx, linear in enumerate(self.linears):
linear_input = linear.forward(linear_input)
return linear_input
class MoralClassifierMfdBk(nn.Module):
def __init__(self,
word_embedding,
lstm,
linears,
el_linears,
mfd_linears,
embed_dropout_prob=.5,
lstm_dropout_prob=.5,
el_dropout_prob=.5,
mfd_dropout_prob=.5,
gpu=False):
super(MoralClassifierMfdBk, self).__init__()
self.word_embedding = word_embedding
self.lstm = lstm
self.linears = nn.ModuleList(linears)
self.el_linears = nn.ModuleList(el_linears)
self.mfd_linears = nn.ModuleList(mfd_linears)
self.linear_num = len(linears)
self.el_linear_num = len(el_linears)
self.mfd_linear_num = len(mfd_linears)
self.embed_dropout = nn.Dropout(p=embed_dropout_prob)
self.lstm_dropout = nn.Dropout(p=lstm_dropout_prob)
self.el_dropout = nn.Dropout(p=el_dropout_prob)
self.mfd_dropout = nn.Dropout(p=mfd_dropout_prob)
self.gpu = gpu
def forward(self, tokens, lens, els, mfds):
# TODO: add non-linear functions
# embedding lookup
tokens_embed = self.word_embedding.forward(tokens)
tokens_embed = self.embed_dropout.forward(tokens_embed)
# lstm layer
_lstm_outputs, (last_hidden, _last_cell) = self.lstm.forward(
tokens_embed, lens)
last_hidden = last_hidden.squeeze(0)
last_hidden = self.lstm_dropout.forward(last_hidden)
# el linear layers
el_linear_input = els
for layer_idx, linear in enumerate(self.el_linears):
el_linear_input = linear.forward(el_linear_input)
el_linear_input = F.relu(el_linear_input)
el_linear_input = self.el_dropout.forward(el_linear_input)
# mfd linear layers
mfd_linear_input = mfds
for layer_idx, linear in enumerate(self.mfd_linears):
mfd_linear_input = linear.forward(mfd_linear_input)
mfd_linear_input = F.relu(mfd_linear_input)
mfd_linear_input = self.el_dropout.forward(mfd_linear_input)
# linear layers
linear_input = torch.cat(
[last_hidden, el_linear_input, mfd_linear_input], dim=1)
for layer_idx, linear in enumerate(self.linears):
linear_input = linear.forward(linear_input)
return linear_input