• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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""" test_unaryop """
16import numpy as np
17
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore import context
21
22context.set_context(mode=context.GRAPH_MODE)
23
24
25def test_scalar_usub():
26    class Net(nn.Cell):
27        def __init__(self, x):
28            super(Net, self).__init__()
29            self.x = x
30
31        def construct(self):
32            ret = -self.x
33            return ret
34
35    net = Net(-2)
36    assert net() == 2
37
38
39def test_tensor_usub():
40    class Net(nn.Cell):
41        def __init__(self):
42            super(Net, self).__init__()
43
44        def construct(self, x):
45            ret = -x
46            return ret
47
48    x = Tensor(np.ones([6, 8, 10], np.int32))
49    net = Net()
50    net(x)
51
52
53def test_scalar_uadd():
54    class Net(nn.Cell):
55        def __init__(self, x):
56            super(Net, self).__init__()
57            self.x = x
58
59        def construct(self):
60            ret = +self.x
61            return ret
62
63    net = Net(-2)
64    assert net() == -2
65