Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e40c8274
Unverified
提交
e40c8274
authored
9月 02, 2022
作者:
ltoniazzi
提交者:
GitHub
9月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tensor conversion to `flatnonzero`, `nonzero_values`, `tile`, `inverse_permutation`, and `diag`
上级
c2ed818e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
63 行增加
和
21 行删除
+63
-21
basic.py
aesara/tensor/basic.py
+25
-19
test_basic.py
tests/tensor/test_basic.py
+38
-2
没有找到文件。
aesara/tensor/basic.py
浏览文件 @
e40c8274
...
...
@@ -940,9 +940,10 @@ def flatnonzero(a):
nonzero_values : Return the non-zero elements of the input array
"""
if
a
.
ndim
==
0
:
_a
=
as_tensor_variable
(
a
)
if
_a
.
ndim
==
0
:
raise
ValueError
(
"Nonzero only supports non-scalar arrays."
)
return
nonzero
(
a
.
flatten
(),
return_matrix
=
False
)[
0
]
return
nonzero
(
_
a
.
flatten
(),
return_matrix
=
False
)[
0
]
def
nonzero_values
(
a
):
...
...
@@ -1324,9 +1325,10 @@ def identity_like(x, dtype: Optional[Union[str, np.generic, np.dtype]] = None):
tensor
tensor the shape of x with ones on main diagonal and zeroes elsewhere of type of dtype.
"""
_x
=
as_tensor_variable
(
x
)
if
dtype
is
None
:
dtype
=
x
.
dtype
return
eye
(
x
.
shape
[
0
],
x
.
shape
[
1
],
k
=
0
,
dtype
=
dtype
)
dtype
=
_
x
.
dtype
return
eye
(
_x
.
shape
[
0
],
_
x
.
shape
[
1
],
k
=
0
,
dtype
=
dtype
)
def
infer_broadcastable
(
shape
):
...
...
@@ -2773,8 +2775,9 @@ def tile(x, reps, ndim=None):
"""
from
aesara.tensor.math
import
ge
if
ndim
is
not
None
and
ndim
<
x
.
ndim
:
raise
ValueError
(
"ndim should be equal or larger than x.ndim"
)
_x
=
as_tensor_variable
(
x
)
if
ndim
is
not
None
and
ndim
<
_x
.
ndim
:
raise
ValueError
(
"ndim should be equal or larger than _x.ndim"
)
# If reps is a scalar, integer or vector, we convert it to a list.
if
not
isinstance
(
reps
,
(
list
,
tuple
)):
...
...
@@ -2799,8 +2802,8 @@ def tile(x, reps, ndim=None):
# assert that reps.shape[0] does not exceed ndim
offset
=
assert_op
(
offset
,
ge
(
offset
,
0
))
# if reps.ndim is less than x.ndim, we pad the reps with
# "1" so that reps will have the same ndim as x.
# if reps.ndim is less than
_
x.ndim, we pad the reps with
# "1" so that reps will have the same ndim as
_
x.
reps_
=
[
switch
(
i
<
offset
,
1
,
reps
[
i
-
offset
])
for
i
in
range
(
ndim
)]
reps
=
reps_
...
...
@@ -2817,17 +2820,17 @@ def tile(x, reps, ndim=None):
):
raise
ValueError
(
"elements of reps must be scalars of integer dtype"
)
# If reps.ndim is less than x.ndim, we pad the reps with
# "1" so that reps will have the same ndim as x
# If reps.ndim is less than
_
x.ndim, we pad the reps with
# "1" so that reps will have the same ndim as
_
x
reps
=
list
(
reps
)
if
ndim
is
None
:
ndim
=
builtins
.
max
(
len
(
reps
),
x
.
ndim
)
ndim
=
builtins
.
max
(
len
(
reps
),
_
x
.
ndim
)
if
len
(
reps
)
<
ndim
:
reps
=
[
1
]
*
(
ndim
-
len
(
reps
))
+
reps
_shape
=
[
1
]
*
(
ndim
-
x
.
ndim
)
+
[
x
.
shape
[
i
]
for
i
in
range
(
x
.
ndim
)]
_shape
=
[
1
]
*
(
ndim
-
_x
.
ndim
)
+
[
_x
.
shape
[
i
]
for
i
in
range
(
_
x
.
ndim
)]
alloc_shape
=
reps
+
_shape
y
=
alloc
(
x
,
*
alloc_shape
)
y
=
alloc
(
_
x
,
*
alloc_shape
)
shuffle_ind
=
np
.
arange
(
ndim
*
2
)
.
reshape
(
2
,
ndim
)
shuffle_ind
=
shuffle_ind
.
transpose
()
.
flatten
()
y
=
y
.
dimshuffle
(
*
shuffle_ind
)
...
...
@@ -3288,8 +3291,9 @@ def inverse_permutation(perm):
Each row of input should contain a permutation of the first integers.
"""
_perm
=
as_tensor_variable
(
perm
)
return
permute_row_elements
(
arange
(
perm
.
shape
[
-
1
],
dtype
=
perm
.
dtype
),
perm
,
inverse
=
True
arange
(
_perm
.
shape
[
-
1
],
dtype
=
_perm
.
dtype
),
_
perm
,
inverse
=
True
)
...
...
@@ -3575,12 +3579,14 @@ def diag(v, k=0):
"""
if
v
.
ndim
==
1
:
return
AllocDiag
(
k
)(
v
)
elif
v
.
ndim
>=
2
:
return
diagonal
(
v
,
offset
=
k
)
_v
=
as_tensor_variable
(
v
)
if
_v
.
ndim
==
1
:
return
AllocDiag
(
k
)(
_v
)
elif
_v
.
ndim
>=
2
:
return
diagonal
(
_v
,
offset
=
k
)
else
:
raise
ValueError
(
"
Input must has v.ndim >= 1
."
)
raise
ValueError
(
"
Number of dimensions of `v` must be greater than one
."
)
def
stacklists
(
arg
):
...
...
tests/tensor/test_basic.py
浏览文件 @
e40c8274
...
...
@@ -1023,6 +1023,12 @@ class TestNonzero:
rand2d
[:
4
]
=
0
check
(
rand2d
)
# Test passing a list
m
=
[
1
,
2
,
0
]
out
=
flatnonzero
(
m
)
f
=
function
([],
out
)
assert
np
.
array_equal
(
f
(),
np
.
flatnonzero
(
m
))
@config.change_flags
(
compute_test_value
=
"raise"
)
def
test_nonzero_values
(
self
):
def
check
(
m
):
...
...
@@ -1449,8 +1455,6 @@ class TestJoinAndSplit:
assert
(
out
==
want
)
.
all
()
# Pass a list to make sure `a` is converted to a
# TensorVariable by roll
a
=
[
1
,
2
,
3
,
4
,
5
,
6
]
b
=
roll
(
a
,
get_shift
(
2
))
want
=
np
.
array
([
5
,
6
,
1
,
2
,
3
,
4
])
...
...
@@ -2221,6 +2225,20 @@ def test_tile():
==
np
.
tile
(
x_
,
(
2
,
3
,
4
,
6
))
)
# Test passing a float
x
=
scalar
()
x_val
=
1.0
assert
np
.
array_equal
(
run_tile
(
x
,
x_val
,
(
2
,),
use_symbolic_reps
),
np
.
tile
(
x_val
,
(
2
,))
)
# Test when x is a list
x
=
matrix
()
x_val
=
[[
1.0
,
2.0
],
[
3.0
,
4.0
]]
assert
np
.
array_equal
(
run_tile
(
x
,
x_val
,
(
2
,),
use_symbolic_reps
),
np
.
tile
(
x_val
,
(
2
,))
)
# Test when reps is integer, scalar or vector.
# Test 1,2,3,4-dimensional cases.
# Test input x has the shape [2], [2, 4], [2, 4, 3], [2, 4, 3, 5].
...
...
@@ -2794,6 +2812,12 @@ class TestInversePermutation:
assert
np
.
all
(
p_val
[
inv_val
]
==
np
.
arange
(
10
))
assert
np
.
all
(
inv_val
[
p_val
]
==
np
.
arange
(
10
))
# Test passing a list
p
=
[
2
,
4
,
3
,
0
,
1
]
inv
=
at
.
inverse_permutation
(
p
)
f
=
aesara
.
function
([],
inv
)
assert
np
.
array_equal
(
f
(),
np
.
array
([
3
,
4
,
0
,
2
,
1
]))
def
test_dim2
(
self
):
# Test the inversion of several permutations at a time
# Each row of p is a different permutation to inverse
...
...
@@ -3449,6 +3473,12 @@ class TestDiag:
with
pytest
.
raises
(
ValueError
):
diag
(
xx
)
# Test passing a list
xx
=
[[
1
,
2
],
[
3
,
4
]]
g
=
diag
(
xx
)
f
=
function
([],
g
)
assert
np
.
array_equal
(
f
(),
np
.
diag
(
xx
))
def
test_infer_shape
(
self
):
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
...
...
@@ -4136,6 +4166,12 @@ def test_identity_like_dtype():
m_out_float
=
identity_like
(
m
,
dtype
=
np
.
float64
)
assert
m_out_float
.
dtype
==
"float64"
# Test passing list
m
=
[[
0
,
1
],
[
1
,
3
]]
out
=
at
.
identity_like
(
m
)
f
=
aesara
.
function
([],
out
)
assert
np
.
array_equal
(
f
(),
np
.
eye
(
2
))
def
test_atleast_Nd
():
ary1
=
dscalar
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论