A helper class to construct a recurrent neural net.
| 273 | # d_acc_state[t] += d_state1 |
| 274 | # Pack structures and return. |
| 275 | class _Recurrent(object): |
| 276 | """A helper class to construct a recurrent neural net.""" |
| 277 | |
| 278 | def __init__(self, |
| 279 | cell_fn, |
| 280 | cell_grad, |
| 281 | theta, |
| 282 | state0, |
| 283 | inputs, |
| 284 | max_input_length, |
| 285 | extras, |
| 286 | use_tpu, |
| 287 | aligned_end=False): |
| 288 | """RNN helper class. |
| 289 | |
| 290 | Args: |
| 291 | cell_fn: A python function, which computes: |
| 292 | state1, extras = cell_fn(theta, state0, inputs[t, :]) |
| 293 | cell_grad: A python function which computes: |
| 294 | dtheta, dstate0, dinputs[t, :] = cell_grad( |
| 295 | theta, state0, inputs[t, :], extras, dstate1) |
| 296 | theta: weights. A structure of tensors. |
| 297 | state0: initial state. A structure of tensors. |
| 298 | inputs: inputs. A structure of tensors. |
| 299 | max_input_length: None, or the maximum effective length of the input over |
| 300 | all batches. A scalar tensor. |
| 301 | extras: A structure of tensors. The 2nd return value of every |
| 302 | invocation of cell_fn is a structure of tensors with matching keys |
| 303 | and shapes of this `extras`. |
| 304 | use_tpu: A boolean indicating whether the computation is mean to |
| 305 | run on a TPU. |
| 306 | aligned_end: A boolean indicating whether the sequence is aligned at |
| 307 | the end. |
| 308 | """ |
| 309 | self._theta = theta |
| 310 | self._state = state0 |
| 311 | self._inputs = inputs |
| 312 | self._max_input_length = self._MaybeComputeMaxInputLength( |
| 313 | inputs, max_input_length) |
| 314 | self._cell_fn = cell_fn |
| 315 | self._cell_grad = cell_grad |
| 316 | self._extras = extras |
| 317 | self._aligned_end = aligned_end |
| 318 | |
| 319 | # pylint: disable=unbalanced-tuple-unpacking |
| 320 | |
| 321 | # NOTE: TF Function (Fwd, Bak, ForwardLoopBody, BackwardLoopBody, |
| 322 | # Forward and Backward defined below) simply takes a list of |
| 323 | # Tensors and returns a list of Tensors. When we pass in a |
| 324 | # structure (a list of structures of Tensors), we use _Flatten to |
| 325 | # convert the structure into a list of tensor. Conversely, the |
| 326 | # following code often uses _Pack to formulate a structure from a |
| 327 | # list of tensors based on a "template". |
| 328 | |
| 329 | # Wraps cell_fn in a TF Function: |
| 330 | # state1 = cell_fn(theta, state0, inputs) |
| 331 | fwd_sig = [self._theta, self._state, self._inputs] |
| 332 |