|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- # Copyright 2021 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.
- # ============================================================================
-
- import mindspore
- from mindspore.common.initializer import initializer, Identity, Dirac, Sparse, VarianceScaling, Orthogonal
- import numpy as np
-
-
- def test_sparse():
- """
- Feature: Test sparse initializer.
- Description: Initialize a 2 dimension sparse matrix to fill the input tensor.
- Expectation: The Tensor is initialized with a 2 dimension sparse matrix.
- """
- tensor1 = initializer(Sparse(sparsity=0.1, sigma=0.01), [5, 8], mindspore.float32)
- tensor1.init_data()
-
-
- def test_orthogonal():
- """
- Feature: Test orthogonal initializer.
- Description: Initialize a (semi) orthogonal matrix to fill the input tensor.
- Expectation: The Tensor is initialized with values from orthogonal matrix.
- """
- tensor1 = initializer(Orthogonal(gain=2.), [2, 3, 4], mindspore.float32)
- tensor2 = initializer('orthogonal', [2, 3, 4], mindspore.float32)
- tensor1.init_data()
- tensor2.init_data()
-
-
- def test_variancescaling():
- """
- Feature: Test varianceScaling initializer.
- Description: Randomly initialize an array with scaling to fill the input tensor.
- Expectation: The Tensor is initialized successfully.
- """
- tensor1 = initializer('varianceScaling', [2, 3], mindspore.float32)
- tensor2 = initializer(VarianceScaling(scale=1.0, mode='fan_out', distribution='untruncated_normal'), [2, 3],
- mindspore.float32)
- tensor3 = initializer(VarianceScaling(scale=2.0, mode='fan_in', distribution='truncated_normal'), [2, 3],
- mindspore.float32)
- tensor4 = initializer(VarianceScaling(scale=3.0, mode='fan_avg', distribution='uniform'), [2, 3],
- mindspore.float32)
- tensor1.init_data()
- tensor2.init_data()
- tensor3.init_data()
- tensor4.init_data()
-
-
- def test_identity():
- """
- Feature: Test identity initializer.
- Description: Initialize an identity matrix to fill a Tensor.
- Expectation: The Tensor is initialized with identity matrix.
- """
- tensor1 = initializer(Identity(), [3, 3], mindspore.float32)
- tensor2 = initializer('identity', [3, 4], mindspore.float32)
- tensor3 = initializer('identity', [4, 3], mindspore.float32)
- expect1 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32)
- expect2 = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]], dtype=np.float32)
- expect3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], dtype=np.float32)
- assert (tensor1.asnumpy() == expect1).all()
- assert (tensor2.asnumpy() == expect2).all()
- assert (tensor3.asnumpy() == expect3).all()
-
-
- def test_dirac():
- """
- Feature: Test dirac initializer.
- Description: Initialize input tensor with the Dirac delta function.
- Expectation: The Tensor is correctly initialized.
- """
- tensor3_1 = initializer(Dirac(groups=1), [6, 2, 3], mindspore.float32)
- tensor3_2 = initializer(Dirac(groups=2), [6, 2, 3], mindspore.float32)
- tensor3_3 = initializer(Dirac(groups=3), [6, 2, 3], mindspore.float32)
-
- tensor4_1 = initializer(Dirac(groups=1), [6, 4, 3, 3], mindspore.float32)
- tensor4_2 = initializer(Dirac(groups=2), [6, 4, 3, 3], mindspore.float32)
- tensor4_3 = initializer(Dirac(groups=3), [6, 4, 3, 3], mindspore.float32)
-
- tensor5_1 = initializer(Dirac(groups=1), [6, 2, 3, 3, 3], mindspore.float32)
- tensor5_2 = initializer(Dirac(groups=2), [6, 2, 3, 3, 3], mindspore.float32)
- tensor5_3 = initializer(Dirac(groups=3), [6, 2, 3, 3, 3], mindspore.float32)
-
- expectation3_1 = np.array([[[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]],
- [[0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.]]], dtype=np.float32)
-
- expectation3_2 = np.array([[[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]],
- [[0., 0., 0.], [0., 0., 0.]],
- [[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]],
- [[0., 0., 0.], [0., 0., 0.]]], dtype=np.float32)
-
- expectation3_3 = np.array([[[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]],
- [[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]],
- [[0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.]]], dtype=np.float32)
-
- expectation4_1 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
-
- expectation4_2 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
-
- expectation4_3 = np.array([[[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]], dtype=np.float32)
-
- expectation5_1 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
-
- expectation5_2 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
-
- expectation5_3 = np.array([[[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]],
- [[[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]],
- [[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 1., 0.], [0., 0., 0.]],
- [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]]]], dtype=np.float32)
-
- assert (tensor3_1.asnumpy() == expectation3_1).all()
- assert (tensor3_2.asnumpy() == expectation3_2).all()
- assert (tensor3_3.asnumpy() == expectation3_3).all()
-
- assert (tensor4_1.asnumpy() == expectation4_1).all()
- assert (tensor4_2.asnumpy() == expectation4_2).all()
- assert (tensor4_3.asnumpy() == expectation4_3).all()
-
- assert (tensor5_1.asnumpy() == expectation5_1).all()
- assert (tensor5_2.asnumpy() == expectation5_2).all()
- assert (tensor5_3.asnumpy() == expectation5_3).all()
|