Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d82c903c
提交
d82c903c
authored
5月 15, 2015
作者:
Anatoly Belikov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implementing gradient for Sort op for tensor3
上级
51368a6b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
43 行增加
和
0 行删除
+43
-0
sort.py
theano/tensor/sort.py
+32
-0
test_sort.py
theano/tensor/tests/test_sort.py
+11
-0
没有找到文件。
theano/tensor/sort.py
浏览文件 @
d82c903c
...
...
@@ -80,11 +80,43 @@ class SortOp(theano.Op):
idx
=
argsort
(
*
inputs
,
kind
=
self
.
kind
,
order
=
self
.
order
)
# not working: numpy.where(idx[None, :]==numpy.arange(2)[:, None, None])
pass
elif
a
.
ndim
==
3
:
if
isinstance
(
axis
,
theano
.
Constant
)
and
axis
.
data
is
not
None
:
indices
=
self
.
__get_argsort_indices
(
a
,
axis
)
inp_grad
=
output_grads
[
0
]
.
reshape
(
a
.
shape
)[
indices
[
0
],
indices
[
1
],
indices
[
2
]]
elif
(
axis
is
None
or
(
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
(
self
,
1
,
axis
,
"sort is not defined for non-integer axes so"
" sort(x, axis+eps) is undefined"
)
return
[
inp_grad
,
axis_grad
]
def
__get_argsort_indices
(
self
,
a
,
axis
):
"""applies argsort to a along axis, returns indices which
can be used to sort original array"""
idx
=
argsort
(
a
,
axis
,
kind
=
self
.
kind
,
order
=
self
.
order
)
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
)):
return
rev_idx
indices
=
[]
if
axis
.
data
>
0
:
axis_data
=
axis
.
data
else
:
axis_data
=
a
.
ndim
+
axis
.
data
for
i
in
range
(
a
.
ndim
):
if
i
==
axis_data
:
indices
.
append
(
rev_idx
)
else
:
index_shape
=
[
1
]
*
a
.
ndim
index_shape
[
i
]
=
a
.
shape
[
i
]
indices
.
append
(
theano
.
tensor
.
arange
(
a
.
shape
[
i
])
.
reshape
(
index_shape
))
return
indices
"""
def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
...
...
theano/tensor/tests/test_sort.py
浏览文件 @
d82c903c
...
...
@@ -82,6 +82,17 @@ class test_sort(unittest.TestCase):
utt
.
verify_grad
(
lambda
x
:
sort
(
x
,
None
),
[
data
])
#utt.verify_grad(lambda x: sort(x, 0), [data])
#utt.verify_grad(lambda x: sort(x, 1), [data])
data
=
np
.
random
.
rand
(
2
,
3
,
2
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
lambda
x
:
sort
(
x
,
None
),
[
data
])
def
test_grad_negative_axis
(
self
):
data
=
np
.
random
.
rand
(
2
,
3
,
2
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
lambda
x
:
sort
(
x
,
-
1
),
[
data
])
data
=
np
.
random
.
rand
(
2
,
3
,
2
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
lambda
x
:
sort
(
x
,
-
3
),
[
data
])
data
=
np
.
random
.
rand
(
2
,
3
,
2
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
lambda
x
:
sort
(
x
,
-
2
),
[
data
])
data
=
np
.
random
.
rand
(
2
,
3
,
2
)
.
astype
(
theano
.
config
.
floatX
)
class
TensorInferShapeTester
(
utt
.
InferShapeTester
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论