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 exp""" 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.Exp() 32 33 def construct(self, x_): 34 forward = self.bijector.forward(x_) 35 return forward 36 37def test_forward(): 38 x = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32) 39 tx = Tensor(x, dtype=dtype.float32) 40 forward = Net() 41 ans = forward(tx) 42 expected = np.exp(x) 43 tol = 1e-5 44 assert (np.abs(ans.asnumpy() - expected) < tol).all() 45 46class Net1(nn.Cell): 47 """ 48 Test class: inverse pass of bijector. 49 """ 50 def __init__(self): 51 super(Net1, self).__init__() 52 self.bijector = msb.Exp() 53 54 def construct(self, y_): 55 inverse = self.bijector.inverse(y_) 56 return inverse 57 58def test_inverse(): 59 y = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32) 60 ty = Tensor(y, dtype=dtype.float32) 61 inverse = Net1() 62 ans = inverse(ty) 63 expected = np.log(y) 64 tol = 1e-6 65 assert (np.abs(ans.asnumpy() - expected) < tol).all() 66 67class Net2(nn.Cell): 68 """ 69 Test class: Forward Jacobian. 70 """ 71 def __init__(self): 72 super(Net2, self).__init__() 73 self.bijector = msb.Exp() 74 75 def construct(self, x_): 76 return self.bijector.forward_log_jacobian(x_) 77 78def test_forward_jacobian(): 79 x = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32) 80 tx = Tensor(x, dtype=dtype.float32) 81 forward_jacobian = Net2() 82 ans = forward_jacobian(tx) 83 expected = x 84 tol = 1e-6 85 assert (np.abs(ans.asnumpy() - expected) < tol).all() 86 87class Net3(nn.Cell): 88 """ 89 Test class: Backward Jacobian. 90 """ 91 def __init__(self): 92 super(Net3, self).__init__() 93 self.bijector = msb.Exp() 94 95 def construct(self, y_): 96 return self.bijector.inverse_log_jacobian(y_) 97 98def test_inverse_jacobian(): 99 y = np.array([2.0, 3.0, 4.0, 5.0], dtype=np.float32) 100 ty = Tensor(y, dtype=dtype.float32) 101 inverse_jacobian = Net3() 102 ans = inverse_jacobian(ty) 103 expected = -np.log(y) 104 tol = 1e-6 105 assert (np.abs(ans.asnumpy() - expected) < tol).all() 106