forked from glample/tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.py
282 lines (248 loc) · 9.9 KB
/
nn.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
277
278
279
280
281
282
import theano
import theano.tensor as T
from utils import shared
class HiddenLayer(object):
"""
Hidden layer with or without bias.
Input: tensor of dimension (dims*, input_dim)
Output: tensor of dimension (dims*, output_dim)
"""
def __init__(self, input_dim, output_dim, bias=True, activation='sigmoid',
name='hidden_layer'):
self.input_dim = input_dim
self.output_dim = output_dim
self.bias = bias
self.name = name
if activation is None:
self.activation = None
elif activation == 'tanh':
self.activation = T.tanh
elif activation == 'sigmoid':
self.activation = T.nnet.sigmoid
elif activation == 'softmax':
self.activation = T.nnet.softmax
else:
raise Exception("Unknown activation function: " % activation)
# Initialize weights and bias
self.weights = shared((input_dim, output_dim), name + '__weights')
self.bias = shared((output_dim,), name + '__bias')
# Define parameters
if self.bias:
self.params = [self.weights, self.bias]
else:
self.params = [self.weights]
def link(self, input):
"""
The input has to be a tensor with the right
most dimension equal to input_dim.
"""
self.input = input
self.linear_output = T.dot(self.input, self.weights)
if self.bias:
self.linear_output = self.linear_output + self.bias
if self.activation is None:
self.output = self.linear_output
else:
self.output = self.activation(self.linear_output)
return self.output
class EmbeddingLayer(object):
"""
Embedding layer: word embeddings representations
Input: tensor of dimension (dim*) with values in range(0, input_dim)
Output: tensor of dimension (dim*, output_dim)
"""
def __init__(self, input_dim, output_dim, name='embedding_layer'):
"""
Typically, input_dim is the vocabulary size,
and output_dim the embedding dimension.
"""
self.input_dim = input_dim
self.output_dim = output_dim
self.name = name
# Randomly generate weights
self.embeddings = shared((input_dim, output_dim),
self.name + '__embeddings')
# Define parameters
self.params = [self.embeddings]
def link(self, input):
"""
Return the embeddings of the given indexes.
Input: tensor of shape (dim*)
Output: tensor of shape (dim*, output_dim)
"""
self.input = input
self.output = self.embeddings[self.input]
return self.output
class DropoutLayer(object):
"""
Dropout layer. Randomly set to 0 values of the input
with probability p.
"""
def __init__(self, p=0.5, name='dropout_layer'):
"""
p has to be between 0 and 1 (1 excluded).
p is the probability of dropping out a unit, so
setting p to 0 is equivalent to have an identity layer.
"""
assert 0. <= p < 1.
self.p = p
self.rng = T.shared_randomstreams.RandomStreams(seed=123456)
self.name = name
def link(self, input):
"""
Dropout link: we just apply mask to the input.
"""
if self.p > 0:
mask = self.rng.binomial(n=1, p=1-self.p, size=input.shape,
dtype=theano.config.floatX)
self.output = input * mask
else:
self.output = input
return self.output
class LSTM(object):
"""
Long short-term memory (LSTM). Can be used with or without batches.
Without batches:
Input: matrix of dimension (sequence_length, input_dim)
Output: vector of dimension (output_dim)
With batches:
Input: tensor3 of dimension (batch_size, sequence_length, input_dim)
Output: matrix of dimension (batch_size, output_dim)
"""
def __init__(self, input_dim, hidden_dim, with_batch=True, name='LSTM'):
"""
Initialize neural network.
"""
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.with_batch = with_batch
self.name = name
# Input gate weights
self.w_xi = shared((input_dim, hidden_dim), name + '__w_xi')
self.w_hi = shared((hidden_dim, hidden_dim), name + '__w_hi')
self.w_ci = shared((hidden_dim, hidden_dim), name + '__w_ci')
# Forget gate weights
# self.w_xf = shared((input_dim, hidden_dim), name + '__w_xf')
# self.w_hf = shared((hidden_dim, hidden_dim), name + '__w_hf')
# self.w_cf = shared((hidden_dim, hidden_dim), name + '__w_cf')
# Output gate weights
self.w_xo = shared((input_dim, hidden_dim), name + '__w_xo')
self.w_ho = shared((hidden_dim, hidden_dim), name + '__w_ho')
self.w_co = shared((hidden_dim, hidden_dim), name + '__w_co')
# Cell weights
self.w_xc = shared((input_dim, hidden_dim), name + '__w_xc')
self.w_hc = shared((hidden_dim, hidden_dim), name + '__w_hc')
# Initialize the bias vectors, c_0 and h_0 to zero vectors
self.b_i = shared((hidden_dim,), name + '__b_i')
# self.b_f = shared((hidden_dim,), name + '__b_f')
self.b_c = shared((hidden_dim,), name + '__b_c')
self.b_o = shared((hidden_dim,), name + '__b_o')
self.c_0 = shared((hidden_dim,), name + '__c_0')
self.h_0 = shared((hidden_dim,), name + '__h_0')
# Define parameters
self.params = [self.w_xi, self.w_hi, self.w_ci,
# self.w_xf, self.w_hf, self.w_cf,
self.w_xo, self.w_ho, self.w_co,
self.w_xc, self.w_hc,
self.b_i, self.b_c, self.b_o, # self.b_f,
self.c_0, self.h_0]
def link(self, input):
"""
Propagate the input through the network and return the last hidden
vector. The whole sequence is also accessible via self.h, but
where self.h of shape (sequence_length, batch_size, output_dim)
"""
def recurrence(x_t, c_tm1, h_tm1):
i_t = T.nnet.sigmoid(T.dot(x_t, self.w_xi) +
T.dot(h_tm1, self.w_hi) +
T.dot(c_tm1, self.w_ci) +
self.b_i)
# f_t = T.nnet.sigmoid(T.dot(x_t, self.w_xf) +
# T.dot(h_tm1, self.w_hf) +
# T.dot(c_tm1, self.w_cf) +
# self.b_f)
c_t = ((1 - i_t) * c_tm1 + i_t * T.tanh(T.dot(x_t, self.w_xc) +
T.dot(h_tm1, self.w_hc) + self.b_c))
o_t = T.nnet.sigmoid(T.dot(x_t, self.w_xo) +
T.dot(h_tm1, self.w_ho) +
T.dot(c_t, self.w_co) +
self.b_o)
h_t = o_t * T.tanh(c_t)
return [c_t, h_t]
# If we use batches, we have to permute the first and second dimension.
if self.with_batch:
self.input = input.dimshuffle(1, 0, 2)
outputs_info = [T.alloc(x, self.input.shape[1], self.hidden_dim)
for x in [self.c_0, self.h_0]]
else:
self.input = input
outputs_info = [self.c_0, self.h_0]
[_, h], _ = theano.scan(
fn=recurrence,
sequences=self.input,
outputs_info=outputs_info,
n_steps=self.input.shape[0]
)
self.h = h
self.output = h[-1]
return self.output
def log_sum_exp(x, axis=None):
"""
Sum probabilities in the log-space.
"""
xmax = x.max(axis=axis, keepdims=True)
xmax_ = x.max(axis=axis)
return xmax_ + T.log(T.exp(x - xmax).sum(axis=axis))
def forward(observations, transitions, viterbi=False,
return_alpha=False, return_best_sequence=False):
"""
Takes as input:
- observations, sequence of shape (n_steps, n_classes)
- transitions, sequence of shape (n_classes, n_classes)
Probabilities must be given in the log space.
Compute alpha, matrix of size (n_steps, n_classes), such that
alpha[i, j] represents one of these 2 values:
- the probability that the real path at node i ends in j
- the maximum probability of a path finishing in j at node i (Viterbi)
Returns one of these 2 values:
- alpha
- the final probability, which can be:
- the sum of the probabilities of all paths
- the probability of the best path (Viterbi)
"""
assert not return_best_sequence or (viterbi and not return_alpha)
def recurrence(obs, previous, transitions):
previous = previous.dimshuffle(0, 'x')
obs = obs.dimshuffle('x', 0)
if viterbi:
scores = previous + obs + transitions
out = scores.max(axis=0)
if return_best_sequence:
out2 = scores.argmax(axis=0)
return out, out2
else:
return out
else:
return log_sum_exp(previous + obs + transitions, axis=0)
initial = observations[0]
alpha, _ = theano.scan(
fn=recurrence,
outputs_info=(initial, None) if return_best_sequence else initial,
sequences=[observations[1:]],
non_sequences=transitions
)
if return_alpha:
return alpha
elif return_best_sequence:
sequence, _ = theano.scan(
fn=lambda beta_i, previous: beta_i[previous],
outputs_info=T.cast(T.argmax(alpha[0][-1]), 'int32'),
sequences=T.cast(alpha[1][::-1], 'int32')
)
sequence = T.concatenate([sequence[::-1], [T.argmax(alpha[0][-1])]])
return sequence
else:
if viterbi:
return alpha[-1].max(axis=0)
else:
return log_sum_exp(alpha[-1], axis=0)