Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
87bc36c7
提交
87bc36c7
authored
7月 05, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
7月 07, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Work around squeeze bug in SciPy samplers
上级
2b78c67f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
72 行增加
和
24 行删除
+72
-24
basic.py
aesara/tensor/random/basic.py
+72
-24
没有找到文件。
aesara/tensor/random/basic.py
浏览文件 @
87bc36c7
import
abc
from
typing
import
List
,
Optional
,
Union
import
numpy
as
np
...
...
@@ -22,6 +23,53 @@ except ImportError: # pragma: no cover
raise
RuntimeError
(
"pypolygamma not installed!"
)
try
:
broadcast_shapes
=
np
.
broadcast_shapes
except
AttributeError
:
from
numpy.lib.stride_tricks
import
_broadcast_shape
def
broadcast_shapes
(
*
shapes
):
arrays
=
[
np
.
empty
(
x
,
dtype
=
[])
for
x
in
shapes
]
return
_broadcast_shape
(
arrays
)
class
ScipyRandomVariable
(
RandomVariable
):
r"""A class for `RandomVariable`\s that use SciPy-based samplers.
This will only work for `RandomVariable`\s for which the output shape is
entirely determined by broadcasting the distribution parameters (e.g. basic
scalar distributions).
The more sophisticated shape logic performed by `RandomVariable` is avoided
in order to reduce the amount of unnecessary extra steps taken to correct
SciPy's shape-removing defect.
"""
@classmethod
@abc.abstractmethod
def
rng_fn_scipy
(
cls
,
rng
,
*
args
,
**
kwargs
):
r"""
`RandomVariable`\s implementations that want to use SciPy-based samplers
need to implement this method instead of the base
`RandomVariable.rng_fn`; otherwise their broadcast dimensions will be
dropped by SciPy.
"""
@classmethod
def
rng_fn
(
cls
,
*
args
,
**
kwargs
):
size
=
args
[
-
1
]
res
=
cls
.
rng_fn_scipy
(
*
args
,
**
kwargs
)
return
np
.
broadcast_to
(
res
,
size
if
size
is
not
None
else
broadcast_shapes
(
*
[
np
.
shape
(
a
)
for
a
in
args
[
1
:
-
1
]]),
)
class
UniformRV
(
RandomVariable
):
name
=
"uniform"
ndim_supp
=
0
...
...
@@ -72,7 +120,7 @@ class NormalRV(RandomVariable):
normal
=
NormalRV
()
class
HalfNormalRV
(
RandomVariable
):
class
HalfNormalRV
(
Scipy
RandomVariable
):
name
=
"halfnormal"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -83,7 +131,7 @@ class HalfNormalRV(RandomVariable):
return
super
()
.
__call__
(
loc
,
scale
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
cls
,
rng
,
loc
,
scale
,
size
):
def
rng_fn
_scipy
(
cls
,
rng
,
loc
,
scale
,
size
):
return
stats
.
halfnorm
.
rvs
(
loc
,
scale
,
random_state
=
rng
,
size
=
size
)
...
...
@@ -104,7 +152,7 @@ class LogNormalRV(RandomVariable):
lognormal
=
LogNormalRV
()
class
GammaRV
(
RandomVariable
):
class
GammaRV
(
Scipy
RandomVariable
):
name
=
"gamma"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -115,7 +163,7 @@ class GammaRV(RandomVariable):
return
super
()
.
__call__
(
shape
,
1.0
/
rate
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
cls
,
rng
,
shape
,
scale
,
size
):
def
rng_fn
_scipy
(
cls
,
rng
,
shape
,
scale
,
size
):
return
stats
.
gamma
.
rvs
(
shape
,
scale
=
scale
,
size
=
size
,
random_state
=
rng
)
...
...
@@ -133,7 +181,7 @@ class ChiSquareRV(RandomVariable):
chisquare
=
ChiSquareRV
()
class
ParetoRV
(
RandomVariable
):
class
ParetoRV
(
Scipy
RandomVariable
):
name
=
"pareto"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -144,14 +192,14 @@ class ParetoRV(RandomVariable):
return
super
()
.
__call__
(
b
,
scale
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
cls
,
rng
,
b
,
scale
,
size
):
def
rng_fn
_scipy
(
cls
,
rng
,
b
,
scale
,
size
):
return
stats
.
pareto
.
rvs
(
b
,
scale
=
scale
,
size
=
size
,
random_state
=
rng
)
pareto
=
ParetoRV
()
class
GumbelRV
(
RandomVariable
):
class
GumbelRV
(
Scipy
RandomVariable
):
name
=
"gumbel"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -163,12 +211,12 @@ class GumbelRV(RandomVariable):
loc
:
Union
[
np
.
ndarray
,
float
],
scale
:
Union
[
np
.
ndarray
,
float
]
=
1.0
,
size
:
Optional
[
Union
[
List
[
int
],
int
]]
=
None
,
**
kwargs
**
kwargs
,
)
->
RandomVariable
:
return
super
()
.
__call__
(
loc
,
scale
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
def
rng_fn
_scipy
(
cls
,
rng
:
Union
[
np
.
random
.
Generator
,
np
.
random
.
RandomState
],
loc
:
Union
[
np
.
ndarray
,
float
],
...
...
@@ -356,7 +404,7 @@ class HyperGeometricRV(RandomVariable):
hypergeometric
=
HyperGeometricRV
()
class
CauchyRV
(
RandomVariable
):
class
CauchyRV
(
Scipy
RandomVariable
):
name
=
"cauchy"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -367,14 +415,14 @@ class CauchyRV(RandomVariable):
return
super
()
.
__call__
(
loc
,
scale
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
cls
,
rng
,
loc
,
scale
,
size
):
def
rng_fn
_scipy
(
cls
,
rng
,
loc
,
scale
,
size
):
return
stats
.
cauchy
.
rvs
(
loc
=
loc
,
scale
=
scale
,
random_state
=
rng
,
size
=
size
)
cauchy
=
CauchyRV
()
class
HalfCauchyRV
(
RandomVariable
):
class
HalfCauchyRV
(
Scipy
RandomVariable
):
name
=
"halfcauchy"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -385,14 +433,14 @@ class HalfCauchyRV(RandomVariable):
return
super
()
.
__call__
(
loc
,
scale
,
size
=
size
,
**
kwargs
)
@classmethod
def
rng_fn
(
cls
,
rng
,
loc
,
scale
,
size
):
def
rng_fn
_scipy
(
cls
,
rng
,
loc
,
scale
,
size
):
return
stats
.
halfcauchy
.
rvs
(
loc
=
loc
,
scale
=
scale
,
random_state
=
rng
,
size
=
size
)
halfcauchy
=
HalfCauchyRV
()
class
InvGammaRV
(
RandomVariable
):
class
InvGammaRV
(
Scipy
RandomVariable
):
name
=
"invgamma"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -400,7 +448,7 @@ class InvGammaRV(RandomVariable):
_print_name
=
(
"InvGamma"
,
"
\\
operatorname{Gamma^{-1}}"
)
@classmethod
def
rng_fn
(
cls
,
rng
,
shape
,
rate
,
size
=
Non
e
):
def
rng_fn
_scipy
(
cls
,
rng
,
shape
,
rate
,
siz
e
):
return
stats
.
invgamma
.
rvs
(
shape
,
scale
=
rate
,
size
=
size
,
random_state
=
rng
)
...
...
@@ -421,7 +469,7 @@ class WaldRV(RandomVariable):
wald
=
WaldRV
()
class
TruncExponentialRV
(
RandomVariable
):
class
TruncExponentialRV
(
Scipy
RandomVariable
):
name
=
"truncexpon"
ndim_supp
=
0
ndims_params
=
[
0
,
0
,
0
]
...
...
@@ -429,7 +477,7 @@ class TruncExponentialRV(RandomVariable):
_print_name
=
(
"TruncExp"
,
"
\\
operatorname{TruncExp}"
)
@classmethod
def
rng_fn
(
cls
,
rng
,
b
,
loc
,
scale
,
size
=
Non
e
):
def
rng_fn
_scipy
(
cls
,
rng
,
b
,
loc
,
scale
,
siz
e
):
return
stats
.
truncexpon
.
rvs
(
b
,
loc
=
loc
,
scale
=
scale
,
size
=
size
,
random_state
=
rng
)
...
...
@@ -438,7 +486,7 @@ class TruncExponentialRV(RandomVariable):
truncexpon
=
TruncExponentialRV
()
class
BernoulliRV
(
RandomVariable
):
class
BernoulliRV
(
Scipy
RandomVariable
):
name
=
"bernoulli"
ndim_supp
=
0
ndims_params
=
[
0
]
...
...
@@ -446,7 +494,7 @@ class BernoulliRV(RandomVariable):
_print_name
=
(
"Bern"
,
"
\\
operatorname{Bern}"
)
@classmethod
def
rng_fn
(
cls
,
rng
,
p
,
size
=
Non
e
):
def
rng_fn
_scipy
(
cls
,
rng
,
p
,
siz
e
):
return
stats
.
bernoulli
.
rvs
(
p
,
size
=
size
,
random_state
=
rng
)
...
...
@@ -475,7 +523,7 @@ class BinomialRV(RandomVariable):
binomial
=
BinomialRV
()
class
NegBinomialRV
(
RandomVariable
):
class
NegBinomialRV
(
Scipy
RandomVariable
):
name
=
"nbinom"
ndim_supp
=
0
ndims_params
=
[
0
,
0
]
...
...
@@ -483,14 +531,14 @@ class NegBinomialRV(RandomVariable):
_print_name
=
(
"NB"
,
"
\\
operatorname{NB}"
)
@classmethod
def
rng_fn
(
cls
,
rng
,
n
,
p
,
size
=
Non
e
):
def
rng_fn
_scipy
(
cls
,
rng
,
n
,
p
,
siz
e
):
return
stats
.
nbinom
.
rvs
(
n
,
p
,
size
=
size
,
random_state
=
rng
)
nbinom
=
NegBinomialRV
()
class
BetaBinomialRV
(
RandomVariable
):
class
BetaBinomialRV
(
Scipy
RandomVariable
):
name
=
"beta_binomial"
ndim_supp
=
0
ndims_params
=
[
0
,
0
,
0
]
...
...
@@ -498,7 +546,7 @@ class BetaBinomialRV(RandomVariable):
_print_name
=
(
"BetaBinom"
,
"
\\
operatorname{BetaBinom}"
)
@classmethod
def
rng_fn
(
cls
,
rng
,
n
,
a
,
b
,
size
=
Non
e
):
def
rng_fn
_scipy
(
cls
,
rng
,
n
,
a
,
b
,
siz
e
):
return
stats
.
betabinom
.
rvs
(
n
,
a
,
b
,
size
=
size
,
random_state
=
rng
)
...
...
@@ -535,7 +583,7 @@ class MultinomialRV(RandomVariable):
n
=
np
.
broadcast_to
(
n
,
size
+
n
.
shape
)
p
=
np
.
broadcast_to
(
p
,
size
+
p
.
shape
)
res
=
np
.
empty
(
p
.
shape
)
res
=
np
.
empty
(
p
.
shape
,
dtype
=
cls
.
dtype
)
for
idx
in
np
.
ndindex
(
p
.
shape
[:
-
1
]):
res
[
idx
]
=
rng
.
multinomial
(
n
[
idx
],
p
[
idx
])
return
res
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论