Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
57ce3e5d
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
57ce3e5d
编写于
7月 30, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
7月 30, 2020
浏览文件
操作
浏览文件
下载
差异文件
!3711 fix topK multi dimention grad func
Merge pull request !3711 from fangzehua/topkgrad
上级
c700fc55
44f26cff
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
4 deletion
+48
-4
mindspore/ops/_grad/grad_nn_ops.py
mindspore/ops/_grad/grad_nn_ops.py
+48
-4
未找到文件。
mindspore/ops/_grad/grad_nn_ops.py
浏览文件 @
57ce3e5d
...
...
@@ -14,6 +14,7 @@
# ============================================================================
"""Define the grad rules of neural network related operations."""
import
math
import
numpy
as
np
from
mindspore.ops
import
_selected_grad_ops
as
SG
from
mindspore.ops.primitive
import
constexpr
...
...
@@ -628,19 +629,62 @@ def get_bprop_onehot(self):
return
bprop
@
constexpr
def
_range_op
(
start
,
limit
,
delta
,
dtype
):
"""helper function for Grad TopK"""
range_op
=
inner
.
Range
(
float
(
start
),
float
(
limit
),
float
(
delta
))
length_input
=
math
.
ceil
((
limit
-
start
)
/
delta
)
input_tensor
=
Tensor
(
list
(
range
(
length_input
)),
dtype
)
range_out
=
range_op
(
input_tensor
)
return
range_out
@
constexpr
def
_get_1d_shape
(
in_shape
):
"""helper function for Grad TopK"""
out_shape
=
1
for
i
in
in_shape
:
out_shape
*=
i
return
(
out_shape
,)
@
bprop_getters
.
register
(
P
.
TopK
)
def
get_bprop_top_kv2
(
self
):
"""Grad definition for `TopK` operation."""
scatter
=
P
.
ScatterNd
()
expand_dims
=
P
.
ExpandDims
()
shape_op
=
P
.
Shape
()
reshape_op
=
P
.
Reshape
()
dtype
=
P
.
DType
()
def
bprop
(
input_x
,
k
,
out
,
dout
):
# (n1, n2, ...., n_p), in_lastdim = n_p
in_shape
=
shape_op
(
input_x
)
in_lastdim
=
in_shape
[
-
1
]
# (n_1, ... n_(p-1), k), ind_lastdim = k
indices
=
out
[
1
]
indices
=
expand_dims
(
indices
,
-
1
)
updates
=
dout
[
0
]
shapes
=
shape_op
(
input_x
)
return
scatter
(
indices
,
updates
,
shapes
),
zeros_like
(
k
)
ind_shape
=
shape_op
(
indices
)
ind_lastdim
=
ind_shape
[
-
1
]
# (n_1*n_2..*n_(p-1), k), outerdim = n_1*n_2..*n_(p-1)
ind_2d
=
reshape_op
(
indices
,
(
-
1
,
ind_lastdim
))
outerdim
=
shape_op
(
ind_2d
)[
0
]
# [0, outterdim, 2*outerdim, ..., (k-1)*outerdim]
indices_dtype
=
dtype
(
indices
)
range_flatten_index
=
_range_op
(
0
,
outerdim
*
in_lastdim
,
in_lastdim
,
indices_dtype
)
# expand_dims to (k, 1), then broadcast
ind
=
reshape_op
(
ind_2d
+
expand_dims
(
range_flatten_index
,
-
1
),
(
-
1
,))
in_shape_1d
=
_get_1d_shape
(
in_shape
)
out_grad
=
reshape_op
(
scatter
(
expand_dims
(
ind
,
-
1
),
reshape_op
(
dout
[
0
],
(
-
1
,)),
in_shape_1d
),
in_shape
)
return
out_grad
,
zeros_like
(
k
)
return
bprop
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录