Loops#
The module aesara.scan provides the basic functionality needed to do loops
in Aesara.
This module provides the Scan Op.
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 past and future) 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 using only the last output of a
scan.A map operation can be performed by applying a function that ignores previous steps of the outputs.
Often a for-loop or while-loop can be expressed as a scan() operation,
and scan is the closest that aesara comes to looping. The advantages
of using scan over for loops in python (among others) are:
it allows the number of iterations to be part of the symbolic graph
it allows computing gradients through the for loop
there exist a bunch of optimizations that help re-write your loop such that less memory is used and that it runs faster
The Scan Op should typically be used by calling any of the following
functions: scan(), map(), reduce(), foldl(),
foldr().
aesara.scan#
- aesara.scan(fn, sequences=None, outputs_info=None, non_sequences=None, n_steps=None, truncate_gradient=-1, go_backwards=False, mode=None, name=None, profile=False, allow_gc=None, strict=False, return_list=False)[source]
This function constructs and applies a
ScanOpto the provided arguments.- Parameters:
fn –
fnis a function that describes the operations involved in one step ofscan.fnshould construct variables describing the output of one iteration step. It should expect as inputVariables representing all the slices of the input sequences and previous values of the outputs, as well as all other arguments given to scan asnon_sequences. The order in which scan passes these variables tofnis the following :all time slices of the first sequence
all time slices of the second sequence
…
all time slices of the last sequence
all past slices of the first output
all past slices of the second output
…
all past slices of the last output
- all other arguments (the list given as
non_sequencesto scan)
- all other arguments (the list given as
The order of the sequences is the same as the one in the list
sequencesgiven toscan. The order of the outputs is the same as the order ofoutputs_info. For any sequence or output the order of the time slices is the same as the one in which they have been given as taps. For example if one writes the following :scan(fn, sequences = [ dict(input= Sequence1, taps = [-3,2,-1]) , Sequence2 , dict(input = Sequence3, taps = 3) ] , outputs_info = [ dict(initial = Output1, taps = [-3,-5]) , dict(initial = Output2, taps = None) , Output3 ] , non_sequences = [ Argument1, Argument2])
fnshould expect the following arguments in this given order:sequence1[t-3]sequence1[t+2]sequence1[t-1]sequence2[t]sequence3[t+3]output1[t-3]output1[t-5]output3[t-1]argument1argument2
The list of
non_sequencescan also contain shared variables used in the function, thoughscanis able to figure those out on its own so they can be skipped. For the clarity of the code we recommend though to provide them toscan. To some extendscancan also figure out othernon sequences(not shared) even if not passed toscan(but used byfn). A simple example of this would be :import aesara.tensor as at W = at.matrix() W_2 = W**2 def f(x): return at.dot(x,W_2)
The function
fnis expected to return two things. One is a list of outputs ordered in the same order asoutputs_info, with the difference that there should be only one output variable per output initial state (even if no tap value is used). Secondlyfnshould return an update dictionary (that tells how to update any shared variable after each iteration step). The dictionary can optionally be given as a list of tuples. There is no constraint on the order of these two list,fncan return either(outputs_list, update_dictionary)or(update_dictionary, outputs_list)or just one of the two (in case the other is empty).To use
scanas awhileloop, the user needs to change the functionfnsuch that also a stopping condition is returned. To do so, one needs to wrap the condition in anuntilclass. The condition should be returned as a third element, for example:... return [y1_t, y2_t], {x:x+1}, until(x < 50)
Note that a number of steps–considered in here as the maximum number of steps–is still required even though a condition is passed. It is used to allocate memory if needed.
sequences –
sequencesis the list ofVariables ordicts describing the sequencesscanhas to iterate over. If a sequence is given as wrapped in adict, then a set of optional information can be provided about the sequence. Thedictshould have the following keys:input(mandatory) –Variablerepresenting the sequence.taps– Temporal taps of the sequence required byfn. They are provided as a list of integers, where a valuekimpiles that at iteration steptscan will pass tofnthe slicet+k. Default value is[0]
All
Variables in the listsequencesare automatically wrapped into adictwheretapsis set to[0]outputs_info –
outputs_infois the list ofVariables ordicts describing the initial state of the outputs computed recurrently. When the initial states are given asdicts, optional information can be provided about the output corresponding to those initial states. Thedictshould have the following keys:initial– AVariablethat represents the initial state of a given output. In case the output is not computed recursively (e.g. amap-like function) and does not require an initial state, this field can be skipped. Given that only the previous time step of the output is used byfn, the initial state should have the same shape as the output and should not involve a downcast of the data type of the output. If multiple time taps are used, the initial state should have one extra dimension that covers all the possible taps. For example if we use-5,-2and-1as past taps, at step0,fnwill require (by an abuse of notation)output[-5],output[-2]andoutput[-1]. This will be given by the initial state, which in this case should have the shape(5,) + output.shape. If thisVariablecontaining the initial state is calledinit_ytheninit_y[0]corresponds tooutput[-5].init_y[1]corresponds tooutput[-4],init_y[2]corresponds tooutput[-3],init_y[3]corresponds tooutput[-2],init_y[4]corresponds tooutput[-1]. While this order might seem strange, it comes natural from splitting an array at a given point. assume that we have a arrayx, and we choosekto be time step0. Then our initial state would bex[:k], while the output will bex[k:]. Looking at this split, elements inx[:k]are ordered exactly like those ininit_y.taps– Temporal taps of the output that will be passed tofn. They are provided as a list of negative integers, where a valuekimplies that at iteration steptscan will pass tofnthe slicet+k.
scanwill follow this logic if partial information is given:If an output is not wrapped in a
dict,scanwill wrap it in one assuming that you use only the last step of the output (i.e. it makes your tap value list equal to[-1]).If you wrap an output in a
dictand you do not provide any taps but you provide an initial state it will assume that you are using only a tap value of-1.If you wrap an output in a
dictbut you do not provide any initial state, it assumes that you are not using any form of taps.If you provide a
Noneinstead of aVariableor a emptydictscanassumes that you will not use any taps for this output (like for example in case of amap)
If
outputs_infois an emptylistorNone,scanassumes that no tap is used for any of the outputs. If information is provided just for a subset of the outputs, an exception is raised, because there is no convention on how scan should map the provided information to the outputs offn.non_sequences –
non_sequencesis the list of arguments that are passed tofnat each steps. One can choose to exclude variables used infnfrom this list, as long as they are part of the computational graph, although–for clarity–this is not encouraged.n_steps –
n_stepsis the number of steps to iterate given as anintor a scalarVariable. If any of the input sequences do not have enough elements,scanwill raise an error. If the value is0, the outputs will have0rows. Ifn_stepsis not provided,scanwill figure out the amount of steps it should run given its input sequences.n_steps < 0is not supported anymore.truncate_gradient –
truncate_gradientis the number of steps to use in truncated back-propagation through time (BPTT). If you compute gradients through aScanOp, they are computed using BPTT. By providing a different value then-1, you choose to use truncated BPTT instead of classical BPTT, where you go for onlytruncate_gradientnumber of steps back in time.go_backwards –
go_backwardsis a flag indicating ifscanshould go backwards through the sequences. If you think of each sequence as indexed by time, making this flagTruewould mean thatscangoes back in time, namely that for any sequence it starts from the end and goes towards0.name – When profiling
scan, it is helpful to provide a name for any instance ofscan. For example, the profiler will produce an overall profile of your code as well as profiles for the computation of one step of each instance ofScan. Thenameof the instance appears in those profiles and can greatly help to disambiguate information.mode – The mode used to compile the inner-graph. If you prefer the computations of one step of
scanto be done differently then the entire function, you can use this parameter to describe how the computations in this loop are done (seeaesara.functionfor details about possible values and their meaning).profile – If
Trueor a non-empty string, a profile object will be created and attached to the inner graph ofScan. WhenprofileisTrue, the profiler results will use the name of theScaninstance, otherwise it will use the passed string. The profiler only collects and prints information when running the inner graph with theCVMLinker.allow_gc –
Set the value of
allow_gcfor the internal graph of theScan. If set toNone, this will use the value ofaesara.config.scan__allow_gc.The full
Scanbehavior related to allocation is determined by this value and the flagaesara.config.allow_gc. If the flagallow_gcisTrue(default) and thisallow_gcisFalse(default), then we letScanallocate all intermediate memory on the first iteration, and they are not garbage collected after that first iteration; this is determined byallow_gc. This can speed up allocation of the subsequent iterations. All those temporary allocations are freed at the end of all iterations; this is what the flagaesara.config.allow_gcmeans.strict – If
True, all the shared variables used infnmust be provided as a part ofnon_sequencesorsequences.return_list – If
True, will always return alist, even if there is only one output.
- Returns:
tupleof the form(outputs, updates).outputsis either aVariableor alistofVariables representing the outputs in the same order as inoutputs_info.updatesis a subclass ofdictspecifying the update rules for all shared variables used inScan. Thisdictshould be passed toaesara.functionwhen you compile your function.- Return type:
tuple
Other ways to create loops#
aesara.scan() comes with bells and whistles that are not always all necessary, which is why Aesara provides several other functions to create a Scan operator:
- aesara.map(fn, sequences, non_sequences=None, truncate_gradient=-1, go_backwards=False, mode=None, name=None)[source]#
Construct a
ScanOpthat functions likemap.- Parameters:
fn – The function that
mapapplies at each iteration step (seescanfor more info).sequences – List of sequences over which
mapiterates (seescanfor more info).non_sequences – List of arguments passed to
fn.mapwill not iterate over these arguments (seescanfor more info).truncate_gradient – See
scan.go_backwards (bool) – Decides the direction of iteration. True means that sequences are parsed from the end towards the beginning, while False is the other way around.
mode – See
scan.name – See
scan.
- aesara.reduce(fn, sequences, outputs_info, non_sequences=None, go_backwards=False, mode=None, name=None)[source]#
Construct a
ScanOpthat functions likereduce.- Parameters:
fn – The function that
reduceapplies at each iteration step (seescanfor more info).sequences – List of sequences over which
reduceiterates (seescanfor more info).outputs_info – List of dictionaries describing the outputs of reduce (see
scanfor more info).non_sequences –
- List of arguments passed to
fn.reducewill not iterate over these arguments (see
scanfor more info).
- List of arguments passed to
go_backwards (bool) – Decides the direction of iteration. True means that sequences are parsed from the end towards the beginning, while False is the other way around.
mode – See
scan.name – See
scan.
- aesara.foldl(fn, sequences, outputs_info, non_sequences=None, mode=None, name=None)[source]#
Construct a
ScanOpthat functions like Haskell’sfoldl.- Parameters:
fn – The function that
foldlapplies at each iteration step (seescanfor more info).sequences – List of sequences over which
foldliterates (seescanfor more info).outputs_info – List of dictionaries describing the outputs of reduce (see
scanfor more info).non_sequences – List of arguments passed to
fn.foldlwill not iterate over these arguments (seescanfor more info).mode – See
scan.name – See
scan.
- aesara.foldr(fn, sequences, outputs_info, non_sequences=None, mode=None, name=None)[source]#
Construct a
ScanOpthat functions like Haskell’sfoldr.- Parameters:
fn – The function that
foldrapplies at each iteration step (seescanfor more info).sequences – List of sequences over which
foldriterates (seescanfor more info).outputs_info – List of dictionaries describing the outputs of reduce (see
scanfor more info).non_sequences – List of arguments passed to
fn.foldrwill not iterate over these arguments (seescanfor more info).mode – See
scan.name – See
scan.