Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
caa580bb
提交
caa580bb
authored
4月 30, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
5月 09, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix broadcasting bug in vectorize of RandomVariables
上级
c9f5f656
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
25 行增加
和
8 行删除
+25
-8
op.py
pytensor/tensor/random/op.py
+7
-7
utils.py
pytensor/tensor/random/utils.py
+10
-1
test_op.py
tests/tensor/random/test_op.py
+8
-0
没有找到文件。
pytensor/tensor/random/op.py
浏览文件 @
caa580bb
...
...
@@ -20,6 +20,7 @@ from pytensor.tensor.basic import (
)
from
pytensor.tensor.random.type
import
RandomGeneratorType
,
RandomStateType
,
RandomType
from
pytensor.tensor.random.utils
import
(
compute_batch_shape
,
explicit_expand_dims
,
normalize_size_param
,
)
...
...
@@ -403,15 +404,14 @@ def vectorize_random_variable(
original_expanded_dist_params
,
dict
(
zip
(
original_dist_params
,
dist_params
))
)
if
len_old_size
and
equal_computations
([
old_size
],
[
size
]):
new_ndim
=
dist_params
[
0
]
.
type
.
ndim
-
original_expanded_dist_params
[
0
]
.
type
.
ndim
if
new_ndim
and
len_old_size
and
equal_computations
([
old_size
],
[
size
]):
# If the original RV had a size variable and a new one has not been provided,
# we need to define a new size as the concatenation of the original size dimensions
# and the novel ones implied by new broadcasted batched parameters dimensions.
# We use the first broadcasted batch dimension for reference.
bcasted_param
=
explicit_expand_dims
(
dist_params
,
op
.
ndims_params
)[
0
]
new_param_ndim
=
(
bcasted_param
.
type
.
ndim
-
op
.
ndims_params
[
0
])
-
len_old_size
if
new_param_ndim
>=
0
:
new_size_dims
=
bcasted_param
.
shape
[:
new_param_ndim
]
size
=
concatenate
([
new_size_dims
,
size
])
broadcasted_batch_shape
=
compute_batch_shape
(
dist_params
,
op
.
ndims_params
)
new_size_dims
=
broadcasted_batch_shape
[:
new_ndim
]
size
=
concatenate
([
new_size_dims
,
size
])
return
op
.
make_node
(
rng
,
size
,
dtype
,
*
dist_params
)
pytensor/tensor/random/utils.py
浏览文件 @
caa580bb
...
...
@@ -11,7 +11,7 @@ from pytensor.graph.basic import Constant, Variable
from
pytensor.scalar
import
ScalarVariable
from
pytensor.tensor
import
get_vector_length
from
pytensor.tensor.basic
import
as_tensor_variable
,
cast
,
constant
from
pytensor.tensor.extra_ops
import
broadcast_to
from
pytensor.tensor.extra_ops
import
broadcast_
arrays
,
broadcast_
to
from
pytensor.tensor.math
import
maximum
from
pytensor.tensor.shape
import
shape_padleft
,
specify_shape
from
pytensor.tensor.type
import
int_dtypes
...
...
@@ -149,6 +149,15 @@ def explicit_expand_dims(
return
new_params
def
compute_batch_shape
(
params
,
ndims_params
:
Sequence
[
int
])
->
TensorVariable
:
params
=
explicit_expand_dims
(
params
,
ndims_params
)
batch_params
=
[
param
[(
...
,
*
(
0
,)
*
core_ndim
)]
for
param
,
core_ndim
in
zip
(
params
,
ndims_params
)
]
return
broadcast_arrays
(
*
batch_params
)[
0
]
.
shape
def
normalize_size_param
(
size
:
int
|
np
.
ndarray
|
Variable
|
Sequence
|
None
,
)
->
Variable
:
...
...
tests/tensor/random/test_op.py
浏览文件 @
caa580bb
...
...
@@ -292,6 +292,14 @@ def test_vectorize_node():
assert
vect_node
.
op
is
normal
assert
vect_node
.
default_output
()
.
type
.
shape
==
(
10
,
5
)
node
=
normal
(
vec
,
size
=
(
5
,))
.
owner
new_inputs
=
node
.
inputs
.
copy
()
new_inputs
[
3
]
=
tensor
(
"mu"
,
shape
=
(
1
,
5
))
# mu
new_inputs
[
4
]
=
tensor
(
"sigma"
,
shape
=
(
10
,))
# sigma
vect_node
=
vectorize_node
(
node
,
*
new_inputs
)
assert
vect_node
.
op
is
normal
assert
vect_node
.
default_output
()
.
type
.
shape
==
(
10
,
5
)
# Test parameter broadcasting with expanding size
node
=
normal
(
vec
,
size
=
(
2
,
5
))
.
owner
new_inputs
=
node
.
inputs
.
copy
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论