Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5d97ffa6
提交
5d97ffa6
authored
9月 15, 2022
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
9月 21, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement moveaxis
上级
0d698099
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
1 行删除
+68
-1
basic.py
aesara/tensor/basic.py
+50
-1
test_basic.py
tests/tensor/test_basic.py
+18
-0
没有找到文件。
aesara/tensor/basic.py
浏览文件 @
5d97ffa6
...
@@ -10,11 +10,14 @@ import warnings
...
@@ -10,11 +10,14 @@ import warnings
from
collections.abc
import
Sequence
from
collections.abc
import
Sequence
from
functools
import
partial
from
functools
import
partial
from
numbers
import
Number
from
numbers
import
Number
from
typing
import
Optional
,
Tuple
,
Union
from
typing
import
Optional
from
typing
import
Sequence
as
TypeSequence
from
typing
import
Tuple
,
Union
from
typing
import
cast
as
type_cast
from
typing
import
cast
as
type_cast
import
numpy
as
np
import
numpy
as
np
from
numpy.core.multiarray
import
normalize_axis_index
from
numpy.core.multiarray
import
normalize_axis_index
from
numpy.core.numeric
import
normalize_axis_tuple
import
aesara
import
aesara
import
aesara.scalar.sharedvar
import
aesara.scalar.sharedvar
...
@@ -3635,6 +3638,51 @@ def swapaxes(y, axis1, axis2):
...
@@ -3635,6 +3638,51 @@ def swapaxes(y, axis1, axis2):
return
y
.
dimshuffle
(
li
)
return
y
.
dimshuffle
(
li
)
def
moveaxis
(
a
:
Union
[
np
.
ndarray
,
TensorVariable
],
source
:
Union
[
int
,
TypeSequence
[
int
]],
destination
:
Union
[
int
,
TypeSequence
[
int
]],
)
->
TensorVariable
:
"""Move axes of a TensorVariable to new positions.
Other axes remain in their original order.
Parameters
----------
a
The TensorVariable whose axes should be reordered.
source
Original positions of the axes to move. These must be unique.
destination
Destination positions for each of the original axes. These must also be
unique.
Returns
-------
result
TensorVariable with moved axes.
"""
a
=
as_tensor_variable
(
a
)
source
=
normalize_axis_tuple
(
source
,
a
.
ndim
,
"source"
)
destination
=
normalize_axis_tuple
(
destination
,
a
.
ndim
,
"destination"
)
if
len
(
source
)
!=
len
(
destination
):
raise
ValueError
(
"`source` and `destination` arguments must have the same number of elements"
)
order
=
[
n
for
n
in
range
(
a
.
ndim
)
if
n
not
in
source
]
for
dest
,
src
in
sorted
(
zip
(
destination
,
source
)):
order
.
insert
(
dest
,
src
)
result
=
a
.
dimshuffle
(
order
)
return
result
def
choose
(
a
,
choices
,
mode
=
"raise"
):
def
choose
(
a
,
choices
,
mode
=
"raise"
):
"""
"""
Construct an array from an index array and a set of arrays to choose from.
Construct an array from an index array and a set of arrays to choose from.
...
@@ -4014,6 +4062,7 @@ __all__ = [
...
@@ -4014,6 +4062,7 @@ __all__ = [
"atleast_3d"
,
"atleast_3d"
,
"choose"
,
"choose"
,
"swapaxes"
,
"swapaxes"
,
"moveaxis"
,
"stacklists"
,
"stacklists"
,
"diag"
,
"diag"
,
"diagonal"
,
"diagonal"
,
...
...
tests/tensor/test_basic.py
浏览文件 @
5d97ffa6
...
@@ -60,6 +60,7 @@ from aesara.tensor.basic import (
...
@@ -60,6 +60,7 @@ from aesara.tensor.basic import (
join
,
join
,
make_vector
,
make_vector
,
mgrid
,
mgrid
,
moveaxis
,
nonzero
,
nonzero
,
nonzero_values
,
nonzero_values
,
ogrid
,
ogrid
,
...
@@ -3984,6 +3985,23 @@ class TestSwapaxes:
...
@@ -3984,6 +3985,23 @@ class TestSwapaxes:
assert
np
.
allclose
(
n_s
,
t_s
)
assert
np
.
allclose
(
n_s
,
t_s
)
def
test_moveaxis
():
x
=
at
.
zeros
((
3
,
4
,
5
))
tuple
(
moveaxis
(
x
,
0
,
-
1
)
.
shape
.
eval
())
==
(
4
,
5
,
3
)
tuple
(
moveaxis
(
x
,
-
1
,
0
)
.
shape
.
eval
())
==
(
5
,
3
,
4
)
tuple
(
moveaxis
(
x
,
[
0
,
1
],
[
-
1
,
-
2
])
.
shape
.
eval
())
==
(
5
,
4
,
3
)
tuple
(
moveaxis
(
x
,
[
0
,
1
,
2
],
[
-
1
,
-
2
,
-
3
])
.
shape
.
eval
())
==
(
5
,
4
,
3
)
def
test_moveaxis_error
():
x
=
at
.
zeros
((
3
,
4
,
5
))
with
pytest
.
raises
(
ValueError
,
match
=
"`source` and `destination` arguments must have the same number of elements"
,
):
moveaxis
(
x
,
[
0
,
1
],
0
)
class
TestChoose
(
utt
.
InferShapeTester
):
class
TestChoose
(
utt
.
InferShapeTester
):
op
=
staticmethod
(
choose
)
op
=
staticmethod
(
choose
)
op_class
=
Choose
op_class
=
Choose
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论