Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3cacf1f7
提交
3cacf1f7
authored
6月 29, 2015
作者:
Iban Harlouchet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
flake8 for tensor/sort.py
上级
d40861ec
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
20 行增加
和
21 行删除
+20
-21
sort.py
theano/tensor/sort.py
+20
-20
test_flake8.py
theano/tests/test_flake8.py
+0
-1
没有找到文件。
theano/tensor/sort.py
浏览文件 @
3cacf1f7
...
...
@@ -28,7 +28,7 @@ class SortOp(theano.Op):
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
(
axis
is
None
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
axis
=
theano
.
Constant
(
theano
.
gof
.
generic
,
None
)
# axis=None flattens the array before sorting
out_type
=
tensor
(
dtype
=
input
.
dtype
,
broadcastable
=
[
False
])
...
...
@@ -45,7 +45,7 @@ class SortOp(theano.Op):
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
theano
.
Constant
)
and
node
.
inputs
[
1
]
.
data
is
None
):
node
.
inputs
[
1
]
.
data
is
None
):
# That means axis = None,
# So the array is flattened before being sorted
return
[(
mul
(
*
inputs_shapes
[
0
]),)]
...
...
@@ -64,16 +64,17 @@ class SortOp(theano.Op):
" matrix (and axis is None or 0) and tensor3"
)
if
a
.
ndim
==
1
:
idx
=
argsort
(
*
inputs
,
kind
=
self
.
kind
,
order
=
self
.
order
)
#
rev_idx = numpy.where(idx[None, :]==numpy.arange(5)[:,None])[1]
#
rev_idx = numpy.where(idx[None, :]==numpy.arange(5)[:,None])[1]
rev_idx
=
theano
.
tensor
.
eq
(
idx
[
None
,
:],
arange
(
a
.
shape
[
0
])[:,
None
])
.
nonzero
()[
1
]
inp_grad
=
output_grads
[
0
][
rev_idx
]
elif
a
.
ndim
==
2
:
if
(
axis
is
None
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
idx
=
argsort
(
*
inputs
,
kind
=
self
.
kind
,
order
=
self
.
order
)
rev_idx
=
theano
.
tensor
.
eq
(
idx
[
None
,
:],
arange
(
a
.
shape
[
0
]
*
a
.
shape
[
1
])[:,
None
])
.
nonzero
()[
1
]
rev_idx
=
theano
.
tensor
.
eq
(
idx
[
None
,
:],
arange
(
a
.
shape
[
0
]
*
a
.
shape
[
1
])[:,
None
])
.
nonzero
()[
1
]
inp_grad
=
output_grads
[
0
][
rev_idx
]
.
reshape
(
a
.
shape
)
elif
(
axis
==
0
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
==
0
)):
...
...
@@ -85,7 +86,7 @@ class SortOp(theano.Op):
indices
=
self
.
__get_argsort_indices
(
a
,
axis
)
inp_grad
=
output_grads
[
0
][
indices
[
0
],
indices
[
1
],
indices
[
2
]]
elif
(
axis
is
None
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
rev_idx
=
self
.
__get_argsort_indices
(
a
,
axis
)
inp_grad
=
output_grads
[
0
][
rev_idx
]
.
reshape
(
a
.
shape
)
axis_grad
=
theano
.
gradient
.
grad_undefined
(
...
...
@@ -103,13 +104,13 @@ class SortOp(theano.Op):
list of lenght len(a.shape) otherwise
"""
# The goal is to get gradient wrt input from gradient
# The goal is to get gradient wrt input from gradient
# wrt sort(input, axis)
idx
=
argsort
(
a
,
axis
,
kind
=
self
.
kind
,
order
=
self
.
order
)
# rev_idx is the reverse of previous argsort operation
rev_idx
=
argsort
(
idx
,
axis
,
kind
=
self
.
kind
,
order
=
self
.
order
)
# rev_idx is the reverse of previous argsort operation
rev_idx
=
argsort
(
idx
,
axis
,
kind
=
self
.
kind
,
order
=
self
.
order
)
if
(
axis
is
None
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
return
rev_idx
indices
=
[]
if
axis
.
data
>=
0
:
...
...
@@ -120,7 +121,7 @@ class SortOp(theano.Op):
if
i
==
axis_data
:
indices
.
append
(
rev_idx
)
else
:
index_shape
=
[
1
]
*
a
.
ndim
index_shape
=
[
1
]
*
a
.
ndim
index_shape
[
i
]
=
a
.
shape
[
i
]
# it's a way to emulate numpy.ogrid[0: a.shape[0], 0: a.shape[1], 0: a.shape[2]]
indices
.
append
(
theano
.
tensor
.
arange
(
a
.
shape
[
i
])
.
reshape
(
index_shape
))
...
...
@@ -178,28 +179,27 @@ class ArgSortOp(theano.Op):
return
hash
(
type
(
self
))
^
hash
(
self
.
order
)
^
hash
(
self
.
kind
)
def
__str__
(
self
):
return
(
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
)))
return
(
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
)))
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
(
axis
is
None
or
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
(
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
None
)):
axis
=
theano
.
Constant
(
theano
.
gof
.
generic
,
None
)
bcast
=
[
False
]
else
:
axis
=
theano
.
tensor
.
as_tensor_variable
(
axis
)
bcast
=
input
.
type
.
broadcastable
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
theano
.
tensor
.
TensorType
(
dtype
=
"int64"
,
broadcastable
=
bcast
)()])
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
theano
.
tensor
.
TensorType
(
dtype
=
"int64"
,
broadcastable
=
bcast
)()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
axis
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
theano
.
_asarray
(
np
.
argsort
(
a
,
axis
,
self
.
kind
,
self
.
order
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
z
[
0
]
=
theano
.
_asarray
(
np
.
argsort
(
a
,
axis
,
self
.
kind
,
self
.
order
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
theano
.
Constant
)
and
...
...
theano/tests/test_flake8.py
浏览文件 @
3cacf1f7
...
...
@@ -60,7 +60,6 @@ whitelist_flake8 = [
"tensor/blas_headers.py"
,
"tensor/type.py"
,
"tensor/fourier.py"
,
"tensor/sort.py"
,
"tensor/__init__.py"
,
"tensor/opt_uncanonicalize.py"
,
"tensor/blas.py"
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论