1# Copyright 2020 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""" 16Test nn.probability.distribution.Geometric. 17""" 18import pytest 19 20import mindspore.nn as nn 21import mindspore.nn.probability.distribution as msd 22from mindspore import dtype 23from mindspore import Tensor 24 25 26def test_arguments(): 27 """ 28 Args passing during initialization. 29 """ 30 g = msd.Geometric() 31 assert isinstance(g, msd.Distribution) 32 g = msd.Geometric([0.1, 0.3, 0.5, 0.9], dtype=dtype.int32) 33 assert isinstance(g, msd.Distribution) 34 35 36def test_type(): 37 with pytest.raises(TypeError): 38 msd.Geometric([0.1], dtype=dtype.bool_) 39 40 41def test_name(): 42 with pytest.raises(TypeError): 43 msd.Geometric([0.1], name=1.0) 44 45 46def test_seed(): 47 with pytest.raises(TypeError): 48 msd.Geometric([0.1], seed='seed') 49 50 51def test_prob(): 52 """ 53 Invalid probability. 54 """ 55 with pytest.raises(ValueError): 56 msd.Geometric([-0.1], dtype=dtype.int32) 57 with pytest.raises(ValueError): 58 msd.Geometric([1.1], dtype=dtype.int32) 59 with pytest.raises(ValueError): 60 msd.Geometric([0.0], dtype=dtype.int32) 61 with pytest.raises(ValueError): 62 msd.Geometric([1.0], dtype=dtype.int32) 63 64 65class GeometricProb(nn.Cell): 66 """ 67 Geometric distribution: initialize with probs. 68 """ 69 70 def __init__(self): 71 super(GeometricProb, self).__init__() 72 self.g = msd.Geometric(0.5, dtype=dtype.int32) 73 74 def construct(self, value): 75 prob = self.g.prob(value) 76 log_prob = self.g.log_prob(value) 77 cdf = self.g.cdf(value) 78 log_cdf = self.g.log_cdf(value) 79 sf = self.g.survival_function(value) 80 log_sf = self.g.log_survival(value) 81 return prob + log_prob + cdf + log_cdf + sf + log_sf 82 83 84def test_geometric_prob(): 85 """ 86 Test probability functions: passing value through construct. 87 """ 88 net = GeometricProb() 89 value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32) 90 ans = net(value) 91 assert isinstance(ans, Tensor) 92 93 94class GeometricProb1(nn.Cell): 95 """ 96 Geometric distribution: initialize without probs. 97 """ 98 99 def __init__(self): 100 super(GeometricProb1, self).__init__() 101 self.g = msd.Geometric(dtype=dtype.int32) 102 103 def construct(self, value, probs): 104 prob = self.g.prob(value, probs) 105 log_prob = self.g.log_prob(value, probs) 106 cdf = self.g.cdf(value, probs) 107 log_cdf = self.g.log_cdf(value, probs) 108 sf = self.g.survival_function(value, probs) 109 log_sf = self.g.log_survival(value, probs) 110 return prob + log_prob + cdf + log_cdf + sf + log_sf 111 112 113def test_geometric_prob1(): 114 """ 115 Test probability functions: passing value/probs through construct. 116 """ 117 net = GeometricProb1() 118 value = Tensor([3, 4, 5, 6, 7], dtype=dtype.float32) 119 probs = Tensor([0.5], dtype=dtype.float32) 120 ans = net(value, probs) 121 assert isinstance(ans, Tensor) 122 123 124class GeometricKl(nn.Cell): 125 """ 126 Test class: kl_loss between Geometric distributions. 127 """ 128 129 def __init__(self): 130 super(GeometricKl, self).__init__() 131 self.g1 = msd.Geometric(0.7, dtype=dtype.int32) 132 self.g2 = msd.Geometric(dtype=dtype.int32) 133 134 def construct(self, probs_b, probs_a): 135 kl1 = self.g1.kl_loss('Geometric', probs_b) 136 kl2 = self.g2.kl_loss('Geometric', probs_b, probs_a) 137 return kl1 + kl2 138 139 140def test_kl(): 141 """ 142 Test kl_loss function. 143 """ 144 ber_net = GeometricKl() 145 probs_b = Tensor([0.3], dtype=dtype.float32) 146 probs_a = Tensor([0.7], dtype=dtype.float32) 147 ans = ber_net(probs_b, probs_a) 148 assert isinstance(ans, Tensor) 149 150 151class GeometricCrossEntropy(nn.Cell): 152 """ 153 Test class: cross_entropy of Geometric distribution. 154 """ 155 156 def __init__(self): 157 super(GeometricCrossEntropy, self).__init__() 158 self.g1 = msd.Geometric(0.3, dtype=dtype.int32) 159 self.g2 = msd.Geometric(dtype=dtype.int32) 160 161 def construct(self, probs_b, probs_a): 162 h1 = self.g1.cross_entropy('Geometric', probs_b) 163 h2 = self.g2.cross_entropy('Geometric', probs_b, probs_a) 164 return h1 + h2 165 166 167def test_cross_entropy(): 168 """ 169 Test cross_entropy between Geometric distributions. 170 """ 171 net = GeometricCrossEntropy() 172 probs_b = Tensor([0.3], dtype=dtype.float32) 173 probs_a = Tensor([0.7], dtype=dtype.float32) 174 ans = net(probs_b, probs_a) 175 assert isinstance(ans, Tensor) 176 177 178class GeometricBasics(nn.Cell): 179 """ 180 Test class: basic mean/sd/mode/entropy function. 181 """ 182 183 def __init__(self): 184 super(GeometricBasics, self).__init__() 185 self.g = msd.Geometric([0.3, 0.5], dtype=dtype.int32) 186 187 def construct(self): 188 mean = self.g.mean() 189 sd = self.g.sd() 190 var = self.g.var() 191 mode = self.g.mode() 192 entropy = self.g.entropy() 193 return mean + sd + var + mode + entropy 194 195 196def test_bascis(): 197 """ 198 Test mean/sd/mode/entropy functionality of Geometric distribution. 199 """ 200 net = GeometricBasics() 201 ans = net() 202 assert isinstance(ans, Tensor) 203 204 205class GeoConstruct(nn.Cell): 206 """ 207 Bernoulli distribution: going through construct. 208 """ 209 210 def __init__(self): 211 super(GeoConstruct, self).__init__() 212 self.g = msd.Geometric(0.5, dtype=dtype.int32) 213 self.g1 = msd.Geometric(dtype=dtype.int32) 214 215 def construct(self, value, probs): 216 prob = self.g('prob', value) 217 prob1 = self.g('prob', value, probs) 218 prob2 = self.g1('prob', value, probs) 219 return prob + prob1 + prob2 220 221 222def test_geo_construct(): 223 """ 224 Test probability function going through construct. 225 """ 226 net = GeoConstruct() 227 value = Tensor([0, 0, 0, 0, 0], dtype=dtype.float32) 228 probs = Tensor([0.5], dtype=dtype.float32) 229 ans = net(value, probs) 230 assert isinstance(ans, Tensor) 231