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 Normal distribution""" 16import numpy as np 17import mindspore.context as context 18import mindspore.nn as nn 19import mindspore.nn.probability.distribution as msd 20from mindspore import Tensor 21from mindspore import dtype 22 23context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 24 25class Net1(nn.Cell): 26 """ 27 Test class: Normal distribution. `dist_spec_args` are `mean`, `sd`. 28 """ 29 def __init__(self): 30 super(Net1, self).__init__() 31 self.normal = msd.Normal(dtype=dtype.float32) 32 self.normal1 = msd.Normal(0.0, 1.0, dtype=dtype.float32) 33 self.normal2 = msd.Normal(3.0, 4.0, dtype=dtype.float32) 34 35 def construct(self, value, mean, sd, mean_a, sd_a): 36 args_list = self.normal.get_dist_args(mean, sd) 37 prob = self.normal1.prob(value, *args_list) 38 args_list1 = self.normal.get_dist_args() 39 prob1 = self.normal2.prob(value, *args_list1) 40 41 args_list2 = self.normal1.get_dist_args() 42 dist_type = self.normal1.get_dist_type() 43 kl_loss = self.normal2.kl_loss(dist_type, *args_list2) 44 45 args_list3 = self.normal.get_dist_args(mean_a, sd_a) 46 dist_type = self.normal1.get_dist_type() 47 kl_loss1 = self.normal2.kl_loss(dist_type, *args_list3) 48 return prob, prob1, kl_loss, kl_loss1 49 50def test1(): 51 """ 52 Test Normal with two `dist_spec_args`. 53 """ 54 net = Net1() 55 mean = Tensor(3.0, dtype=dtype.float32) 56 sd = Tensor(4.0, dtype=dtype.float32) 57 mean_a = Tensor(0.0, dtype=dtype.float32) 58 sd_a = Tensor(1.0, dtype=dtype.float32) 59 value = Tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) 60 ans, expected, ans1, expected1 = net(value, mean, sd, mean_a, sd_a) 61 tol = 1e-6 62 assert (np.abs(ans.asnumpy() - expected.asnumpy()) < tol).all() 63 assert (np.abs(ans1.asnumpy() - expected1.asnumpy()) < tol).all() 64 65class Net2(nn.Cell): 66 """ 67 Test class: Exponential distribution. `dist_spec_args` is `rate`. 68 """ 69 def __init__(self): 70 super(Net2, self).__init__() 71 self.expon = msd.Exponential(dtype=dtype.float32) 72 self.expon1 = msd.Exponential(1.0, dtype=dtype.float32) 73 self.expon2 = msd.Exponential(2.0, dtype=dtype.float32) 74 75 def construct(self, value, rate, rate1): 76 args_list = self.expon.get_dist_args(rate) 77 prob = self.expon1.prob(value, *args_list) 78 args_list1 = self.expon.get_dist_args() 79 prob1 = self.expon2.prob(value, *args_list1) 80 81 args_list2 = self.expon1.get_dist_args() 82 dist_type = self.expon1.get_dist_type() 83 kl_loss = self.expon2.kl_loss(dist_type, *args_list2) 84 85 args_list3 = self.expon.get_dist_args(rate1) 86 dist_type = self.expon.get_dist_type() 87 kl_loss1 = self.expon2.kl_loss(dist_type, *args_list3) 88 return prob, prob1, kl_loss, kl_loss1 89 90def test2(): 91 """ 92 Test Expomential with single `dist_spec_args`. 93 """ 94 net = Net2() 95 rate = Tensor(2.0, dtype=dtype.float32) 96 rate1 = Tensor(1.0, dtype=dtype.float32) 97 value = Tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) 98 ans, expected, ans1, expected1 = net(value, rate, rate1) 99 tol = 1e-6 100 assert (np.abs(ans.asnumpy() - expected.asnumpy()) < tol).all() 101 assert (np.abs(ans1.asnumpy() - expected1.asnumpy()) < tol).all() 102