MCPcopy Index your code
hub / github.com/lisa-lab/DeepLearningTutorials / lstm_layer

Function lstm_layer

code/lstm.py:160–205  ·  view source on GitHub ↗
(tparams, state_below, options, prefix='lstm', mask=None)

Source from the content-addressed store, hash-verified

158
159
160def lstm_layer(tparams, state_below, options, prefix='lstm', mask=None):
161 nsteps = state_below.shape[0]
162 if state_below.ndim == 3:
163 n_samples = state_below.shape[1]
164 else:
165 n_samples = 1
166
167 assert mask is not None
168
169 def _slice(_x, n, dim):
170 if _x.ndim == 3:
171 return _x[:, :, n * dim:(n + 1) * dim]
172 return _x[:, n * dim:(n + 1) * dim]
173
174 def _step(m_, x_, h_, c_):
175 preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
176 preact += x_
177
178 i = tensor.nnet.sigmoid(_slice(preact, 0, options['dim_proj']))
179 f = tensor.nnet.sigmoid(_slice(preact, 1, options['dim_proj']))
180 o = tensor.nnet.sigmoid(_slice(preact, 2, options['dim_proj']))
181 c = tensor.tanh(_slice(preact, 3, options['dim_proj']))
182
183 c = f * c_ + i * c
184 c = m_[:, None] * c + (1. - m_)[:, None] * c_
185
186 h = o * tensor.tanh(c)
187 h = m_[:, None] * h + (1. - m_)[:, None] * h_
188
189 return h, c
190
191 state_below = (tensor.dot(state_below, tparams[_p(prefix, 'W')]) +
192 tparams[_p(prefix, 'b')])
193
194 dim_proj = options['dim_proj']
195 rval, updates = theano.scan(_step,
196 sequences=[mask, state_below],
197 outputs_info=[tensor.alloc(numpy_floatX(0.),
198 n_samples,
199 dim_proj),
200 tensor.alloc(numpy_floatX(0.),
201 n_samples,
202 dim_proj)],
203 name=_p(prefix, '_layers'),
204 n_steps=nsteps)
205 return rval[0]
206
207
208# ff: Feed Forward (normal neural net), only useful to put after lstm

Callers

nothing calls this directly

Calls 2

_pFunction · 0.85
numpy_floatXFunction · 0.85

Tested by

no test coverage detected