1# Copyright 2019 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 cases for scalar affine""" 16import numpy as np 17import mindspore.context as context 18import mindspore.nn as nn 19import mindspore.nn.probability.bijector as msb 20from mindspore import Tensor 21from mindspore import dtype 22 23context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 24 25class Net(nn.Cell): 26 """ 27 Test class: forward pass of bijector. 28 """ 29 def __init__(self): 30 super(Net, self).__init__() 31 self.bijector = msb.ScalarAffine(scale=2.0, shift=1.0) 32 33 def construct(self, x_): 34 return self.bijector.forward(x_) 35 36def test_forward(): 37 forward = Net() 38 x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32) 39 ans = forward(Tensor(x, dtype=dtype.float32)) 40 tol = 1e-6 41 expected = 2 * x + 1 42 assert (np.abs(ans.asnumpy() - expected) < tol).all() 43 44class Net1(nn.Cell): 45 """ 46 Test class: backward pass of bijector. 47 """ 48 def __init__(self): 49 super(Net1, self).__init__() 50 self.bijector = msb.ScalarAffine(shift=1.0, scale=2.0) 51 52 def construct(self, x_): 53 return self.bijector.inverse(x_) 54 55def test_backward(): 56 backward = Net1() 57 x = np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32) 58 ans = backward(Tensor(x, dtype=dtype.float32)) 59 tol = 1e-6 60 expected = 0.5 * (x - 1.0) 61 assert (np.abs(ans.asnumpy() - expected) < tol).all() 62 63class Net2(nn.Cell): 64 """ 65 Test class: Forward Jacobian. 66 """ 67 def __init__(self): 68 super(Net2, self).__init__() 69 self.bijector = msb.ScalarAffine(shift=1.0, scale=2.0) 70 71 def construct(self, x_): 72 return self.bijector.forward_log_jacobian(x_) 73 74def test_forward_jacobian(): 75 forward_jacobian = Net2() 76 x = Tensor([2.0, 3.0, 4.0, 5.0], dtype=dtype.float32) 77 ans = forward_jacobian(x) 78 expected = np.log([2.0]) 79 tol = 1e-6 80 assert (np.abs(ans.asnumpy() - expected) < tol).all() 81 82class Net3(nn.Cell): 83 """ 84 Test class: Backward Jacobian. 85 """ 86 def __init__(self): 87 super(Net3, self).__init__() 88 self.bijector = msb.ScalarAffine(shift=1.0, scale=2.0) 89 90 def construct(self, x_): 91 return self.bijector.inverse_log_jacobian(x_) 92 93def test_backward_jacobian(): 94 backward_jacobian = Net3() 95 x = Tensor([2.0, 3.0, 4.0, 5.0], dtype=dtype.float32) 96 ans = backward_jacobian(x) 97 expected = np.log([0.5]) 98 tol = 1e-6 99 assert (np.abs(ans.asnumpy() - expected) < tol).all() 100