Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
24501862
提交
24501862
authored
5月 28, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
2月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Require independent dimensions in multivariate size arguments to RandomVariable
上级
da86d351
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
118 行增加
和
47 行删除
+118
-47
basic.py
aesara/tensor/random/basic.py
+42
-14
op.py
aesara/tensor/random/op.py
+14
-15
opt.py
aesara/tensor/random/opt.py
+12
-2
test_basic.py
tests/tensor/random/test_basic.py
+38
-13
test_opt.py
tests/tensor/random/test_opt.py
+12
-3
没有找到文件。
aesara/tensor/random/basic.py
浏览文件 @
24501862
...
@@ -321,14 +321,21 @@ class MvNormalRV(RandomVariable):
...
@@ -321,14 +321,21 @@ class MvNormalRV(RandomVariable):
if
mean
.
ndim
>
1
or
cov
.
ndim
>
2
:
if
mean
.
ndim
>
1
or
cov
.
ndim
>
2
:
# Neither SciPy nor NumPy implement parameter broadcasting for
# Neither SciPy nor NumPy implement parameter broadcasting for
# multivariate normals (or
m
any other multivariate distributions),
# multivariate normals (or any other multivariate distributions),
# so we
have implement a quick and dirty one
here
# so we
need to implement that
here
mean
,
cov
=
broadcast_params
([
mean
,
cov
],
cls
.
ndims_params
)
mean
,
cov
=
broadcast_params
([
mean
,
cov
],
cls
.
ndims_params
)
size
=
tuple
(
size
or
())
size
=
tuple
(
size
or
())
if
size
:
if
size
:
mean
=
np
.
broadcast_to
(
mean
,
size
+
mean
.
shape
)
if
(
cov
=
np
.
broadcast_to
(
cov
,
size
+
cov
.
shape
)
0
<
mean
.
ndim
-
1
<=
len
(
size
)
and
size
[
-
mean
.
ndim
+
1
:]
!=
mean
.
shape
[:
-
1
]
):
raise
ValueError
(
"shape mismatch: objects cannot be broadcast to a single shape"
)
mean
=
np
.
broadcast_to
(
mean
,
size
+
mean
.
shape
[
-
1
:])
cov
=
np
.
broadcast_to
(
cov
,
size
+
cov
.
shape
[
-
2
:])
res
=
np
.
empty
(
mean
.
shape
)
res
=
np
.
empty
(
mean
.
shape
)
for
idx
in
np
.
ndindex
(
mean
.
shape
[:
-
1
]):
for
idx
in
np
.
ndindex
(
mean
.
shape
[:
-
1
]):
...
@@ -352,16 +359,33 @@ class DirichletRV(RandomVariable):
...
@@ -352,16 +359,33 @@ class DirichletRV(RandomVariable):
@classmethod
@classmethod
def
rng_fn
(
cls
,
rng
,
alphas
,
size
):
def
rng_fn
(
cls
,
rng
,
alphas
,
size
):
if
size
is
None
:
if
alphas
.
ndim
>
1
:
size
=
()
if
size
is
None
:
samples_shape
=
tuple
(
np
.
atleast_1d
(
size
))
+
alphas
.
shape
size
=
()
samples
=
np
.
empty
(
samples_shape
)
alphas_bcast
=
np
.
broadcast_to
(
alphas
,
samples_shape
)
for
index
in
np
.
ndindex
(
*
samples_shape
[:
-
1
]):
size
=
tuple
(
np
.
atleast_1d
(
size
))
samples
[
index
]
=
rng
.
dirichlet
(
alphas_bcast
[
index
])
return
samples
if
size
:
if
(
0
<
alphas
.
ndim
-
1
<=
len
(
size
)
and
size
[
-
alphas
.
ndim
+
1
:]
!=
alphas
.
shape
[:
-
1
]
):
raise
ValueError
(
"shape mismatch: objects cannot be broadcast to a single shape"
)
samples_shape
=
size
+
alphas
.
shape
[
-
1
:]
else
:
samples_shape
=
alphas
.
shape
samples
=
np
.
empty
(
samples_shape
)
alphas_bcast
=
np
.
broadcast_to
(
alphas
,
samples_shape
)
for
index
in
np
.
ndindex
(
*
samples_shape
[:
-
1
]):
samples
[
index
]
=
rng
.
dirichlet
(
alphas_bcast
[
index
])
return
samples
else
:
return
rng
.
dirichlet
(
alphas
,
size
=
size
)
dirichlet
=
DirichletRV
()
dirichlet
=
DirichletRV
()
...
@@ -579,8 +603,12 @@ class MultinomialRV(RandomVariable):
...
@@ -579,8 +603,12 @@ class MultinomialRV(RandomVariable):
size
=
tuple
(
size
or
())
size
=
tuple
(
size
or
())
if
size
:
if
size
:
n
=
np
.
broadcast_to
(
n
,
size
+
n
.
shape
)
if
0
<
p
.
ndim
-
1
<=
len
(
size
)
and
size
[
-
p
.
ndim
+
1
:]
!=
p
.
shape
[:
-
1
]:
p
=
np
.
broadcast_to
(
p
,
size
+
p
.
shape
)
raise
ValueError
(
"shape mismatch: objects cannot be broadcast to a single shape"
)
n
=
np
.
broadcast_to
(
n
,
size
)
p
=
np
.
broadcast_to
(
p
,
size
+
p
.
shape
[
-
1
:])
res
=
np
.
empty
(
p
.
shape
,
dtype
=
cls
.
dtype
)
res
=
np
.
empty
(
p
.
shape
,
dtype
=
cls
.
dtype
)
for
idx
in
np
.
ndindex
(
p
.
shape
[:
-
1
]):
for
idx
in
np
.
ndindex
(
p
.
shape
[:
-
1
]):
...
...
aesara/tensor/random/op.py
浏览文件 @
24501862
...
@@ -190,15 +190,14 @@ class RandomVariable(Op):
...
@@ -190,15 +190,14 @@ class RandomVariable(Op):
size_len
=
get_vector_length
(
size
)
size_len
=
get_vector_length
(
size
)
if
self
.
ndim_supp
==
0
and
size_len
>
0
:
if
size_len
>
0
:
# In this case, we have a univariate distribution with a non-empty
if
self
.
ndim_supp
==
0
:
# `size` parameter, which means that the `size` parameter
return
size
# completely determines the shape of the random variable. More
else
:
# importantly, the `size` parameter may be the only correct source
supp_shape
=
self
.
_shape_from_params
(
# of information for the output shape, in that we would be misled
dist_params
,
param_shapes
=
param_shapes
# by the `dist_params` if we tried to infer the relevant parts of
)
# the output shape from those.
return
tuple
(
size
)
+
tuple
(
supp_shape
)
return
size
# Broadcast the parameters
# Broadcast the parameters
param_shapes
=
params_broadcast_shapes
(
param_shapes
=
params_broadcast_shapes
(
...
@@ -307,19 +306,19 @@ class RandomVariable(Op):
...
@@ -307,19 +306,19 @@ class RandomVariable(Op):
Existing Aesara `Generator` or `RandomState` object to be used. Creates a
Existing Aesara `Generator` or `RandomState` object to be used. Creates a
new one, if `None`.
new one, if `None`.
size: int or Sequence
size: int or Sequence
Num
py-like size of the output (i.e. replications)
.
Num
Py-like size parameter
.
dtype: str
dtype: str
The dtype of the sampled output. If the value ``"floatX"`` is
The dtype of the sampled output. If the value ``"floatX"`` is
given, then `
`dtype`` is set to ``aesara.config.floatX``. Th
is
given, then `
dtype` is set to ``aesara.config.floatX``. This value
is
value is only used when `self.dtype
` isn't set.
only used when ``self.dtype`
` isn't set.
dist_params: list
dist_params: list
Distribution parameters.
Distribution parameters.
Results
Results
-------
-------
out:
`Apply`
out:
Apply
A node with inputs `
(rng, size, dtype) + dist_args
` and outputs
A node with inputs `
`(rng, size, dtype) + dist_args`
` and outputs
`
(rng_var, out_var)
`.
`
`(rng_var, out_var)`
`.
"""
"""
size
=
normalize_size_param
(
size
)
size
=
normalize_size_param
(
size
)
...
...
aesara/tensor/random/opt.py
浏览文件 @
24501862
...
@@ -83,9 +83,19 @@ def local_rv_size_lift(fgraph, node):
...
@@ -83,9 +83,19 @@ def local_rv_size_lift(fgraph, node):
if
get_vector_length
(
size
)
>
0
:
if
get_vector_length
(
size
)
>
0
:
dist_params
=
[
dist_params
=
[
broadcast_to
(
broadcast_to
(
p
,
(
tuple
(
size
)
+
tuple
(
p
.
shape
))
if
node
.
op
.
ndim_supp
>
0
else
size
p
,
(
tuple
(
size
)
+
(
tuple
(
p
.
shape
)[
-
node
.
op
.
ndims_params
[
i
]
:]
if
node
.
op
.
ndims_params
[
i
]
>
0
else
()
)
)
if
node
.
op
.
ndim_supp
>
0
else
size
,
)
)
for
p
in
dist_params
for
i
,
p
in
enumerate
(
dist_params
)
]
]
else
:
else
:
return
return
...
...
tests/tensor/random/test_basic.py
浏览文件 @
24501862
...
@@ -534,7 +534,7 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
...
@@ -534,7 +534,7 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
*
10.0
,
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
*
10.0
,
]
]
),
),
[
2
,
3
],
[
2
,
3
,
2
],
),
),
(
(
np
.
array
([[
0
,
1
,
2
],
[
4
,
5
,
6
]],
dtype
=
config
.
floatX
),
np
.
array
([[
0
,
1
,
2
],
[
4
,
5
,
6
]],
dtype
=
config
.
floatX
),
...
@@ -551,12 +551,12 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
...
@@ -551,12 +551,12 @@ def mvnormal_test_fn(mean=None, cov=None, size=None, random_state=None):
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
*
10.0
,
np
.
eye
(
3
,
dtype
=
config
.
floatX
)
*
10.0
,
]
]
),
),
[
2
,
3
],
[
2
,
3
,
2
,
2
],
),
),
(
(
np
.
array
([[
0
],
[
10
],
[
100
]],
dtype
=
config
.
floatX
),
np
.
array
([[
0
],
[
10
],
[
100
]],
dtype
=
config
.
floatX
),
np
.
eye
(
1
,
dtype
=
config
.
floatX
)
*
1e-6
,
np
.
eye
(
1
,
dtype
=
config
.
floatX
)
*
1e-6
,
[
2
,
3
],
[
2
,
3
,
3
],
),
),
],
],
)
)
...
@@ -567,6 +567,11 @@ def test_mvnormal_samples(mu, cov, size):
...
@@ -567,6 +567,11 @@ def test_mvnormal_samples(mu, cov, size):
def
test_mvnormal_default_args
():
def
test_mvnormal_default_args
():
rv_numpy_tester
(
multivariate_normal
,
test_fn
=
mvnormal_test_fn
)
rv_numpy_tester
(
multivariate_normal
,
test_fn
=
mvnormal_test_fn
)
with
pytest
.
raises
(
ValueError
,
match
=
"shape mismatch.*"
):
multivariate_normal
.
rng_fn
(
None
,
np
.
zeros
((
1
,
2
)),
np
.
ones
((
1
,
2
,
2
)),
size
=
(
4
,)
)
@config.change_flags
(
compute_test_value
=
"raise"
)
@config.change_flags
(
compute_test_value
=
"raise"
)
def
test_mvnormal_ShapeFeature
():
def
test_mvnormal_ShapeFeature
():
...
@@ -596,11 +601,10 @@ def test_mvnormal_ShapeFeature():
...
@@ -596,11 +601,10 @@ def test_mvnormal_ShapeFeature():
cov
=
at
.
as_tensor
(
test_covar
)
.
type
()
cov
=
at
.
as_tensor
(
test_covar
)
.
type
()
cov
.
tag
.
test_value
=
test_covar
cov
.
tag
.
test_value
=
test_covar
d_rv
=
multivariate_normal
(
mean
,
cov
,
size
=
[
2
,
3
])
d_rv
=
multivariate_normal
(
mean
,
cov
,
size
=
[
2
,
3
,
2
])
fg
=
FunctionGraph
(
fg
=
FunctionGraph
(
[
i
for
i
in
graph_inputs
([
d_rv
])
if
not
isinstance
(
i
,
Constant
)],
outputs
=
[
d_rv
],
[
d_rv
],
clone
=
False
,
clone
=
False
,
features
=
[
ShapeFeature
()],
features
=
[
ShapeFeature
()],
)
)
...
@@ -617,10 +621,13 @@ def test_mvnormal_ShapeFeature():
...
@@ -617,10 +621,13 @@ def test_mvnormal_ShapeFeature():
"alphas, size"
,
"alphas, size"
,
[
[
(
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
None
),
(
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
None
),
(
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
10
),
(
(
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
(
10
,
2
),
(
10
,
3
),
),
(
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
),
(
10
,
2
,
3
),
),
),
],
],
)
)
...
@@ -633,6 +640,15 @@ def test_dirichlet_samples(alphas, size):
...
@@ -633,6 +640,15 @@ def test_dirichlet_samples(alphas, size):
rv_numpy_tester
(
dirichlet
,
alphas
,
size
=
size
,
test_fn
=
dirichlet_test_fn
)
rv_numpy_tester
(
dirichlet
,
alphas
,
size
=
size
,
test_fn
=
dirichlet_test_fn
)
def
test_dirichlet_rng
():
alphas
=
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
"shape mismatch.*"
):
# The independent dimension's shape is missing from size (i.e. should
# be `(10, 2, 3)`)
dirichlet
.
rng_fn
(
None
,
alphas
,
size
=
(
10
,
2
))
M_at
=
iscalar
(
"M"
)
M_at
=
iscalar
(
"M"
)
M_at
.
tag
.
test_value
=
3
M_at
.
tag
.
test_value
=
3
...
@@ -644,8 +660,8 @@ M_at.tag.test_value = 3
...
@@ -644,8 +660,8 @@ M_at.tag.test_value = 3
(
at
.
ones
((
M_at
,)),
(
M_at
+
1
,)),
(
at
.
ones
((
M_at
,)),
(
M_at
+
1
,)),
(
at
.
ones
((
M_at
,)),
(
2
,
M_at
)),
(
at
.
ones
((
M_at
,)),
(
2
,
M_at
)),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
()),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
()),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
(
M_at
+
2
,)),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
(
M_at
+
2
,
M_at
)),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
(
2
,
M_at
+
2
,
M_at
+
3
)),
(
at
.
ones
((
M_at
,
M_at
+
1
)),
(
2
,
M_at
+
2
,
M_at
+
3
,
M_at
)),
],
],
)
)
def
test_dirichlet_infer_shape
(
M
,
size
):
def
test_dirichlet_infer_shape
(
M
,
size
):
...
@@ -684,8 +700,7 @@ def test_dirichlet_ShapeFeature():
...
@@ -684,8 +700,7 @@ def test_dirichlet_ShapeFeature():
d_rv
=
dirichlet
(
at
.
ones
((
M_at
,
N_at
)),
name
=
"Gamma"
)
d_rv
=
dirichlet
(
at
.
ones
((
M_at
,
N_at
)),
name
=
"Gamma"
)
fg
=
FunctionGraph
(
fg
=
FunctionGraph
(
[
i
for
i
in
graph_inputs
([
d_rv
])
if
not
isinstance
(
i
,
Constant
)],
outputs
=
[
d_rv
],
[
d_rv
],
clone
=
False
,
clone
=
False
,
features
=
[
ShapeFeature
()],
features
=
[
ShapeFeature
()],
)
)
...
@@ -1092,7 +1107,7 @@ def test_betabinom_samples(M, a, p, size):
...
@@ -1092,7 +1107,7 @@ def test_betabinom_samples(M, a, p, size):
(
(
np
.
array
([
10
,
20
],
dtype
=
np
.
int64
),
np
.
array
([
10
,
20
],
dtype
=
np
.
int64
),
np
.
array
([[
0.999
,
0.001
],
[
0.001
,
0.999
]],
dtype
=
config
.
floatX
),
np
.
array
([[
0.999
,
0.001
],
[
0.001
,
0.999
]],
dtype
=
config
.
floatX
),
(
3
,),
(
3
,
2
),
lambda
*
args
,
**
kwargs
:
np
.
stack
([
np
.
array
([[
10
,
0
],
[
0
,
20
]])]
*
3
),
lambda
*
args
,
**
kwargs
:
np
.
stack
([
np
.
array
([[
10
,
0
],
[
0
,
20
]])]
*
3
),
),
),
],
],
...
@@ -1109,6 +1124,16 @@ def test_multinomial_samples(M, p, size, test_fn):
...
@@ -1109,6 +1124,16 @@ def test_multinomial_samples(M, p, size, test_fn):
)
)
def
test_multinomial_rng
():
test_M
=
np
.
array
([
10
,
20
],
dtype
=
np
.
int64
)
test_p
=
np
.
array
([[
0.999
,
0.001
],
[
0.001
,
0.999
]],
dtype
=
config
.
floatX
)
with
pytest
.
raises
(
ValueError
,
match
=
"shape mismatch.*"
):
# The independent dimension's shape is missing from size (i.e. should
# be `(1, 2)`)
multinomial
.
rng_fn
(
None
,
test_M
,
test_p
,
size
=
(
1
,))
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"p, size, test_fn"
,
"p, size, test_fn"
,
[
[
...
...
tests/tensor/random/test_opt.py
浏览文件 @
24501862
...
@@ -12,6 +12,7 @@ from aesara.graph.optdb import OptimizationQuery
...
@@ -12,6 +12,7 @@ from aesara.graph.optdb import OptimizationQuery
from
aesara.tensor.elemwise
import
DimShuffle
from
aesara.tensor.elemwise
import
DimShuffle
from
aesara.tensor.random.basic
import
(
from
aesara.tensor.random.basic
import
(
dirichlet
,
dirichlet
,
multinomial
,
multivariate_normal
,
multivariate_normal
,
normal
,
normal
,
poisson
,
poisson
,
...
@@ -120,12 +121,20 @@ def test_inplace_optimization():
...
@@ -120,12 +121,20 @@ def test_inplace_optimization():
np
.
array
([[
0
],
[
10
],
[
100
]],
dtype
=
config
.
floatX
),
np
.
array
([[
0
],
[
10
],
[
100
]],
dtype
=
config
.
floatX
),
np
.
diag
(
np
.
array
([
1e-6
],
dtype
=
config
.
floatX
)),
np
.
diag
(
np
.
array
([
1e-6
],
dtype
=
config
.
floatX
)),
],
],
[
2
,
3
],
[
2
,
3
,
3
],
),
),
(
(
dirichlet
,
dirichlet
,
[
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
)],
[
np
.
array
([[
100
,
1
,
1
],
[
1
,
100
,
1
],
[
1
,
1
,
100
]],
dtype
=
config
.
floatX
)],
[
2
,
3
],
[
2
,
3
,
3
],
),
(
multinomial
,
[
np
.
array
([
10
,
20
],
dtype
=
"int64"
),
np
.
array
([[
0.999
,
0.001
],
[
0.001
,
0.999
]],
dtype
=
config
.
floatX
),
],
[
3
,
2
],
),
),
],
],
)
)
...
@@ -288,7 +297,7 @@ def test_local_rv_size_lift(dist_op, dist_params, size):
...
@@ -288,7 +297,7 @@ def test_local_rv_size_lift(dist_op, dist_params, size):
np
.
array
([[
-
1
,
20
],
[
300
,
-
4000
]],
dtype
=
config
.
floatX
),
np
.
array
([[
-
1
,
20
],
[
300
,
-
4000
]],
dtype
=
config
.
floatX
),
np
.
eye
(
2
)
.
astype
(
config
.
floatX
)
*
1e-6
,
np
.
eye
(
2
)
.
astype
(
config
.
floatX
)
*
1e-6
,
),
),
(
3
,),
(
3
,
2
),
1e-3
,
1e-3
,
),
),
],
],
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论