Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bcd52e63
提交
bcd52e63
authored
2月 19, 2026
作者:
elc45
提交者:
Ricardo Vieira
2月 24, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make scan dosctring render legibly
上级
83d029c2
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
63 行增加
和
81 行删除
+63
-81
basic.py
pytensor/scan/basic.py
+63
-81
没有找到文件。
pytensor/scan/basic.py
浏览文件 @
bcd52e63
...
@@ -164,7 +164,7 @@ def _manage_output_api_change(outputs, updates, return_updates):
...
@@ -164,7 +164,7 @@ def _manage_output_api_change(outputs, updates, return_updates):
def
scan
(
def
scan
(
fn
,
fn
:
typing
.
Callable
,
sequences
=
None
,
sequences
=
None
,
outputs_info
=
None
,
outputs_info
=
None
,
non_sequences
=
None
,
non_sequences
=
None
,
...
@@ -179,28 +179,27 @@ def scan(
...
@@ -179,28 +179,27 @@ def scan(
return_list
=
False
,
return_list
=
False
,
return_updates
:
bool
=
True
,
return_updates
:
bool
=
True
,
):
):
r"""This function constructs and applies a
`Scan` `Op`
to the provided arguments.
r"""This function constructs and applies a
scan operation
to the provided arguments.
Parameters
Parameters
----------
----------
fn
fn
: callable
`fn` is a
function that describes the operations involved in one
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
output of one iteration step. It should expect as input
`Variable`\s representing all the slices of the input sequences
`Variable`\s representing all the slices of the input sequences
and previous values of the outputs, as well as all other arguments
and previous values of the outputs, as well as all other arguments
given to scan as `non_sequences`. The order in which scan passes
given to scan as `non_sequences`. The order in which scan passes
these variables to `fn` is the following :
these variables to `fn` is the following :
* all time slices of the first sequence
* all time slices of the first sequence
* all time slices of the second sequence
* all time slices of the second sequence
* ...
* ...
* all time slices of the last sequence
* all time slices of the last sequence
* all past slices of the first output
* all past slices of the first output
* all past slices of the second output
* all past slices of the second output
* ...
* ...
* all past slices of the last output
* all past slices of the last output
* all other arguments (the list given as `non_sequences` to
* all other arguments (the list given as `non_sequences` to
`scan`)
`scan`)
The order of the sequences is the same as the one in the list
The order of the sequences is the same as the one in the list
...
@@ -228,16 +227,16 @@ def scan(
...
@@ -228,16 +227,16 @@ def scan(
`fn` should expect the following arguments in this given order:
`fn` should expect the following arguments in this given order:
#. ``sequence1[t-3]``
#. ``sequence1[t-3]``
#. ``sequence1[t+2]``
#. ``sequence1[t+2]``
#. ``sequence1[t-1]``
#. ``sequence1[t-1]``
#. ``sequence2[t]``
#. ``sequence2[t]``
#. ``sequence3[t+3]``
#. ``sequence3[t+3]``
#. ``output1[t-3]``
#. ``output1[t-3]``
#. ``output1[t-5]``
#. ``output1[t-5]``
#. ``output3[t-1]``
#. ``output3[t-1]``
#. ``argument1``
#. ``argument1``
#. ``argument2``
#. ``argument2``
The list of `non_sequences` can also contain shared variables
The list of `non_sequences` can also contain shared variables
used in the function, though `scan` is able to figure those
used in the function, though `scan` is able to figure those
...
@@ -283,13 +282,11 @@ def scan(
...
@@ -283,13 +282,11 @@ def scan(
Note that a number of steps--considered in here as the maximum
Note that a number of steps--considered in here as the maximum
number of steps--is still required even though a condition is
number of steps--is still required even though a condition is
passed. It is used to allocate memory if needed.
passed. It is used to allocate memory if needed.
sequences : list of Variable or dict or None, optional
sequences
The sequences `scan` has to iterate over. If wrapped in
`sequences` is the list of `Variable`\s or ``dict``\s
a ``dict``, then a set of optional information can be
describing the sequences `scan` has to iterate over. If a
provided about the sequence. The ``dict``should have the
sequence is given as wrapped in a ``dict``, then a set of optional
following keys:
information can be provided about the sequence. The ``dict``
should have the following keys:
* ``input`` (*mandatory*) -- `Variable` representing the
* ``input`` (*mandatory*) -- `Variable` representing the
sequence.
sequence.
...
@@ -301,14 +298,11 @@ def scan(
...
@@ -301,14 +298,11 @@ def scan(
All `Variable`\s in the list `sequences` are automatically
All `Variable`\s in the list `sequences` are automatically
wrapped into a ``dict`` where ``taps`` is set to ``[0]``
wrapped into a ``dict`` where ``taps`` is set to ``[0]``
outputs_info : list of Variable or dict or None, optional
outputs_info
The initial state of the outputs computed recurrently.
`outputs_info` is the list of `Variable`\s or ``dict``\s
If given as ``dict``\s, optional information can be
describing the initial state of the outputs computed
provided about the output corresponding to those initial states.
recurrently. When the initial states are given as ``dict``\s,
The ``dict`` should have the following keys:
optional information can be provided about the output corresponding
to those initial states. The ``dict`` should have the following
keys:
* ``initial`` -- A `Variable` that represents the initial
* ``initial`` -- A `Variable` that represents the initial
state of a given output. In case the output is not computed
state of a given output. In case the output is not computed
...
@@ -359,59 +353,50 @@ def scan(
...
@@ -359,59 +353,50 @@ def scan(
provided just for a subset of the outputs, an exception is
provided just for a subset of the outputs, an exception is
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 : list of Variable or None, optional
non_sequences
The arguments that are passed to `fn` at each step.
`non_sequences` is the list of arguments that are passed to
One can choose to exclude variables used in `fn` from this list,
`fn` at each steps. One can choose to exclude variables
as long as they are part of the computational graph, although
used in `fn` from this list, as long as they are part of the
this is not encouraged for clarity.
computational graph, although--for clarity--this is *not* encouraged.
n_steps : int or Variable or None, optional
The number of steps to iterate given as an ``int``
n_steps
`n_steps` is the number of steps to iterate given as an ``int``
or a scalar `Variable`. If any of the input sequences do not have
or a scalar `Variable`. 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
outputs will have ``0`` rows. If
`n_steps` is
not provided, `scan` will
outputs will have ``0`` rows. If not provided, `scan` will
figure out the amount of steps it should run given its input
figure out the amount of steps it should run given its input
sequences. ``n_steps < 0`` is not supported anymore.
sequences. ``n_steps < 0`` is not supported.
truncate_gradient : int
truncate_gradient
The number of steps to use in truncated
`truncate_gradient` is the number of steps to use in truncated
back-propagation through time (BPTT). If you compute gradients through
back-propagation through time (BPTT). If you compute gradients through
a `Scan` `Op`, they are computed using BPTT. By providing a different
a `Scan` `Op`, they are computed using BPTT. By providing a different
value then ``-1``, you choose to use truncated BPTT instead of classical
value then ``-1``, you choose to use truncated BPTT instead of classical
BPTT, where you go for only `truncate_gradient` number of steps back in
BPTT, where you go for only `truncate_gradient` number of steps back in
time.
time.
go_backwards : bool
go_backwards
Indicates 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 : str or None, optional
name
When profiling `scan`, it is helpful to provide a name for any
When profiling `scan`, it is helpful to provide a name for any
instance of `scan`.
instance of `scan`. For example, the profiler will produce an
For example, the profiler will produce an overall profile of your code
overall profile of your code as well as profiles for the computation
as well as profiles for the computation of one step of each instance of
of one step of each instance of `Scan`. The `name` of the instance
`Scan`. The `name` of the instance appears in those profiles and can
appears in those profiles and can greatly help to disambiguate information.
greatly help to disambiguate information.
mode : str or None, optional
mode
The mode used to compile the inner-graph.
The mode used to compile the inner-graph.
If you prefer the computations of one step of `scan` to be done
If you prefer the computations of one step of `scan` to be done
differently then the entire function, you can use this parameter to
differently then the entire function, you can use this parameter to
describe how the computations in this loop are done (see
describe how the computations in this loop are done (see
`pytensor.function` for details about possible values and their meaning).
`pytensor.function` for details about possible values and their meaning).
profile : bool or str
profile
If ``True`` or a non-empty string, a profile object will be created and
If ``True`` or a non-empty string, a profile object will be created and
attached to the inner graph of `Scan`. When `profile` is ``True``, the
attached to the inner graph of `Scan`. When `profile` is ``True``, the
profiler results will use the name of the `Scan` instance, otherwise it
profiler results will use the name of the `Scan` instance, otherwise it
will use the passed string. The profiler only collects and prints
will use the passed string. The profiler only collects and prints
information when running the inner graph with the `CVM` `Linker`.
information when running the inner graph with the `CVM` `Linker`.
allow_gc : bool or None, optional
allow_gc
Set the value of `allow_gc` for the internal graph of the `Scan`. If
Set the value of `allow_gc` for the internal graph of the `Scan`. If
set to ``None``, this will use the value of
set to ``None``, this will use the value of
`pytensor.config.scan__allow_gc`.
`pytensor.config.scan__allow_gc`.
...
@@ -425,24 +410,21 @@ def scan(
...
@@ -425,24 +410,21 @@ def scan(
speed up allocation of the subsequent iterations. All those temporary
speed up allocation of the subsequent iterations. All those temporary
allocations are freed at the end of all iterations; this is what the
allocations are freed at the end of all iterations; this is what the
flag `pytensor.config.allow_gc` means.
flag `pytensor.config.allow_gc` means.
strict : bool
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`.
return_list : bool
return_list
If ``True``, will always return a ``list``, even if there is only one output.
If ``True``, will always return a ``list``, even if there is only one output.
return_updates : bool, optional
If ``True`` (default), the returned tuple includes the updates dictionary.
Returns
Returns
-------
-------
tuple
outputs : Variable or list of Variable
``tuple`` of the form ``(outputs, updates)``.
The outputs of the scan, in the same order as `outputs_info`.
``outputs`` is either a `Variable` or a ``list`` of `Variable`\s
updates : dict
representing the outputs in the same order as in `outputs_info`.
Dictionary of update rules for shared variables used in the scan.
``updates`` is a subclass of ``dict`` specifying the update rules for
Pass this to `pytensor.function` when compiling your function.
all shared variables used in `Scan`.
This ``dict`` should be passed to `pytensor.function` when you compile
your function.
"""
"""
# General observation : this code is executed only once, at creation
# General observation : this code is executed only once, at creation
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论