提交 34154000 authored 作者: Cesar Laurent's avatar Cesar Laurent

Added docstring of scan_checkpoint.

上级 9b02fa85
...@@ -4,16 +4,67 @@ import theano ...@@ -4,16 +4,67 @@ import theano
def scan_with_checkpoints(fn, sequences=[], outputs_info=None, def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
non_sequences=[], name="checkpointscan_fn", non_sequences=[], name="checkpointscan_fn",
n_steps=None, save_every_N=10): n_steps=None, save_every_N=10):
""" """Scan function that uses less memory, but is more restrictive.
In ``scan``, if you compute the gradient of the output with respect
to the input, you will have to store the intermediate results at
each time step, which can be prohibitively huge. This function allows
to do several steps of forward computations without storing the
intermediate results, and to recompute them during the gradient
computation.
Current assumptions : Current assumptions :
- Every sequence has the same length - Every sequence has the same length.
- If n_steps is specified, it has the same value as the length of any sequence - If n_steps is specified, it has the same value as the length of any
sequence.
- The value of "save_every_N" divides the number of steps the Scan will - The value of "save_every_N" divides the number of steps the Scan will
run without remainder run without remainder.
- Only singly-recurrent and non-recurrent outputs are used. - Only singly-recurrent and non-recurrent outputs are used.
No multiple recurrences. No multiple recurrences.
- Only the last timestep of any output will ever be used. - Only the last timestep of any output will ever be used.
Parameters
----------
fn
``fn`` is a function that describes the operations involved in one
step of ``scan``. See the documentation of ``scan`` for more
information.
sequences
``sequences`` is the list of Theano variables or dictionaries
describing the sequences ``scan`` has to iterate over. All
sequences must be the same length in this version of ``scan``.
outputs_info
``outputs_info`` is the list of Theano variables or dictionaries
describing the initial state of the outputs computed
recurrently.
non_sequences
``non_sequences`` is the list of arguments that are passed to
``fn`` at each steps. One can opt to exclude variable
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.
n_steps
``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
enough elements, scan will raise an error. If the *value is 0* the
outputs will have *0 rows*. If the value is negative, ``scan``
will run backwards in time. If the ``go_backwards`` flag is already
set and also ``n_steps`` is negative, ``scan`` will run forward
in time. If n_steps is not provided, ``scan`` will figure
out the amount of steps it should run given its input sequences.
save_every_N
``save_every_N`` is the number of steps to go without storing
the computations of scan (ie they will have to be recomputed
during the gradient computation).
See Also
--------
scan : Looping in Theano.
""" """
# Standardize the format of input arguments # Standardize the format of input arguments
if not isinstance(sequences, list): if not isinstance(sequences, list):
...@@ -26,8 +77,6 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None, ...@@ -26,8 +77,6 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
# Determine how many steps the original scan would run # Determine how many steps the original scan would run
if n_steps is None: if n_steps is None:
n_steps = sequences[0].shape[0] n_steps = sequences[0].shape[0]
else:
n_steps = n_steps
# Compute the number of steps of the inner and of the outer scan # Compute the number of steps of the inner and of the outer scan
o_n_steps = theano.tensor.cast(n_steps / save_every_N, 'int64') o_n_steps = theano.tensor.cast(n_steps / save_every_N, 'int64')
...@@ -71,5 +120,4 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None, ...@@ -71,5 +120,4 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
name=name + "_outer", name=name + "_outer",
n_steps=o_n_steps, allow_gc=True) n_steps=o_n_steps, allow_gc=True)
# Keep only the last timestep of every output but keep all the updates
return results, updates return results, updates
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论