Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
22f463c1
提交
22f463c1
authored
6月 21, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
6月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add take_along_axis function
上级
69dc7d15
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
90 行增加
和
0 行删除
+90
-0
basic.py
aesara/tensor/basic.py
+49
-0
test_basic.py
tests/tensor/test_basic.py
+41
-0
没有找到文件。
aesara/tensor/basic.py
浏览文件 @
22f463c1
...
@@ -15,6 +15,7 @@ from numbers import Number
...
@@ -15,6 +15,7 @@ from numbers import Number
from
typing
import
Dict
,
Tuple
,
Union
from
typing
import
Dict
,
Tuple
,
Union
import
numpy
as
np
import
numpy
as
np
from
numpy.core.multiarray
import
normalize_axis_index
import
aesara
import
aesara
import
aesara.scalar.sharedvar
import
aesara.scalar.sharedvar
...
@@ -4347,7 +4348,55 @@ def expand_dims(
...
@@ -4347,7 +4348,55 @@ def expand_dims(
return
a
.
reshape
(
shape
)
return
a
.
reshape
(
shape
)
def
_make_along_axis_idx
(
arr_shape
,
indices
,
axis
):
"""Take from `numpy.lib.shape_base`."""
# compute dimensions to iterate over
if
str
(
indices
.
dtype
)
not
in
int_dtypes
:
raise
IndexError
(
"`indices` must be an integer array"
)
shape_ones
=
(
1
,)
*
indices
.
ndim
dest_dims
=
list
(
range
(
axis
))
+
[
None
]
+
list
(
range
(
axis
+
1
,
indices
.
ndim
))
# build a fancy index, consisting of orthogonal aranges, with the
# requested index inserted at the right location
fancy_index
=
[]
for
dim
,
n
in
zip
(
dest_dims
,
arr_shape
):
if
dim
is
None
:
fancy_index
.
append
(
indices
)
else
:
ind_shape
=
shape_ones
[:
dim
]
+
(
-
1
,)
+
shape_ones
[
dim
+
1
:]
fancy_index
.
append
(
arange
(
n
)
.
reshape
(
ind_shape
))
return
tuple
(
fancy_index
)
def
take_along_axis
(
arr
,
indices
,
axis
=
0
):
"""Take values from the input array by matching 1d index and data slices.
This iterates over matching 1d slices oriented along the specified axis in
the index and data arrays, and uses the former to look up values in the
latter. These slices can be different lengths.
Functions returning an index along an axis, like `argsort` and
`argpartition`, produce suitable indices for this function.
"""
arr
=
as_tensor_variable
(
arr
)
indices
=
as_tensor_variable
(
indices
)
# normalize inputs
if
axis
is
None
:
arr
=
arr
.
flatten
()
axis
=
0
else
:
axis
=
normalize_axis_index
(
axis
,
arr
.
ndim
)
if
arr
.
ndim
!=
indices
.
ndim
:
raise
ValueError
(
"`indices` and `arr` must have the same number of dimensions"
)
# use the fancy index
return
arr
[
_make_along_axis_idx
(
arr
.
shape
,
indices
,
axis
)]
__all__
=
[
__all__
=
[
"take_along_axis"
,
"expand_dims"
,
"expand_dims"
,
"atleast_Nd"
,
"atleast_Nd"
,
"atleast_1d"
,
"atleast_1d"
,
...
...
tests/tensor/test_basic.py
浏览文件 @
22f463c1
...
@@ -4175,3 +4175,44 @@ def test_expand_dims():
...
@@ -4175,3 +4175,44 @@ def test_expand_dims():
exp_res
=
np
.
expand_dims
(
x_val
,
(
2
,
1
))
exp_res
=
np
.
expand_dims
(
x_val
,
(
2
,
1
))
res_val
=
aesara
.
function
([
x_at
],
res_at
)(
x_val
)
res_val
=
aesara
.
function
([
x_at
],
res_at
)(
x_val
)
assert
np
.
array_equal
(
exp_res
,
res_val
)
assert
np
.
array_equal
(
exp_res
,
res_val
)
class
TestTakeAlongAxis
:
@pytest.mark.parametrize
(
[
"shape"
,
"axis"
,
"samples"
],
(
((
1
,),
None
,
1
),
((
1
,),
-
1
,
10
),
((
3
,
2
,
1
),
-
1
,
1
),
((
3
,
2
,
1
),
0
,
10
),
((
3
,
2
,
1
),
-
1
,
10
),
),
ids
=
str
,
)
def
test_take_along_axis
(
self
,
shape
,
axis
,
samples
):
rng
=
np
.
random
.
default_rng
()
arr
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
indices_size
=
list
(
shape
)
indices_size
[
axis
or
0
]
=
samples
indices
=
rng
.
integers
(
low
=
0
,
high
=
shape
[
axis
or
0
],
size
=
indices_size
)
arr_in
=
aet
.
tensor
(
config
.
floatX
,
[
s
==
1
for
s
in
arr
.
shape
])
indices_in
=
aet
.
tensor
(
np
.
int64
,
[
s
==
1
for
s
in
indices
.
shape
])
out
=
aet
.
take_along_axis
(
arr_in
,
indices_in
,
axis
)
func
=
aesara
.
function
([
arr_in
,
indices_in
],
out
)
assert
np
.
allclose
(
np
.
take_along_axis
(
arr
,
indices
,
axis
=
axis
),
func
(
arr
,
indices
)
)
def
test_ndim_dtype_failures
(
self
):
arr
=
aet
.
tensor
(
config
.
floatX
,
[
False
]
*
2
)
indices
=
aet
.
tensor
(
np
.
int64
,
[
False
]
*
3
)
with
pytest
.
raises
(
ValueError
):
aet
.
take_along_axis
(
arr
,
indices
)
indices
=
aet
.
tensor
(
np
.
float64
,
[
False
]
*
2
)
with
pytest
.
raises
(
IndexError
):
aet
.
take_along_axis
(
arr
,
indices
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论