Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2823dfca
提交
2823dfca
authored
2月 12, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
2月 13, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Faster python implementation of MvNormal
Also remove bad default values
上级
1ed36119
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
49 行增加
和
52 行删除
+49
-52
basic.py
pytensor/tensor/random/basic.py
+17
-40
test_basic.py
tests/tensor/random/rewriting/test_basic.py
+4
-2
test_basic.py
tests/tensor/random/test_basic.py
+28
-10
没有找到文件。
pytensor/tensor/random/basic.py
浏览文件 @
2823dfca
...
@@ -3,6 +3,9 @@ import warnings
...
@@ -3,6 +3,9 @@ import warnings
import
numpy
as
np
import
numpy
as
np
import
scipy.stats
as
stats
import
scipy.stats
as
stats
from
numpy
import
broadcast_shapes
as
np_broadcast_shapes
from
numpy
import
einsum
as
np_einsum
from
numpy.linalg
import
cholesky
as
np_cholesky
import
pytensor
import
pytensor
from
pytensor.tensor
import
get_vector_length
,
specify_shape
from
pytensor.tensor
import
get_vector_length
,
specify_shape
...
@@ -831,27 +834,6 @@ class VonMisesRV(RandomVariable):
...
@@ -831,27 +834,6 @@ class VonMisesRV(RandomVariable):
vonmises
=
VonMisesRV
()
vonmises
=
VonMisesRV
()
def
safe_multivariate_normal
(
mean
,
cov
,
size
=
None
,
rng
=
None
):
"""A shape consistent multivariate normal sampler.
What we mean by "shape consistent": SciPy will return scalars when the
arguments are vectors with dimension of size 1. We require that the output
be at least 1D, so that it's consistent with the underlying random
variable.
"""
res
=
np
.
atleast_1d
(
stats
.
multivariate_normal
(
mean
=
mean
,
cov
=
cov
,
allow_singular
=
True
)
.
rvs
(
size
=
size
,
random_state
=
rng
)
)
if
size
is
not
None
:
res
=
res
.
reshape
([
*
size
,
-
1
])
return
res
class
MvNormalRV
(
RandomVariable
):
class
MvNormalRV
(
RandomVariable
):
r"""A multivariate normal random variable.
r"""A multivariate normal random variable.
...
@@ -904,25 +886,20 @@ class MvNormalRV(RandomVariable):
...
@@ -904,25 +886,20 @@ class MvNormalRV(RandomVariable):
@classmethod
@classmethod
def
rng_fn
(
cls
,
rng
,
mean
,
cov
,
size
):
def
rng_fn
(
cls
,
rng
,
mean
,
cov
,
size
):
if
mean
.
ndim
>
1
or
cov
.
ndim
>
2
:
if
size
is
None
:
# Neither SciPy nor NumPy implement parameter broadcasting for
size
=
np_broadcast_shapes
(
mean
.
shape
[:
-
1
],
cov
.
shape
[:
-
2
])
# multivariate normals (or any other multivariate distributions),
# so we need to implement that here
chol
=
np_cholesky
(
cov
)
out
=
rng
.
normal
(
size
=
(
*
size
,
mean
.
shape
[
-
1
]))
if
size
is
None
:
np_einsum
(
mean
,
cov
=
broadcast_params
([
mean
,
cov
],
[
1
,
2
])
"...ij,...j->...i"
,
# numpy doesn't have a batch matrix-vector product
else
:
chol
,
mean
=
np
.
broadcast_to
(
mean
,
size
+
mean
.
shape
[
-
1
:])
out
,
cov
=
np
.
broadcast_to
(
cov
,
size
+
cov
.
shape
[
-
2
:])
out
=
out
,
optimize
=
False
,
# Nothing to optimize with two operands, skip costly setup
res
=
np
.
empty
(
mean
.
shape
)
)
for
idx
in
np
.
ndindex
(
mean
.
shape
[:
-
1
]):
out
+=
mean
m
=
mean
[
idx
]
return
out
c
=
cov
[
idx
]
res
[
idx
]
=
safe_multivariate_normal
(
m
,
c
,
rng
=
rng
)
return
res
else
:
return
safe_multivariate_normal
(
mean
,
cov
,
size
=
size
,
rng
=
rng
)
multivariate_normal
=
MvNormalRV
()
multivariate_normal
=
MvNormalRV
()
...
...
tests/tensor/random/rewriting/test_basic.py
浏览文件 @
2823dfca
...
@@ -778,8 +778,10 @@ def rand_bool_mask(shape, rng=None):
...
@@ -778,8 +778,10 @@ def rand_bool_mask(shape, rng=None):
multivariate_normal
,
multivariate_normal
,
(
(
np
.
array
([
200
,
250
],
dtype
=
config
.
floatX
),
np
.
array
([
200
,
250
],
dtype
=
config
.
floatX
),
# Second covariance is invalid, to test it is not chosen
# Second covariance is very large, to test it is not chosen
np
.
dstack
([
np
.
eye
(
2
),
np
.
eye
(
2
)
*
0
,
np
.
eye
(
2
)])
.
T
.
astype
(
config
.
floatX
)
np
.
dstack
([
np
.
eye
(
2
),
np
.
eye
(
2
)
*
1000
,
np
.
eye
(
2
)])
.
T
.
astype
(
config
.
floatX
)
*
1e-6
,
*
1e-6
,
),
),
(
3
,),
(
3
,),
...
...
tests/tensor/random/test_basic.py
浏览文件 @
2823dfca
...
@@ -521,13 +521,19 @@ def test_pareto_samples(alpha, scale, size):
...
@@ -521,13 +521,19 @@ def test_pareto_samples(alpha, scale, size):
def
mvnormal_test_fn
(
mean
=
None
,
cov
=
None
,
size
=
None
,
random_state
=
None
):
def
mvnormal_test_fn
(
mean
=
None
,
cov
=
None
,
size
=
None
,
random_state
=
None
):
if
mean
is
None
:
rng
=
random_state
if
random_state
is
not
None
else
np
.
random
.
default_rng
()
mean
=
np
.
array
([
0.0
],
dtype
=
config
.
floatX
)
if
cov
is
None
:
if
size
is
None
:
cov
=
np
.
array
([[
1.0
]],
dtype
=
config
.
floatX
)
size
=
np
.
broadcast_shapes
(
mean
.
shape
[:
-
1
],
cov
.
shape
[:
-
2
])
if
size
is
not
None
:
size
=
tuple
(
size
)
mean
=
np
.
broadcast_to
(
mean
,
(
*
size
,
*
mean
.
shape
[
-
1
:]))
return
multivariate_normal
.
rng_fn
(
random_state
,
mean
,
cov
,
size
)
cov
=
np
.
broadcast_to
(
cov
,
(
*
size
,
*
cov
.
shape
[
-
2
:]))
@np.vectorize
(
signature
=
"(n),(n,n)->(n)"
)
def
vec_mvnormal
(
mean
,
cov
):
return
rng
.
multivariate_normal
(
mean
,
cov
,
method
=
"cholesky"
)
return
vec_mvnormal
(
mean
,
cov
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
...
@@ -609,18 +615,30 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
...
@@ -609,18 +615,30 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
),
),
],
],
)
)
@pytest.mark.skipif
(
config
.
floatX
==
"float32"
,
reason
=
"Draws are only strictly equal to numpy in float64"
,
)
def
test_mvnormal_samples
(
mu
,
cov
,
size
):
def
test_mvnormal_samples
(
mu
,
cov
,
size
):
compare_sample_values
(
compare_sample_values
(
multivariate_normal
,
mu
,
cov
,
size
=
size
,
test_fn
=
mvnormal_test_fn
multivariate_normal
,
mu
,
cov
,
size
=
size
,
test_fn
=
mvnormal_test_fn
)
)
def
test_mvnormal_default_args
():
def
test_mvnormal_no_default_args
():
compare_sample_values
(
multivariate_normal
,
test_fn
=
mvnormal_test_fn
)
with
pytest
.
raises
(
TypeError
,
match
=
"missing 2 required positional arguments: 'mean' and 'cov'"
):
multivariate_normal
()
def
test_mvnormal_impl_catches_incompatible_size
():
with
pytest
.
raises
(
ValueError
,
match
=
"operands could not be broadcast together "
):
with
pytest
.
raises
(
ValueError
,
match
=
"operands could not be broadcast together "
):
multivariate_normal
.
rng_fn
(
multivariate_normal
.
rng_fn
(
None
,
np
.
zeros
((
3
,
2
)),
np
.
ones
((
3
,
2
,
2
)),
size
=
(
4
,)
np
.
random
.
default_rng
(),
np
.
zeros
((
3
,
2
)),
np
.
broadcast_to
(
np
.
eye
(
2
),
(
3
,
2
,
2
)),
size
=
(
4
,),
)
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论