Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
4516b8fe
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看板
提交
4516b8fe
编写于
4月 22, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
4月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
!437 Add L2Loss op for VM.
Merge pull request !437 from liuxiao/add-l2-loss
上级
3b083d07
c874e2d4
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
101 addition
and
1 deletion
+101
-1
mindspore/ops/_grad/grad_nn_ops.py
mindspore/ops/_grad/grad_nn_ops.py
+11
-0
mindspore/ops/_op_impl/tbe/__init__.py
mindspore/ops/_op_impl/tbe/__init__.py
+1
-0
mindspore/ops/_op_impl/tbe/l2_loss.py
mindspore/ops/_op_impl/tbe/l2_loss.py
+44
-0
mindspore/ops/operations/__init__.py
mindspore/ops/operations/__init__.py
+2
-1
mindspore/ops/operations/nn_ops.py
mindspore/ops/operations/nn_ops.py
+35
-0
tests/ut/python/ops/test_ops.py
tests/ut/python/ops/test_ops.py
+8
-0
未找到文件。
mindspore/ops/_grad/grad_nn_ops.py
浏览文件 @
4516b8fe
...
...
@@ -456,6 +456,17 @@ def get_bprop_smooth_l1_loss(self):
return
bprop
@
bprop_getters
.
register
(
P
.
L2Loss
)
def
get_bprop_l2_loss
(
self
):
"""Grad definition for `L2Loss` operation."""
def
bprop
(
x
,
out
,
dout
):
dx
=
x
*
dout
return
(
dx
,)
return
bprop
@
bprop_getters
.
register
(
P
.
PReLU
)
def
get_bprop_prelu
(
self
):
"""Grad definition for `PReLU` operation."""
...
...
mindspore/ops/_op_impl/tbe/__init__.py
浏览文件 @
4516b8fe
...
...
@@ -117,6 +117,7 @@ from .layer_norm_beta_gamma_backprop import _layer_norm_beta_gamma_backprop_tbe
from
.layer_norm
import
_layer_norm_tbe
from
.layer_norm_grad
import
_layer_norm_grad_tbe
from
.layer_norm_x_backprop
import
_layer_norm_x_backprop_tbe
from
.l2_loss
import
_l2_loss_tbe
from
.square_sum_v1
import
_square_sum_v1_tbe
from
.square_sum_v2
import
_square_sum_v2_tbe
from
.confusion_transpose_d
import
_confusion_transpose_d_tbe
...
...
mindspore/ops/_op_impl/tbe/l2_loss.py
0 → 100644
浏览文件 @
4516b8fe
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""L2Loss op"""
from
mindspore.ops.op_info_register
import
op_info_register
,
TBERegOp
,
DataType
l2_loss_op_info
=
TBERegOp
(
"L2Loss"
)
\
.
fusion_type
(
"OPAQUE"
)
\
.
async_flag
(
False
)
\
.
binfile_name
(
"l2_loss.so"
)
\
.
compute_cost
(
10
)
\
.
kernel_name
(
"l2_loss"
)
\
.
partial_flag
(
True
)
\
.
input
(
0
,
"x"
,
None
,
"required"
,
None
)
\
.
output
(
0
,
"y"
,
True
,
"required"
,
"all"
)
\
.
dtype_format
(
DataType
.
F16_Default
,
DataType
.
F16_Default
)
\
.
dtype_format
(
DataType
.
F16_FracZ
,
DataType
.
F16_Default
)
\
.
dtype_format
(
DataType
.
F16_FracNZ
,
DataType
.
F16_Default
)
\
.
dtype_format
(
DataType
.
F16_5HD
,
DataType
.
F16_Default
)
\
.
dtype_format
(
DataType
.
F16_C1HWNCoC0
,
DataType
.
F16_Default
)
\
.
dtype_format
(
DataType
.
F32_Default
,
DataType
.
F32_Default
)
\
.
dtype_format
(
DataType
.
F32_FracZ
,
DataType
.
F32_Default
)
\
.
dtype_format
(
DataType
.
F32_FracNZ
,
DataType
.
F32_Default
)
\
.
dtype_format
(
DataType
.
F32_5HD
,
DataType
.
F32_Default
)
\
.
dtype_format
(
DataType
.
F32_C1HWNCoC0
,
DataType
.
F32_Default
)
\
.
get_op_info
()
@
op_info_register
(
l2_loss_op_info
)
def
_l2_loss_tbe
():
"""L2Loss TBE register"""
return
mindspore/ops/operations/__init__.py
浏览文件 @
4516b8fe
...
...
@@ -55,7 +55,7 @@ from .nn_ops import (LSTM, SGD, Adam, ApplyMomentum, BatchNorm,
DropoutDoMask
,
DropoutGenMask
,
Flatten
,
FusedBatchNorm
,
Gelu
,
Elu
,
GetNext
,
L2Normalize
,
LayerNorm
,
GetNext
,
L2Normalize
,
LayerNorm
,
L2Loss
,
LogSoftmax
,
MaxPool
,
ExtractImagePatches
,
AvgPool
,
Conv2DBackpropInput
,
...
...
@@ -167,6 +167,7 @@ __all__ = [
'FloatStatus'
,
'Reciprocal'
,
'SmoothL1Loss'
,
'L2Loss'
,
'ReduceAll'
,
'ScalarToArray'
,
'ScalarToTensor'
,
...
...
mindspore/ops/operations/nn_ops.py
浏览文件 @
4516b8fe
...
...
@@ -1332,6 +1332,41 @@ class SmoothL1Loss(PrimitiveWithInfer):
return
prediction
class
L2Loss
(
PrimitiveWithInfer
):
"""
Calculates half of the L2 norm of a tensor without using the `sqrt`.
Set `input_x` as x and output as loss.
.. math::
loss = sum(x ** 2) / 2
Inputs:
- **input_x** (Tensor) - A input Tensor.
Outputs:
Tensor. Has the same dtype as `input_x`. The output tensor is the value of loss which is a scalar tensor.
Examples
>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.float16)
>>> l2_loss = P.L2Loss()
>>> l2_loss(input_x)
7.0
"""
@
prim_attr_register
def
__init__
(
self
):
"""init L2Loss"""
def
infer_shape
(
self
,
input_x
):
loss_shape
=
[]
return
loss_shape
def
infer_dtype
(
self
,
x_type
):
validator
.
check_subclass
(
"x_type"
,
x_type
,
mstype
.
tensor
,
self
.
name
)
validator
.
check_tensor_type_same
({
'x_type'
:
x_type
},
[
mstype
.
double
,
mstype
.
float_
,
mstype
.
float16
],
self
.
name
)
return
x_type
class
SGD
(
PrimitiveWithInfer
):
"""
Computes stochastic gradient descent (optionally with momentum).
...
...
tests/ut/python/ops/test_ops.py
浏览文件 @
4516b8fe
...
...
@@ -871,6 +871,14 @@ test_case_nn_ops = [
'desc_inputs'
:
[[
3
,
3
],
[
3
,
3
],
[
3
,
3
],
[
3
,
3
],
[
3
,
3
]],
'desc_bprop'
:
[
3
,
3
],
'skip'
:
[
'backward'
]}),
(
'L2Loss_1'
,
{
'block'
:
P
.
L2Loss
(),
'desc_inputs'
:
[
Tensor
(
np
.
array
([
1
,
2
,
3
,
4
]),
mstype
.
float16
)],
'desc_bprop'
:
[]}),
(
'L2Loss_2'
,
{
'block'
:
P
.
L2Loss
(),
'desc_inputs'
:
[
Tensor
(
np
.
array
([[
1
,
1
],
[
2
,
2
],
[
3
,
3
],
[
4
,
4
]]),
mstype
.
float16
)],
'desc_bprop'
:
[]}),
]
test_case_array_ops
=
[
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录