The scan operation is meant to be able to describe symbolically loops, recurrent relations or dynamical systems. In general, we will say that the scan op implements system of equations of the following form:
The equations describe a system evolving in time, where represents the
current step. The system is described by inputs, states, outputs and
parameteres.
The inputs, denoted by are time-varying quantities,
hence indexed by
. They however only influence the system, but are
not influenced by the system.
The states are time-varying quantities, whose value at
time
depends on its (or other state) previous values as well as
the inputs and parameters. Note that the first few values of the states are
always provided, otherwise we could not imploy the recurrent equation to
generate these sequence of values without a starting point.
The outputs, are outputs of the system, i.e. values that
depend on the previous values of the states and inputs. The difference
between outputs and states is that outputs do not feed back into the system.
The parameters are fixed quantities that are re-used at
every time step of the evolution of the system.
Each of the equations above are implemented by the inner function of scan. You can think of the inner function as a theano function that gets executed at each step to get the new values. This inner function should not be confused with the constructive function, which is what the user gives to the scan function. The constructive function is used to construct the computational graph that is afterwards compiled into the inner function.
The implementation of scan is spread over several files. The different files, and section of the code they deal with, are :
First the scan arguments are parsed by the function canonical_arguments, that wraps them into lists and adds default values for the arguments. One important step that happens in this function is that the inputs arguments are converted such that they all have a single tap, namely 0. For example if you have [{'input':u, 'taps':[0, 4]}] as the list of inputs arguments to scan, it gets converted into [{'input':u, 'taps':[0]}, {'input':u[4:], 'taps':[0]}].
The second step is to check if n_steps is a constant and has the value 1 or -1. If that is true then the function one_step_scan is called which unwraps the computation of the inner function into the outer graph without adding any scan op in the graph.