Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
482e6cc2
提交
482e6cc2
authored
1月 03, 2026
作者:
ricardoV94
提交者:
Ricardo Vieira
1月 03, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Speedup TestRavelMultiIndex
上级
f485be15
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
52 行增加
和
38 行删除
+52
-38
test_extra_ops.py
tests/tensor/test_extra_ops.py
+52
-38
没有找到文件。
tests/tensor/test_extra_ops.py
浏览文件 @
482e6cc2
...
...
@@ -1020,44 +1020,43 @@ class TestUnravelIndex(utt.InferShapeTester):
class
TestRavelMultiIndex
(
utt
.
InferShapeTester
):
def
test_ravel_multi_index
(
self
):
def
check
(
shape
,
index_ndim
,
mode
,
order
):
multi_index
=
np
.
unravel_index
(
np
.
arange
(
np
.
prod
(
shape
)),
shape
,
order
=
order
)
# create some invalid indices to test the mode
if
mode
in
(
"wrap"
,
"clip"
):
multi_index
=
(
multi_index
[
0
]
-
1
,
*
multi_index
[
1
:])
# test with scalars and higher-dimensional indices
if
index_ndim
==
0
:
multi_index
=
tuple
(
i
[
-
1
]
for
i
in
multi_index
)
elif
index_ndim
==
2
:
multi_index
=
tuple
(
i
[:,
np
.
newaxis
]
for
i
in
multi_index
)
@staticmethod
def
get_test_multiindex
(
shape
,
index_ndim
,
mode
,
order
):
multi_index
=
np
.
unravel_index
(
np
.
arange
(
np
.
prod
(
shape
)),
shape
,
order
=
order
)
# create some invalid indices to test the mode
if
mode
in
(
"wrap"
,
"clip"
):
multi_index
=
(
multi_index
[
0
]
-
1
,
*
multi_index
[
1
:])
# test with scalars and higher-dimensional indices
if
index_ndim
==
0
:
multi_index
=
tuple
(
i
[
-
1
]
for
i
in
multi_index
)
elif
index_ndim
==
2
:
multi_index
=
tuple
(
i
[:,
np
.
newaxis
]
for
i
in
multi_index
)
return
multi_index
def
test_eval
(
self
):
def
check_eval
(
shape
,
index_ndim
,
mode
,
order
):
multi_index
=
self
.
get_test_multiindex
(
shape
,
index_ndim
,
mode
,
order
)
multi_index_symb
=
[
pytensor
.
shared
(
i
)
for
i
in
multi_index
]
shape_symb
=
pytensor
.
shared
(
np
.
array
(
shape
))
out_symb
=
ravel_multi_index
(
multi_index_symb
,
shape_symb
,
mode
,
order
)
# reference result
res
=
out_symb
.
eval
()
ref
=
np
.
ravel_multi_index
(
multi_index
,
shape
,
mode
,
order
)
np
.
testing
.
assert_equal
(
ref
,
res
)
def
fn
(
mi
,
s
):
return
function
([],
ravel_multi_index
(
mi
,
s
,
mode
,
order
))
# shape given as a tuple
f_array_tuple
=
fn
(
multi_index
,
shape
)
f_symb_tuple
=
fn
(
multi_index_symb
,
shape
)
np
.
testing
.
assert_equal
(
ref
,
f_array_tuple
())
np
.
testing
.
assert_equal
(
ref
,
f_symb_tuple
())
# shape given as an array
shape_array
=
np
.
array
(
shape
)
f_array_array
=
fn
(
multi_index
,
shape_array
)
np
.
testing
.
assert_equal
(
ref
,
f_array_array
())
for
mode
in
(
"raise"
,
"wrap"
,
"clip"
):
for
order
in
(
"C"
,
"F"
):
for
index_ndim
in
(
0
,
1
,
2
):
check_eval
((
3
,),
index_ndim
,
mode
,
order
)
check_eval
((
3
,
4
),
index_ndim
,
mode
,
order
)
check_eval
((
3
,
4
,
5
),
index_ndim
,
mode
,
order
)
# shape given as an PyTensor variable
shape_symb
=
pytensor
.
shared
(
shape_array
)
f_array_symb
=
fn
(
multi_index
,
shape_symb
)
np
.
testing
.
assert_equal
(
ref
,
f_array_symb
())
def
test_shape
(
self
):
def
check_shape
(
shape
,
index_ndim
,
mode
,
order
):
multi_index
=
self
.
get_test_multiindex
(
shape
,
index_ndim
,
mode
,
order
)
# shape testing
shape_symb
=
pytensor
.
shared
(
np
.
array
(
shape
))
self
.
_compile_and_check
(
[],
[
ravel_multi_index
(
multi_index
,
shape_symb
,
mode
,
order
)],
...
...
@@ -1065,13 +1064,28 @@ class TestRavelMultiIndex(utt.InferShapeTester):
RavelMultiIndex
,
)
for
mode
in
(
"raise"
,
"wrap"
,
"clip"
):
for
order
in
(
"C"
,
"F"
):
for
index_ndim
in
(
0
,
1
,
2
):
check
((
3
,),
index_ndim
,
mode
,
order
)
check
((
3
,
4
),
index_ndim
,
mode
,
order
)
check
((
3
,
4
,
5
),
index_ndim
,
mode
,
order
)
for
index_ndim
in
(
0
,
1
,
2
):
check_shape
((
3
,),
index_ndim
,
"raise"
,
"C"
)
check_shape
((
3
,
4
),
index_ndim
,
"raise"
,
"C"
)
check_shape
((
3
,
4
,
5
),
index_ndim
,
"raise"
,
"C"
)
def
test_constant_inputs
(
self
):
shape
=
(
3
,)
multi_index
=
np
.
unravel_index
(
np
.
arange
(
np
.
prod
(
shape
)),
shape
)
ref
=
np
.
ravel_multi_index
(
multi_index
,
shape
)
# shape given as a tuple
f_tuple_shape
=
ravel_multi_index
(
multi_index
,
shape
)
.
eval
(
mode
=
"FAST_COMPILE"
)
np
.
testing
.
assert_equal
(
ref
,
f_tuple_shape
)
# shape given as an array
shape_array
=
np
.
array
(
shape
)
f_array_shape
=
ravel_multi_index
(
multi_index
,
shape_array
)
.
eval
(
mode
=
"FAST_COMPILE"
)
np
.
testing
.
assert_equal
(
ref
,
f_array_shape
)
def
test_errors
(
self
):
# must provide integers
with
pytest
.
raises
(
TypeError
):
ravel_multi_index
((
fvector
(),
ivector
()),
(
3
,
4
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论