• 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 L1Regularizer """
16import math
17import numpy as np
18import mindspore.nn as nn
19import mindspore.context as context
20from mindspore import Tensor, ms_function
21
22context.set_context(mode=context.GRAPH_MODE)
23
24
25class Net_l1_regularizer(nn.Cell):
26    def __init__(self, scale):
27        super(Net_l1_regularizer, self).__init__()
28        self.l1_regularizer = nn.L1Regularizer(scale)
29
30    @ms_function
31    def construct(self, weights):
32        return self.l1_regularizer(weights)
33
34
35def test_l1_regularizer02():
36    scale = 0.0
37    weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
38    try:
39        l1_regularizer = Net_l1_regularizer(scale)
40        l1_regularizer(weights)
41    except ValueError:
42        assert True
43
44
45def test_l1_regularizer03():
46    scale = -0.5
47    weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
48    try:
49        l1_regularizer = Net_l1_regularizer(scale)
50        l1_regularizer(weights)
51    except ValueError:
52        assert True
53
54
55def test_l1_regularizer04():
56    scale = math.inf
57    weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
58    try:
59        l1_regularizer = Net_l1_regularizer(scale)
60        l1_regularizer(weights)
61    except ValueError:
62        assert True
63
64
65def test_l1_regularizer05():
66    scale = math.nan
67    weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
68    try:
69        l1_regularizer = Net_l1_regularizer(scale)
70        l1_regularizer(weights)
71    except ValueError:
72        assert True
73
74
75def test_l1_regularizer06():
76    scale = 0.5
77    weights = "sss"
78    try:
79        l1_regularizer = Net_l1_regularizer(scale)
80        l1_regularizer(weights)
81    except TypeError:
82        assert True
83
84
85def test_l1_regularizer07():
86    scale = 0.5
87    try:
88        l1_regularizer = Net_l1_regularizer(scale)
89        l1_regularizer()
90    except TypeError:
91        assert True
92
93
94def test_l1_regularizer09():
95    scale = 0.5
96    weights = Tensor([[False, False], [False, False]])
97    try:
98        net = nn.L1Regularizer(scale)
99        net(weights)
100    except TypeError:
101        assert True
102