Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a95c3f85
提交
a95c3f85
authored
5月 25, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
5月 25, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clean up local_subtensor_rv_lift and local_dimshuffle_rv_lift docstrings
上级
dd75c2d5
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
24 行增加
和
23 行删除
+24
-23
opt.py
aesara/tensor/random/opt.py
+24
-23
没有找到文件。
aesara/tensor/random/opt.py
浏览文件 @
a95c3f85
...
@@ -76,24 +76,27 @@ def lift_rv_shapes(node):
...
@@ -76,24 +76,27 @@ def lift_rv_shapes(node):
@local_optimizer
([
DimShuffle
])
@local_optimizer
([
DimShuffle
])
def
local_dimshuffle_rv_lift
(
fgraph
,
node
):
def
local_dimshuffle_rv_lift
(
fgraph
,
node
):
"""Lift
`DimShuffle`s through `RandomVariable` `Op`
s.
"""Lift
a ``DimShuffle`` through ``RandomVariable`` input
s.
For example, ``normal(mu, std).T == normal(mu.T, std.T)``.
For example, ``normal(mu, std).T == normal(mu.T, std.T)``.
The basic idea behind this
optimization
is that we need to separate the
The basic idea behind this
rewrite
is that we need to separate the
`
DimShuffle`ing into independent `DimShuffle
`s that each occur in two
`
`DimShuffle``-ing into distinct ``DimShuffle`
`s that each occur in two
distinct sub-spaces: the
parameters and ``size`` (i.e. replications)
distinct sub-spaces: the
(set of independent) parameters and ``size``
sub-spaces.
(i.e. replications)
sub-spaces.
If a `
DimShuffle
` exchanges dimensions across those two sub-spaces, then we
If a `
`DimShuffle`
` exchanges dimensions across those two sub-spaces, then we
don't do anything.
don't do anything.
Otherwise, if the `
DimShuffle
` only exchanges dimensions within each of
Otherwise, if the `
`DimShuffle`
` only exchanges dimensions within each of
those sub-spaces, we can break it apart and apply the parameter-space
those sub-spaces, we can break it apart and apply the parameter-space
`DimShuffle` to the `RandomVariable`'s distribution parameters, and the
``DimShuffle`` to the distribution parameters, and then apply the
apply the replications-space `DimShuffle` to the `RandomVariable`'s``size``
replications-space ``DimShuffle`` to the ``size`` tuple. The latter is a
tuple. The latter is a particularly simple rearranging of a tuple, but the
particularly simple rearranging of a tuple, but the former requires a
former requires a little more work.
little more work.
TODO: Currently, multivariate support for this rewrite is disabled.
"""
"""
ds_op
=
node
.
op
ds_op
=
node
.
op
...
@@ -132,7 +135,7 @@ def local_dimshuffle_rv_lift(fgraph, node):
...
@@ -132,7 +135,7 @@ def local_dimshuffle_rv_lift(fgraph, node):
# If the indices in `ds_new_order` are entirely within the replication
# If the indices in `ds_new_order` are entirely within the replication
# indices group or the independent variates indices group, then we can apply
# indices group or the independent variates indices group, then we can apply
# this
optimization
.
# this
rewrite
.
ds_new_order
=
ds_op
.
new_order
ds_new_order
=
ds_op
.
new_order
# Create a map from old index order to new/`DimShuffled` index order
# Create a map from old index order to new/`DimShuffled` index order
...
@@ -212,28 +215,29 @@ def local_dimshuffle_rv_lift(fgraph, node):
...
@@ -212,28 +215,29 @@ def local_dimshuffle_rv_lift(fgraph, node):
@local_optimizer
([
Subtensor
,
AdvancedSubtensor1
,
AdvancedSubtensor
])
@local_optimizer
([
Subtensor
,
AdvancedSubtensor1
,
AdvancedSubtensor
])
def
local_subtensor_rv_lift
(
fgraph
,
node
):
def
local_subtensor_rv_lift
(
fgraph
,
node
):
"""Lift
``*Subtensor`` `Op`s up to a `RandomVariable`'s parameter
s.
"""Lift
a ``*Subtensor`` through ``RandomVariable`` input
s.
In a fashion similar to `
local_dimshuffle_rv_lift
`, the indexed dimensions
In a fashion similar to `
`local_dimshuffle_rv_lift`
`, the indexed dimensions
need to be separated into distinct replication-space and (independent)
need to be separated into distinct replication-space and (independent)
parameter-space ``*Subtensor``s.
parameter-space ``*Subtensor``s.
The replication-space ``*Subtensor`` can be used to determine a
The replication-space ``*Subtensor`` can be used to determine a
sub/super-set of the replication-space and, thus, a "smaller"/"larger"
sub/super-set of the replication-space and, thus, a "smaller"/"larger"
``size`` tuple. The parameter-space ``*Subtensor`` is simply lifted and
``size`` tuple. The parameter-space ``*Subtensor`` is simply lifted and
applied to the
`RandomVariable`'s
distribution parameters.
applied to the distribution parameters.
Consider the following example graph:
Consider the following example graph:
``normal(mu, std, size=(d1, d2, d3))[idx1, idx2, idx3]``. The
``normal(mu, std, size=(d1, d2, d3))[idx1, idx2, idx3]``. The
``*Subtensor`` `
Op
` requests indices ``idx1``, ``idx2``, and ``idx3``,
``*Subtensor`` `
`Op`
` requests indices ``idx1``, ``idx2``, and ``idx3``,
which correspond to all three ``size`` dimensions. Now, depending on the
which correspond to all three ``size`` dimensions. Now, depending on the
broadcasted dimensions of ``mu`` and ``std``, this ``*Subtensor`` `
Op
`
broadcasted dimensions of ``mu`` and ``std``, this ``*Subtensor`` `
`Op`
`
could be reducing the ``size`` parameter and/or subsetting the independent
could be reducing the ``size`` parameter and/or sub
-
setting the independent
``mu`` and ``std`` parameters. Only once the dimensions are properly
``mu`` and ``std`` parameters. Only once the dimensions are properly
separated into the two replication/parameter subspaces can we determine how
separated into the two replication/parameter subspaces can we determine how
the ``*Subtensor`` indices are distributed.
the ``*Subtensor`` indices are distributed.
For instance, ``normal(mu, std, size=(d1, d2, d3))[idx1, idx2, idx3]``
For instance, ``normal(mu, std, size=(d1, d2, d3))[idx1, idx2, idx3]``
could become ``normal(mu[idx1], std[idx2], size=np.shape(idx1) + np.shape(idx2) + np.shape(idx3))``
could become
``normal(mu[idx1], std[idx2], size=np.shape(idx1) + np.shape(idx2) + np.shape(idx3))``
if ``mu.shape == std.shape == ()``
if ``mu.shape == std.shape == ()``
``normal`` is a rather simple case, because it's univariate. Multivariate
``normal`` is a rather simple case, because it's univariate. Multivariate
...
@@ -242,10 +246,7 @@ def local_subtensor_rv_lift(fgraph, node):
...
@@ -242,10 +246,7 @@ def local_subtensor_rv_lift(fgraph, node):
distributions it is. For example, the dimensions of the multivariate
distributions it is. For example, the dimensions of the multivariate
normal's image can be mapped directly to each dimension of its parameters.
normal's image can be mapped directly to each dimension of its parameters.
We use these mappings to change a graph like ``multivariate_normal(mu, Sigma)[idx1]``
We use these mappings to change a graph like ``multivariate_normal(mu, Sigma)[idx1]``
into ``multivariate_normal(mu[idx1], Sigma[idx1, idx1])``. Notice how
into ``multivariate_normal(mu[idx1], Sigma[idx1, idx1])``.
Also, there's the important matter of "advanced" indexing, which may not
only subset an array, but also broadcast it to a larger size.
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论