Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8036b142
提交
8036b142
authored
9月 21, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove unnecessary Subtensor dependencies in nonzero functions
上级
73d798ab
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
53 行增加
和
87 行删除
+53
-87
test_basic.py
tests/tensor/test_basic.py
+31
-41
basic.py
theano/tensor/basic.py
+22
-46
没有找到文件。
tests/tensor/test_basic.py
浏览文件 @
8036b142
...
@@ -3019,97 +3019,87 @@ class TestTriangle:
...
@@ -3019,97 +3019,87 @@ class TestTriangle:
class
TestNonzero
:
class
TestNonzero
:
@change_flags
(
compute_test_value
=
"raise"
)
def
test_nonzero
(
self
):
def
test_nonzero
(
self
):
def
check
(
m
):
def
check
(
m
):
m_symb
=
theano
.
tensor
.
tensor
(
m_symb
=
theano
.
tensor
.
tensor
(
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
)
)
m_symb
.
tag
.
test_value
=
m
f_tuple
=
function
([
m_symb
],
nonzero
(
m_symb
,
return_matrix
=
False
)
)
res_tuple_tt
=
nonzero
(
m_symb
,
return_matrix
=
False
)
f_matrix
=
function
([
m_symb
],
nonzero
(
m_symb
,
return_matrix
=
True
)
)
res_matrix_tt
=
nonzero
(
m_symb
,
return_matrix
=
True
)
assert
np
.
allclose
(
f_matrix
(
m
),
np
.
vstack
(
np
.
nonzero
(
m
)))
res_tuple
=
tuple
(
r
.
tag
.
test_value
for
r
in
res_tuple_tt
)
for
i
,
j
in
zip
(
f_tuple
(
m
),
np
.
nonzero
(
m
)):
res_matrix
=
res_matrix_tt
.
tag
.
test_value
assert
np
.
allclose
(
res_matrix
,
np
.
vstack
(
np
.
nonzero
(
m
)))
for
i
,
j
in
zip
(
res_tuple
,
np
.
nonzero
(
m
)):
assert
np
.
allclose
(
i
,
j
)
assert
np
.
allclose
(
i
,
j
)
rand0d
=
np
.
array
(
rand
())
rand0d
=
np
.
empty
(
())
with
pytest
.
raises
(
ValueError
):
with
pytest
.
raises
(
ValueError
):
check
(
rand0d
)
check
(
rand0d
)
rand1d
=
rand
(
8
)
rand1d
=
np
.
empty
((
8
,)
)
rand1d
[:
4
]
=
0
rand1d
[:
4
]
=
0
check
(
rand1d
)
check
(
rand1d
)
rand2d
=
rand
(
8
,
9
)
rand2d
=
np
.
empty
((
8
,
9
)
)
rand2d
[:
4
]
=
0
rand2d
[:
4
]
=
0
check
(
rand2d
)
check
(
rand2d
)
rand3d
=
rand
(
8
,
9
,
10
)
@change_flags
(
compute_test_value
=
"raise"
)
rand3d
[:
4
]
=
0
check
(
rand3d
)
rand4d
=
rand
(
8
,
9
,
10
,
11
)
rand4d
[:
4
]
=
0
check
(
rand4d
)
def
test_flatnonzero
(
self
):
def
test_flatnonzero
(
self
):
def
check
(
m
):
def
check
(
m
):
m_symb
=
theano
.
tensor
.
tensor
(
m_symb
=
theano
.
tensor
.
tensor
(
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
)
)
f
=
function
([
m_symb
],
flatnonzero
(
m_symb
))
m_symb
.
tag
.
test_value
=
m
result
=
f
(
m
)
res_tt
=
flatnonzero
(
m_symb
)
result
=
res_tt
.
tag
.
test_value
assert
np
.
allclose
(
result
,
np
.
flatnonzero
(
m
))
assert
np
.
allclose
(
result
,
np
.
flatnonzero
(
m
))
rand0d
=
np
.
array
(
rand
())
rand0d
=
np
.
empty
(
())
with
pytest
.
raises
(
ValueError
):
with
pytest
.
raises
(
ValueError
):
check
(
rand0d
)
check
(
rand0d
)
rand1d
=
rand
(
8
)
rand1d
=
np
.
empty
((
8
,)
)
rand1d
[:
4
]
=
0
rand1d
[:
4
]
=
0
check
(
rand1d
)
check
(
rand1d
)
rand2d
=
rand
(
8
,
9
)
rand2d
=
np
.
empty
((
8
,
9
)
)
rand2d
[:
4
]
=
0
rand2d
[:
4
]
=
0
check
(
rand2d
)
check
(
rand2d
)
rand3d
=
rand
(
8
,
9
,
10
)
@change_flags
(
compute_test_value
=
"raise"
)
rand3d
[:
4
]
=
0
check
(
rand3d
)
rand4d
=
rand
(
8
,
9
,
10
,
11
)
rand4d
[:
4
]
=
0
check
(
rand4d
)
def
test_nonzero_values
(
self
):
def
test_nonzero_values
(
self
):
def
check
(
m
):
def
check
(
m
):
m_symb
=
theano
.
tensor
.
tensor
(
m_symb
=
theano
.
tensor
.
tensor
(
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
dtype
=
m
.
dtype
,
broadcastable
=
(
False
,)
*
m
.
ndim
)
)
f
=
function
([
m_symb
],
nonzero_values
(
m_symb
))
m_symb
.
tag
.
test_value
=
m
result
=
f
(
m
)
res_tt
=
nonzero_values
(
m_symb
)
result
=
res_tt
.
tag
.
test_value
assert
np
.
allclose
(
result
,
m
[
np
.
nonzero
(
m
)])
assert
np
.
allclose
(
result
,
m
[
np
.
nonzero
(
m
)])
rand0d
=
rand
(
)
rand0d
=
np
.
empty
(()
)
with
pytest
.
raises
(
ValueError
):
with
pytest
.
raises
(
ValueError
):
check
(
rand0d
)
check
(
rand0d
)
rand1d
=
rand
(
8
)
rand1d
=
np
.
empty
((
8
,)
)
rand1d
[:
4
]
=
0
rand1d
[:
4
]
=
0
check
(
rand1d
)
check
(
rand1d
)
rand2d
=
rand
(
8
,
9
)
rand2d
=
np
.
empty
((
8
,
9
)
)
rand2d
[:
4
]
=
0
rand2d
[:
4
]
=
0
check
(
rand2d
)
check
(
rand2d
)
rand3d
=
rand
(
8
,
9
,
10
)
rand3d
[:
4
]
=
0
check
(
rand3d
)
rand4d
=
rand
(
8
,
9
,
10
,
11
)
rand4d
[:
4
]
=
0
check
(
rand4d
)
def
test_identity
():
def
test_identity
():
def
check
(
dtype
):
def
check
(
dtype
):
...
...
theano/tensor/basic.py
浏览文件 @
8036b142
...
@@ -2723,22 +2723,15 @@ class Nonzero(gof.Op):
...
@@ -2723,22 +2723,15 @@ class Nonzero(gof.Op):
"""
"""
Return the indices of the elements that are non-zero.
Return the indices of the elements that are non-zero.
Returns a matrix of shape (ndim, number of nonzero elements) such that
element (i,j) is the index in the ith dimension of the jth non-zero
element.
Note this is different than NumPy, which returns a tuple of arrays, one for
each dimension of the input array.
Parameters
Parameters
----------
----------
a
: array_like
a: array_like
Input array.
Input array.
Returns
Returns
-------
-------
matrix
indices: list
Matrix containing the indices of the non-zero elements of a
.
A list containing the indices of the non-zero elements of `a`
.
See Also
See Also
--------
--------
...
@@ -2754,20 +2747,17 @@ class Nonzero(gof.Op):
...
@@ -2754,20 +2747,17 @@ class Nonzero(gof.Op):
a
=
as_tensor_variable
(
a
)
a
=
as_tensor_variable
(
a
)
if
a
.
ndim
==
0
:
if
a
.
ndim
==
0
:
raise
ValueError
(
"Nonzero only supports non-scalar arrays."
)
raise
ValueError
(
"Nonzero only supports non-scalar arrays."
)
output
=
[
TensorType
(
dtype
=
"int64"
,
broadcastable
=
(
False
,
False
))()]
output
=
[
TensorType
(
dtype
=
"int64"
,
broadcastable
=
(
False
,))()
for
i
in
range
(
a
.
ndim
)
]
return
gof
.
Apply
(
self
,
[
a
],
output
)
return
gof
.
Apply
(
self
,
[
a
],
output
)
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
a
=
inp
[
0
]
a
=
inp
[
0
]
(
out
,)
=
out_
result_tuple
=
np
.
nonzero
(
a
)
result_tuple
=
np
.
nonzero
(
a
)
if
len
(
result_tuple
[
0
])
>
0
:
for
i
,
res
in
enumerate
(
result_tuple
):
result
=
np
.
vstack
(
result_tuple
)
out_
[
i
][
0
]
=
res
.
astype
(
"int64"
)
else
:
result
=
np
.
zeros
((
len
(
result_tuple
),
0
))
out
[
0
]
=
result
.
astype
(
"int64"
)
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
return
[
grad_undefined
(
self
,
0
,
inp
[
0
])]
return
[
grad_undefined
(
self
,
0
,
inp
[
0
])]
...
@@ -2809,22 +2799,23 @@ def nonzero(a, return_matrix=False):
...
@@ -2809,22 +2799,23 @@ def nonzero(a, return_matrix=False):
flattened input array.
flattened input array.
"""
"""
matrix_result
=
_nonzero
(
a
)
res
=
_nonzero
(
a
)
if
return_matrix
:
if
isinstance
(
res
,
list
)
:
re
turn
matrix_result
re
s
=
tuple
(
res
)
else
:
else
:
if
a
.
ndim
>
0
:
res
=
(
res
,)
tuple_result
=
tuple
([
matrix_result
[
i
]
for
i
in
range
(
a
.
ndim
)])
if
return_matrix
:
if
len
(
res
)
>
1
:
return
stack
(
res
,
0
)
elif
len
(
res
)
==
1
:
return
shape_padleft
(
res
[
0
])
else
:
else
:
tuple_result
=
tuple
([
matrix_result
[
0
]])
return
res
return
tuple_result
def
flatnonzero
(
a
):
def
flatnonzero
(
a
):
"""
"""Return a vector of indices that are non-zero in the flattened version of `a`.
Return a vector of indices that are non-zero in the flattened version of a.
This is equivalent to nonzero(a.flatten(), return_matrix=True)[0]
Parameters
Parameters
----------
----------
...
@@ -2845,26 +2836,11 @@ def flatnonzero(a):
...
@@ -2845,26 +2836,11 @@ def flatnonzero(a):
"""
"""
if
a
.
ndim
==
0
:
if
a
.
ndim
==
0
:
raise
ValueError
(
"Nonzero only supports non-scalar arrays."
)
raise
ValueError
(
"Nonzero only supports non-scalar arrays."
)
return
nonzero
(
a
.
flatten
(),
return_matrix
=
Tru
e
)[
0
]
return
nonzero
(
a
.
flatten
(),
return_matrix
=
Fals
e
)[
0
]
def
nonzero_values
(
a
):
def
nonzero_values
(
a
):
"""
"""Return a vector of non-zero elements contained in the input array.
Return a vector of non-zero elements contained in the input array.
The following behavior works to extract non-zero elements from an array
in NumPy but is *NOT* supported by Theano:
a[numpy.nonzero(a)]
Instead, the nonzero_values function or method should be used:
tensor.nonzero_values(a)
a.nonzero_values()
This is equivalent to the following:
a.flatten()[tensor.flatnonzero(a)]
Parameters
Parameters
----------
----------
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论