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 Geometric distribution""" 16import numpy as np 17from scipy import stats 18import mindspore.context as context 19import mindspore.nn as nn 20import mindspore.nn.probability.distribution as msd 21from mindspore import Tensor 22from mindspore import dtype 23 24context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 25 26 27class Prob(nn.Cell): 28 """ 29 Test class: probability of Geometric distribution. 30 """ 31 32 def __init__(self): 33 super(Prob, self).__init__() 34 self.g = msd.Geometric(0.7, dtype=dtype.int32) 35 36 def construct(self, x_): 37 return self.g.prob(x_) 38 39 40def test_pmf(): 41 """ 42 Test pmf. 43 """ 44 geom_benchmark = stats.geom(0.7) 45 expect_pmf = geom_benchmark.pmf([0, 1, 2, 3, 4]).astype(np.float32) 46 pdf = Prob() 47 x_ = Tensor(np.array([-1, 0, 1, 2, 3] 48 ).astype(np.float32), dtype=dtype.float32) 49 output = pdf(x_) 50 tol = 1e-6 51 assert (np.abs(output.asnumpy() - expect_pmf) < tol).all() 52 53 54class LogProb(nn.Cell): 55 """ 56 Test class: log probability of Geometric distribution. 57 """ 58 59 def __init__(self): 60 super(LogProb, self).__init__() 61 self.g = msd.Geometric(0.7, dtype=dtype.int32) 62 63 def construct(self, x_): 64 return self.g.log_prob(x_) 65 66 67def test_log_likelihood(): 68 """ 69 Test log_pmf. 70 """ 71 geom_benchmark = stats.geom(0.7) 72 expect_logpmf = geom_benchmark.logpmf([1, 2, 3, 4, 5]).astype(np.float32) 73 logprob = LogProb() 74 x_ = Tensor(np.array([0, 1, 2, 3, 4]).astype( 75 np.int32), dtype=dtype.float32) 76 output = logprob(x_) 77 tol = 1e-6 78 assert (np.abs(output.asnumpy() - expect_logpmf) < tol).all() 79 80 81class KL(nn.Cell): 82 """ 83 Test class: kl_loss between Geometric distributions. 84 """ 85 86 def __init__(self): 87 super(KL, self).__init__() 88 self.g = msd.Geometric(0.7, dtype=dtype.int32) 89 90 def construct(self, x_): 91 return self.g.kl_loss('Geometric', x_) 92 93 94def test_kl_loss(): 95 """ 96 Test kl_loss. 97 """ 98 probs1_a = 0.7 99 probs1_b = 0.5 100 probs0_a = 1 - probs1_a 101 probs0_b = 1 - probs1_b 102 expect_kl_loss = np.log(probs1_a / probs1_b) + \ 103 (probs0_a / probs1_a) * np.log(probs0_a / probs0_b) 104 kl_loss = KL() 105 output = kl_loss(Tensor([probs1_b], dtype=dtype.float32)) 106 tol = 1e-6 107 assert (np.abs(output.asnumpy() - expect_kl_loss) < tol).all() 108 109 110class Basics(nn.Cell): 111 """ 112 Test class: mean/sd/mode of Geometric distribution. 113 """ 114 115 def __init__(self): 116 super(Basics, self).__init__() 117 self.g = msd.Geometric([0.5, 0.5], dtype=dtype.int32) 118 119 def construct(self): 120 return self.g.mean(), self.g.sd(), self.g.mode() 121 122 123def test_basics(): 124 """ 125 Test mean/standard deviation/mode. 126 """ 127 basics = Basics() 128 mean, sd, mode = basics() 129 expect_mean = [1.0, 1.0] 130 expect_sd = np.sqrt(np.array([0.5, 0.5]) / np.square(np.array([0.5, 0.5]))) 131 expect_mode = [0.0, 0.0] 132 tol = 1e-6 133 assert (np.abs(mean.asnumpy() - expect_mean) < tol).all() 134 assert (np.abs(sd.asnumpy() - expect_sd) < tol).all() 135 assert (np.abs(mode.asnumpy() - expect_mode) < tol).all() 136 137 138class Sampling(nn.Cell): 139 """ 140 Test class: log probability of bernoulli distribution. 141 """ 142 143 def __init__(self, shape, seed=0): 144 super(Sampling, self).__init__() 145 self.g = msd.Geometric([0.7, 0.5], seed=seed, dtype=dtype.int32) 146 self.shape = shape 147 148 def construct(self, probs=None): 149 return self.g.sample(self.shape, probs) 150 151 152def test_sample(): 153 """ 154 Test sample. 155 """ 156 shape = (2, 3) 157 sample = Sampling(shape) 158 output = sample() 159 assert output.shape == (2, 3, 2) 160 161 162class CDF(nn.Cell): 163 """ 164 Test class: cdf of Geometric distribution. 165 """ 166 167 def __init__(self): 168 super(CDF, self).__init__() 169 self.g = msd.Geometric(0.7, dtype=dtype.int32) 170 171 def construct(self, x_): 172 return self.g.cdf(x_) 173 174 175def test_cdf(): 176 """ 177 Test cdf. 178 """ 179 geom_benchmark = stats.geom(0.7) 180 expect_cdf = geom_benchmark.cdf([0, 1, 2, 3, 4]).astype(np.float32) 181 x_ = Tensor(np.array([-1, 0, 1, 2, 3] 182 ).astype(np.int32), dtype=dtype.float32) 183 cdf = CDF() 184 output = cdf(x_) 185 tol = 1e-6 186 assert (np.abs(output.asnumpy() - expect_cdf) < tol).all() 187 188 189class LogCDF(nn.Cell): 190 """ 191 Test class: log cdf of Geometric distribution. 192 """ 193 194 def __init__(self): 195 super(LogCDF, self).__init__() 196 self.g = msd.Geometric(0.7, dtype=dtype.int32) 197 198 def construct(self, x_): 199 return self.g.log_cdf(x_) 200 201 202def test_logcdf(): 203 """ 204 Test log_cdf. 205 """ 206 geom_benchmark = stats.geom(0.7) 207 expect_logcdf = geom_benchmark.logcdf([1, 2, 3, 4, 5]).astype(np.float32) 208 x_ = Tensor(np.array([0, 1, 2, 3, 4]).astype( 209 np.int32), dtype=dtype.float32) 210 logcdf = LogCDF() 211 output = logcdf(x_) 212 tol = 1e-6 213 assert (np.abs(output.asnumpy() - expect_logcdf) < tol).all() 214 215 216class SF(nn.Cell): 217 """ 218 Test class: survial function of Geometric distribution. 219 """ 220 221 def __init__(self): 222 super(SF, self).__init__() 223 self.g = msd.Geometric(0.7, dtype=dtype.int32) 224 225 def construct(self, x_): 226 return self.g.survival_function(x_) 227 228 229def test_survival(): 230 """ 231 Test survival function. 232 """ 233 geom_benchmark = stats.geom(0.7) 234 expect_survival = geom_benchmark.sf([0, 1, 2, 3, 4]).astype(np.float32) 235 x_ = Tensor(np.array([-1, 0, 1, 2, 3] 236 ).astype(np.int32), dtype=dtype.float32) 237 sf = SF() 238 output = sf(x_) 239 tol = 1e-6 240 assert (np.abs(output.asnumpy() - expect_survival) < tol).all() 241 242 243class LogSF(nn.Cell): 244 """ 245 Test class: log survial function of Geometric distribution. 246 """ 247 248 def __init__(self): 249 super(LogSF, self).__init__() 250 self.g = msd.Geometric(0.7, dtype=dtype.int32) 251 252 def construct(self, x_): 253 return self.g.log_survival(x_) 254 255 256def test_log_survival(): 257 """ 258 Test log_survival function. 259 """ 260 geom_benchmark = stats.geom(0.7) 261 expect_logsurvival = geom_benchmark.logsf( 262 [0, 1, 2, 3, 4]).astype(np.float32) 263 x_ = Tensor(np.array([-1, 0, 1, 2, 3] 264 ).astype(np.float32), dtype=dtype.float32) 265 log_sf = LogSF() 266 output = log_sf(x_) 267 tol = 5e-6 268 assert (np.abs(output.asnumpy() - expect_logsurvival) < tol).all() 269 270 271class EntropyH(nn.Cell): 272 """ 273 Test class: entropy of Geometric distribution. 274 """ 275 276 def __init__(self): 277 super(EntropyH, self).__init__() 278 self.g = msd.Geometric(0.7, dtype=dtype.int32) 279 280 def construct(self): 281 return self.g.entropy() 282 283 284def test_entropy(): 285 """ 286 Test entropy. 287 """ 288 geom_benchmark = stats.geom(0.7) 289 expect_entropy = geom_benchmark.entropy().astype(np.float32) 290 entropy = EntropyH() 291 output = entropy() 292 tol = 1e-6 293 assert (np.abs(output.asnumpy() - expect_entropy) < tol).all() 294 295 296class CrossEntropy(nn.Cell): 297 """ 298 Test class: cross entropy between Geometric distributions. 299 """ 300 301 def __init__(self): 302 super(CrossEntropy, self).__init__() 303 self.g = msd.Geometric(0.7, dtype=dtype.int32) 304 305 def construct(self, x_): 306 entropy = self.g.entropy() 307 kl_loss = self.g.kl_loss('Geometric', x_) 308 h_sum_kl = entropy + kl_loss 309 ans = self.g.cross_entropy('Geometric', x_) 310 return h_sum_kl - ans 311 312 313def test_cross_entropy(): 314 """ 315 Test cross_entropy. 316 """ 317 cross_entropy = CrossEntropy() 318 prob = Tensor([0.5], dtype=dtype.float32) 319 diff = cross_entropy(prob) 320 tol = 1e-6 321 assert (np.abs(diff.asnumpy() - np.zeros(diff.shape)) < tol).all() 322