Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0c7720cf
提交
0c7720cf
authored
1月 20, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
3月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a Numba implementation of CategoricalRV
上级
1a5749d4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
97 行增加
和
0 行删除
+97
-0
random.py
aesara/link/numba/dispatch/random.py
+36
-0
test_numba.py
tests/link/test_numba.py
+61
-0
没有找到文件。
aesara/link/numba/dispatch/random.py
浏览文件 @
0c7720cf
...
@@ -297,3 +297,39 @@ def numba_funcify_BernoulliRV(op, node, **kwargs):
...
@@ -297,3 +297,39 @@ def numba_funcify_BernoulliRV(op, node, **kwargs):
body_fn
,
body_fn
,
{
"out_dtype"
:
out_dtype
,
"direct_cast"
:
numba_basic
.
direct_cast
},
{
"out_dtype"
:
out_dtype
,
"direct_cast"
:
numba_basic
.
direct_cast
},
)
)
@numba_funcify.register
(
aer
.
CategoricalRV
)
def
numba_funcify_CategoricalRV
(
op
,
node
,
**
kwargs
):
out_dtype
=
node
.
outputs
[
1
]
.
type
.
numpy_dtype
ind_shape_len
=
node
.
inputs
[
3
]
.
type
.
ndim
-
1
neg_ind_shape_len
=
-
ind_shape_len
size_len
=
int
(
get_vector_length
(
node
.
inputs
[
1
]))
@numba_basic.numba_njit
def
sampler
(
rng
,
size
,
dtype
,
p
):
size_tpl
=
numba_ndarray
.
to_fixed_tuple
(
size
,
size_len
)
ind_shape
=
p
.
shape
[:
-
1
]
if
ind_shape_len
>
0
:
if
size_len
>
0
and
size_tpl
[
neg_ind_shape_len
:]
!=
ind_shape
:
raise
ValueError
(
"Parameters shape and size do not match."
)
samples_shape
=
size_tpl
[:
neg_ind_shape_len
]
+
ind_shape
p_bcast
=
np
.
broadcast_to
(
p
,
size_tpl
[:
neg_ind_shape_len
]
+
p
.
shape
)
else
:
samples_shape
=
size_tpl
p_bcast
=
p
unif_samples
=
np
.
random
.
uniform
(
0
,
1
,
samples_shape
)
res
=
np
.
empty
(
samples_shape
,
dtype
=
out_dtype
)
for
idx
in
np
.
ndindex
(
*
samples_shape
):
res
[
idx
]
=
np
.
searchsorted
(
np
.
cumsum
(
p_bcast
[
idx
]),
unif_samples
[
idx
])
return
(
rng
,
res
)
return
sampler
tests/link/test_numba.py
浏览文件 @
0c7720cf
...
@@ -3056,6 +3056,67 @@ def test_RandomVariable(rv_op, dist_args, size):
...
@@ -3056,6 +3056,67 @@ def test_RandomVariable(rv_op, dist_args, size):
)
)
@pytest.mark.parametrize
(
"rv_op, dist_args, size, cm"
,
[
pytest
.
param
(
aer
.
categorical
,
[
set_test_value
(
at
.
dvector
(),
np
.
array
([
100000
,
1
,
1
],
dtype
=
np
.
float64
),
),
],
None
,
contextlib
.
suppress
(),
),
pytest
.
param
(
aer
.
categorical
,
[
set_test_value
(
at
.
dmatrix
(),
np
.
array
(
[[
100000
,
1
,
1
],
[
1
,
100000
,
1
],
[
1
,
1
,
100000
]],
dtype
=
np
.
float64
,
),
),
],
(
10
,
3
),
contextlib
.
suppress
(),
),
pytest
.
param
(
aer
.
categorical
,
[
set_test_value
(
at
.
dmatrix
(),
np
.
array
(
[[
100000
,
1
,
1
],
[
1
,
100000
,
1
],
[
1
,
1
,
100000
]],
dtype
=
np
.
float64
,
),
),
],
(
10
,
4
),
pytest
.
raises
(
ValueError
,
match
=
"Parameters shape.*"
),
),
],
ids
=
str
,
)
def
test_CategoricalRV
(
rv_op
,
dist_args
,
size
,
cm
):
rng
=
shared
(
np
.
random
.
RandomState
(
29402
))
g
=
rv_op
(
*
dist_args
,
size
=
size
,
rng
=
rng
)
g_fg
=
FunctionGraph
(
outputs
=
[
g
])
with
cm
:
compare_numba_and_py
(
g_fg
,
[
i
.
tag
.
test_value
for
i
in
g_fg
.
inputs
if
not
isinstance
(
i
,
(
SharedVariable
,
Constant
))
],
)
def
test_RandomState_updates
():
def
test_RandomState_updates
():
rng
=
shared
(
np
.
random
.
RandomState
(
1
))
rng
=
shared
(
np
.
random
.
RandomState
(
1
))
rng_new
=
shared
(
np
.
random
.
RandomState
(
2
))
rng_new
=
shared
(
np
.
random
.
RandomState
(
2
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论