提交 a234c129 authored 作者: Iban Harlouchet's avatar Iban Harlouchet

numpydoc for theano/scan_module/scan.py

上级 0a7415d7
""" """
This module provides the Scan Op This module provides the Scan Op.
Scanning is a general form of recurrence, which can be used for looping. 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 The idea is that you *scan* a function along some input sequence, producing
...@@ -32,6 +32,7 @@ host at each step ...@@ -32,6 +32,7 @@ host at each step
The Scan Op should typically be used by calling any of the following The Scan Op should typically be used by calling any of the following
functions: ``scan()``, ``map()``, ``reduce()``, ``foldl()``, functions: ``scan()``, ``map()``, ``reduce()``, ``foldl()``,
``foldr()``. ``foldr()``.
""" """
__docformat__ = 'restructedtext en' __docformat__ = 'restructedtext en'
__authors__ = ("Razvan Pascanu " __authors__ = ("Razvan Pascanu "
...@@ -84,7 +85,9 @@ def scan(fn, ...@@ -84,7 +85,9 @@ def scan(fn,
This function constructs and applies a Scan op to the provided This function constructs and applies a Scan op to the provided
arguments. arguments.
:param fn: Parameters
----------
fn
``fn`` is a function that describes the operations involved in one ``fn`` is a function that describes the operations involved in one
step of ``scan``. ``fn`` should construct variables describing the step of ``scan``. ``fn`` should construct variables describing the
output of one iteration step. It should expect as input theano output of one iteration step. It should expect as input theano
...@@ -175,7 +178,7 @@ def scan(fn, ...@@ -175,7 +178,7 @@ def scan(fn,
number of steps ) is still required even though a condition is number of steps ) is still required even though a condition is
passed (and it is used to allocate memory if needed). = {}): passed (and it is used to allocate memory if needed). = {}):
:param sequences: sequences
``sequences`` is the list of Theano variables or dictionaries ``sequences`` is the list of Theano variables or dictionaries
describing the sequences ``scan`` has to iterate over. If a describing the sequences ``scan`` has to iterate over. If a
sequence is given as wrapped in a dictionary, then a set of optional sequence is given as wrapped in a dictionary, then a set of optional
...@@ -193,8 +196,7 @@ def scan(fn, ...@@ -193,8 +196,7 @@ def scan(fn,
Any Theano variable in the list ``sequences`` is automatically Any Theano variable in the list ``sequences`` is automatically
wrapped into a dictionary where ``taps`` is set to ``[0]`` wrapped into a dictionary where ``taps`` is set to ``[0]``
outputs_info
:param outputs_info:
``outputs_info`` is the list of Theano variables or dictionaries ``outputs_info`` is the list of Theano variables or dictionaries
describing the initial state of the outputs computed describing the initial state of the outputs computed
recurrently. When this initial states are given as dictionary recurrently. When this initial states are given as dictionary
...@@ -252,15 +254,13 @@ def scan(fn, ...@@ -252,15 +254,13 @@ def scan(fn,
raised (because there is no convention on how scan should map raised (because there is no convention on how scan should map
the provided information to the outputs of ``fn``) the provided information to the outputs of ``fn``)
non_sequences
:param non_sequences:
``non_sequences`` is the list of arguments that are passed to ``non_sequences`` is the list of arguments that are passed to
``fn`` at each steps. One can opt to exclude variable ``fn`` at each steps. One can opt to exclude variable
used in ``fn`` from this list as long as they are part of the used in ``fn`` from this list as long as they are part of the
computational graph, though for clarity we encourage not to do so. computational graph, though for clarity we encourage not to do so.
n_steps
:param n_steps:
``n_steps`` is the number of steps to iterate given as an int ``n_steps`` is the number of steps to iterate given as an int
or Theano scalar. If any of the input sequences do not have or Theano scalar. If any of the input sequences do not have
enough elements, scan will raise an error. If the *value is 0* the enough elements, scan will raise an error. If the *value is 0* the
...@@ -270,8 +270,7 @@ def scan(fn, ...@@ -270,8 +270,7 @@ def scan(fn,
in time. If n_steps is not provided, ``scan`` will figure in time. If n_steps is not provided, ``scan`` will figure
out the amount of steps it should run given its input sequences. out the amount of steps it should run given its input sequences.
truncate_gradient
:param truncate_gradient:
``truncate_gradient`` is the number of steps to use in truncated ``truncate_gradient`` is the number of steps to use in truncated
BPTT. If you compute gradients through a scan op, they are BPTT. If you compute gradients through a scan op, they are
computed using backpropagation through time. By providing a computed using backpropagation through time. By providing a
...@@ -279,16 +278,14 @@ def scan(fn, ...@@ -279,16 +278,14 @@ def scan(fn,
of classical BPTT, where you go for only ``truncate_gradient`` of classical BPTT, where you go for only ``truncate_gradient``
number of steps back in time. number of steps back in time.
go_backwards
:param go_backwards:
``go_backwards`` is a flag indicating if ``scan`` should go ``go_backwards`` is a flag indicating if ``scan`` should go
backwards through the sequences. If you think of each sequence backwards through the sequences. If you think of each sequence
as indexed by time, making this flag True would mean that as indexed by time, making this flag True would mean that
``scan`` goes back in time, namely that for any sequence it ``scan`` goes back in time, namely that for any sequence it
starts from the end and goes towards 0. starts from the end and goes towards 0.
name
:param name:
When profiling ``scan``, it is crucial to provide a name for any When profiling ``scan``, it is crucial to provide a name for any
instance of ``scan``. The profiler will produce an overall instance of ``scan``. The profiler will produce an overall
profile of your code as well as profiles for the computation of profile of your code as well as profiles for the computation of
...@@ -296,7 +293,7 @@ def scan(fn, ...@@ -296,7 +293,7 @@ def scan(fn,
appears in those profiles and can greatly help to disambiguate appears in those profiles and can greatly help to disambiguate
information. information.
:param mode: mode
It is recommended to leave this argument to None, especially It is recommended to leave this argument to None, especially
when profiling ``scan`` (otherwise the results are not going to when profiling ``scan`` (otherwise the results are not going to
be accurate). If you prefer the computations of one step of be accurate). If you prefer the computations of one step of
...@@ -305,7 +302,7 @@ def scan(fn, ...@@ -305,7 +302,7 @@ def scan(fn,
loop are done (see ``theano.function`` for details about loop are done (see ``theano.function`` for details about
possible values and their meaning). possible values and their meaning).
:param profile: profile
Flag or string. If true, or different from the empty string, a Flag or string. If true, or different from the empty string, a
profile object will be created and attached to the inner graph of profile object will be created and attached to the inner graph of
scan. In case ``profile`` is True, the profile object will have the scan. In case ``profile`` is True, the profile object will have the
...@@ -314,25 +311,27 @@ def scan(fn, ...@@ -314,25 +311,27 @@ def scan(fn,
inner graph with the new cvm linker ( with default modes, inner graph with the new cvm linker ( with default modes,
other linkers this argument is useless) other linkers this argument is useless)
:param allow_gc: allow_gc
Set the value of allow gc for the internal graph of scan. If Set the value of allow gc for the internal graph of scan. If
set to None, this will use the value of config.scan.allow_gc. set to None, this will use the value of config.scan.allow_gc.
:param strict: strict
If true, all the shared variables used in ``fn`` must be provided as a If true, all the shared variables used in ``fn`` must be provided as a
part of ``non_sequences`` or ``sequences``. part of ``non_sequences`` or ``sequences``.
:rtype: tuple Returns
:return: tuple of the form (outputs, updates); ``outputs`` is either a -------
Theano variable or a list of Theano variables representing the tuple
outputs of ``scan`` (in the same order as in Tuple of the form (outputs, updates); ``outputs`` is either a
``outputs_info``). ``updates`` is a subclass of dictionary Theano variable or a list of Theano variables representing the
specifying the outputs of ``scan`` (in the same order as in ``outputs_info``).
update rules for all shared variables used in scan ``updates`` is a subclass of dictionary specifying the update rules for
This dictionary should be passed to ``theano.function`` when all shared variables used in scan.
you compile your function. The change compared to a normal This dictionary should be passed to ``theano.function`` when you compile
dictionary is that we validate that keys are SharedVariable your function. The change compared to a normal dictionary is that we
and addition of those dictionary are validated to be consistent. validate that keys are SharedVariable and addition of those dictionary
are validated to be consistent.
""" """
# General observation : this code is executed only once, at creation # General observation : this code is executed only once, at creation
# of the computational graph, so we don't yet need to be smart about # of the computational graph, so we don't yet need to be smart about
...@@ -344,9 +343,10 @@ def scan(fn, ...@@ -344,9 +343,10 @@ def scan(fn,
# check if inputs are just single variables instead of lists # check if inputs are just single variables instead of lists
def wrap_into_list(x): def wrap_into_list(x):
''' """
Wrap the input into a list if it is not already a list Wrap the input into a list if it is not already a list.
'''
"""
if x is None: if x is None:
return [] return []
elif not isinstance(x, (list, tuple)): elif not isinstance(x, (list, tuple)):
...@@ -534,7 +534,7 @@ def scan(fn, ...@@ -534,7 +534,7 @@ def scan(fn,
if len(lengths_vec) == 0: if len(lengths_vec) == 0:
# ^ No information about the number of steps # ^ No information about the number of steps
raise ValueError(' No information about the number of steps ' raise ValueError('No information about the number of steps '
'provided. Either provide a value for ' 'provided. Either provide a value for '
'n_steps argument of scan or provide an input ' 'n_steps argument of scan or provide an input '
'sequence') 'sequence')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论