Scanning is a general form of recurrence, which can be used for looping.
The idea is that you *scan* a function along some input sequence, producing
an output at each time-step that can be seen (but not modified) by the
function at the next time-step. (Technically, the function can see the
previous K time-steps of your outputs and L time steps (from the past and
future of the sequence) of your inputs.
So for example, ``sum()`` could be computed by scanning the ``z+x_i``
function over a list, given an initial state of ``z=0``.
Special cases:
- A ``reduce()`` operation can be performed by returning only the last
output of a ``scan``.
- A ``map()`` operation can be performed by applying a function that
ignores each previous output.
Often a for loop can be expressed as a ``scan()`` operation, and ``scan`` is the closest that theano comes to looping. The advantage of using ``scan`` over
for loops is that it allows you to express the loop symbolically. The
Scan Op should always be used by applying the ``scan`` function.
:param fn: Function that describes the operations involved in one step of scan
Given variables representing all the slices of input and
past values of outputs and other non sequences parameters, ``fn`` should
produce variables describing the output of one time step of scan.
The order in which the argument to this function are given is very
important. You should have the following order
* all time slices of the first sequence (as given in the ``sequences`` list) ordered cronologically
* all time slices of the second sequence (as given in the ``sequences`` list) ordered cronologically
* ...
* all time slices of the first output (as given in the ``initial_state`` list) ordered cronologically
* all time slices of the second otuput (as given in the ``initial_state`` list) ordered cronologically
* ...
* all other parameters over which scan doesn't iterate given in the same order as in ``non_sequences``
The outputs of these function should have the same order as in the list ``initial_states``
:param sequences: list of Theano variables over which scan needs to iterate
:param initial_states: list of Theano variables containing the initial state used for the output.
Note that if the function applied recursively uses only the previous value of the output or none, this initial state
should have same shape as one time step of the output; otherwise, the
initial state should have the same number of dimension as output. This
can easily be understand through an example. For computing ``y[t]`` let
assume that we need ``y[t-1]``, ``y[t-2]`` and ``y(t-4)``. Through an abuse of notation, when ``t = 0``, we would need values for ``y[-1]``, ``y[-2]`` and
``y[-4]``. These values are provided by the initial state of ``y``, which
should have same number of dimension as ``y``, where the first dimension should
be large enough to cover all past values, which in this case is 4.
If ``init_y`` is the variable containing the initial state of ``y``, then
``init_y[0]`` corresponds to ``y[-4]``, ``init_y[1]`` corresponds to ``y[-3]``,
``init_y[2]`` corresponds to ``y[-2]``, ``init_y[3]`` corresponds to ``y[-1]``.
By default, scan is set to use the last time step for each output.
:param non_sequences: Parameters over which scan should not iterate.
These parameters are given at each time step to the function applied recursively.