• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.ops import composite as C
23
24def smoothl1loss(beta):
25    np.random.seed(42)
26    prediction = np.random.randn(20).astype(np.float32)
27    target = np.random.randn(20).astype(np.float32)
28
29    net = nn.SmoothL1Loss(beta)
30    return net(Tensor(prediction), Tensor(target))
31
32@pytest.mark.level0
33@pytest.mark.platform_x86_cpu
34@pytest.mark.env_onecard
35def test_smoothl1loss():
36    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
37
38    epsilon = 1e-6
39
40    beta = 1.0
41    loss = smoothl1loss(beta)
42    expect = [0.46941718, 0.00382918, 0.16829303, 2.447778, 0.04812113, 0.05953304,
43              2.2302065, 0.07672881, 0.00860204, 0.34798968, 0.00956192, 1.818008,
44              0.03262977, 0.36599946, 2.047463, 0.2168481, 0.7216947, 1.7739174,
45              0.08826803, 1.109165]
46    diff = np.absolute(loss.asnumpy() - np.array(expect))
47    assert(diff < epsilon).all()
48
49    beta = 1 / 9
50    loss = smoothl1loss(beta)
51    expect = [0.9133791, 0.03446258, 0.5246048, 2.8922224, 0.2546738, 0.289504,
52              2.674651, 0.33618113, 0.07560876, 0.7786982, 0.08273339, 2.2624524,
53              0.19990394, 0.8000138, 2.4919074, 0.6030006, 1.1661391, 2.2183619,
54              0.3646064, 1.5536094]
55    diff = np.absolute(loss.asnumpy() - np.array(expect))
56    assert(diff < epsilon).all()
57
58
59class Grad(nn.Cell):
60    def __init__(self, network):
61        super(Grad, self).__init__()
62        self.grad = C.GradOperation(get_all=True, sens_param=True)
63        self.network = network
64
65    def construct(self, x1, x2, sens):
66        gout = self.grad(self.network)(x1, x2, sens)
67        return gout
68
69
70def smoothl1loss_grad(beta):
71    np.random.seed(42)
72    prediction = np.random.randn(20).astype(np.float32)
73    target = np.random.randn(20).astype(np.float32)
74    sens = np.random.randn(20).astype(np.float32)
75
76    net = nn.SmoothL1Loss(beta)
77    grad = Grad(net)
78    return grad(Tensor(prediction), Tensor(target), Tensor(sens))
79
80@pytest.mark.level0
81@pytest.mark.platform_x86_cpu
82@pytest.mark.env_onecard
83def test_smoothl1loss_grad():
84    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
85
86    epsilon = 1e-6
87
88    beta = 1.0
89    dx = smoothl1loss_grad(beta)
90    dx1_expect = [-0.71552587, 0.01499678, -0.06709455, -0.30110368, -0.45868093,
91                  0.24838912, -0.46063876, 0.41411355, 0.04507046, -1.4708229,
92                  0.04481723, 0.38508227, -0.17292616, -0.52333146, -1.0309995,
93                  0.61330026, 0.83921754, -0.3092124, 0.1391843, -0.9755451]
94
95    dx2_expect = [0.71552587, -0.01499678, 0.06709455, 0.30110368, 0.45868093,
96                  -0.24838912, 0.46063876, -0.41411355, -0.04507046, 1.4708229,
97                  -0.04481723, -0.38508227, 0.17292616, 0.52333146, 1.0309995,
98                  -0.61330026, -0.83921754, 0.3092124, -0.1391843, 0.9755451]
99
100    diff1 = np.absolute(dx[0].asnumpy() - np.array(dx1_expect))
101    diff2 = np.absolute(dx[1].asnumpy() - np.array(dx2_expect))
102    assert(diff1 < epsilon).all()
103    assert(diff2 < epsilon).all()
104
105    beta = 1 / 9
106    dx = smoothl1loss_grad(beta)
107    dx1_expect = [-0.73846656, 0.13497104, -0.11564828, -0.30110368, -1.478522,
108                  0.7198442, -0.46063876, 1.0571222, 0.3436183, -1.7630402,
109                  0.32408398, 0.38508227, -0.676922, -0.6116763, -1.0309995,
110                  0.93128014, 0.83921754, -0.3092124, 0.33126342, -0.9755451]
111    dx2_expect = [0.73846656, -0.13497104, 0.11564828, 0.30110368, 1.478522,
112                  -0.7198442, 0.46063876, -1.0571222, -0.3436183, 1.7630402,
113                  -0.32408398, -0.38508227, 0.676922, 0.6116763, 1.0309995,
114                  -0.93128014, -0.83921754, 0.3092124, -0.33126342, 0.9755451]
115
116    diff1 = np.absolute(dx[0].asnumpy() - np.array(dx1_expect))
117    diff2 = np.absolute(dx[1].asnumpy() - np.array(dx2_expect))
118    assert(diff1 < epsilon).all()
119    assert(diff2 < epsilon).all()
120