|
|
|
@ -24,10 +24,12 @@ from mindspore import dtype
|
|
|
|
|
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Prob(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: probability of categorical distribution.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Prob, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -35,13 +37,15 @@ class Prob(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.prob(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pmf():
|
|
|
|
|
"""
|
|
|
|
|
Test pmf.
|
|
|
|
|
"""
|
|
|
|
|
expect_pmf = [0.7, 0.3, 0.7, 0.3, 0.3]
|
|
|
|
|
pmf = Prob()
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 0, 1, 1]).astype(np.int32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 0, 1, 1]).astype(
|
|
|
|
|
np.int32), dtype=dtype.float32)
|
|
|
|
|
output = pmf(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy() - expect_pmf) < tol).all()
|
|
|
|
@ -51,6 +55,7 @@ class LogProb(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: log probability of categorical distribution.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(LogProb, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -58,21 +63,25 @@ class LogProb(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.log_prob(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_likelihood():
|
|
|
|
|
"""
|
|
|
|
|
Test log_pmf.
|
|
|
|
|
"""
|
|
|
|
|
expect_logpmf = np.log([0.7, 0.3, 0.7, 0.3, 0.3])
|
|
|
|
|
logprob = LogProb()
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 0, 1, 1]).astype(np.int32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 0, 1, 1]).astype(
|
|
|
|
|
np.int32), dtype=dtype.float32)
|
|
|
|
|
output = logprob(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy() - expect_logpmf) < tol).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class KL(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: kl_loss between categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(KL, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -80,6 +89,7 @@ class KL(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.kl_loss('Categorical', x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_kl_loss():
|
|
|
|
|
"""
|
|
|
|
|
Test kl_loss.
|
|
|
|
@ -89,10 +99,12 @@ def test_kl_loss():
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy()) < tol).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Sampling(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: sampling of categorical distribution.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Sampling, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.2, 0.1, 0.7], dtype=dtype.int32)
|
|
|
|
@ -101,6 +113,7 @@ class Sampling(nn.Cell):
|
|
|
|
|
def construct(self):
|
|
|
|
|
return self.c.sample(self.shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sample():
|
|
|
|
|
"""
|
|
|
|
|
Test sample.
|
|
|
|
@ -109,10 +122,12 @@ def test_sample():
|
|
|
|
|
sample = Sampling()
|
|
|
|
|
sample()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Basics(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: mean/var/mode of categorical distribution.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(Basics, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.2, 0.1, 0.7], dtype=dtype.int32)
|
|
|
|
@ -120,6 +135,7 @@ class Basics(nn.Cell):
|
|
|
|
|
def construct(self):
|
|
|
|
|
return self.c.mean(), self.c.var(), self.c.mode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_basics():
|
|
|
|
|
"""
|
|
|
|
|
Test mean/variance/mode.
|
|
|
|
@ -139,6 +155,7 @@ class CDF(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: cdf of categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(CDF, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -146,21 +163,25 @@ class CDF(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.cdf(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cdf():
|
|
|
|
|
"""
|
|
|
|
|
Test cdf.
|
|
|
|
|
"""
|
|
|
|
|
expect_cdf = [0.7, 0.7, 1, 0.7, 1]
|
|
|
|
|
x_ = Tensor(np.array([0, 0, 1, 0, 1]).astype(np.int32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([0, 0, 1, 0, 1]).astype(
|
|
|
|
|
np.int32), dtype=dtype.float32)
|
|
|
|
|
cdf = CDF()
|
|
|
|
|
output = cdf(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy() - expect_cdf) < tol).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogCDF(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: log cdf of categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(LogCDF, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -168,12 +189,14 @@ class LogCDF(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.log_cdf(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_logcdf():
|
|
|
|
|
"""
|
|
|
|
|
Test log_cdf.
|
|
|
|
|
"""
|
|
|
|
|
expect_logcdf = np.log([0.7, 0.7, 1, 0.7, 1])
|
|
|
|
|
x_ = Tensor(np.array([0, 0, 1, 0, 1]).astype(np.int32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([0, 0, 1, 0, 1]).astype(
|
|
|
|
|
np.int32), dtype=dtype.float32)
|
|
|
|
|
logcdf = LogCDF()
|
|
|
|
|
output = logcdf(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
@ -184,6 +207,7 @@ class SF(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: survival function of categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(SF, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -191,12 +215,14 @@ class SF(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.survival_function(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_survival():
|
|
|
|
|
"""
|
|
|
|
|
Test survival funciton.
|
|
|
|
|
Test survival function.
|
|
|
|
|
"""
|
|
|
|
|
expect_survival = [0.3, 0., 0., 0.3, 0.3]
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 1, 0, 0]).astype(np.int32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([0, 1, 1, 0, 0]).astype(
|
|
|
|
|
np.int32), dtype=dtype.float32)
|
|
|
|
|
sf = SF()
|
|
|
|
|
output = sf(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
@ -207,6 +233,7 @@ class LogSF(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: log survival function of categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(LogSF, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -214,21 +241,25 @@ class LogSF(nn.Cell):
|
|
|
|
|
def construct(self, x_):
|
|
|
|
|
return self.c.log_survival(x_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_survival():
|
|
|
|
|
"""
|
|
|
|
|
Test log survival funciton.
|
|
|
|
|
Test log survival function.
|
|
|
|
|
"""
|
|
|
|
|
expect_logsurvival = np.log([1., 0.3, 0.3, 0.3, 0.3])
|
|
|
|
|
x_ = Tensor(np.array([-2, 0, 0, 0.5, 0.5]).astype(np.float32), dtype=dtype.float32)
|
|
|
|
|
x_ = Tensor(np.array([-2, 0, 0, 0.5, 0.5]
|
|
|
|
|
).astype(np.float32), dtype=dtype.float32)
|
|
|
|
|
log_sf = LogSF()
|
|
|
|
|
output = log_sf(x_)
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy() - expect_logsurvival) < tol).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EntropyH(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: entropy of categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(EntropyH, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -236,6 +267,7 @@ class EntropyH(nn.Cell):
|
|
|
|
|
def construct(self):
|
|
|
|
|
return self.c.entropy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_entropy():
|
|
|
|
|
"""
|
|
|
|
|
Test entropy.
|
|
|
|
@ -247,10 +279,12 @@ def test_entropy():
|
|
|
|
|
tol = 1e-6
|
|
|
|
|
assert (np.abs(output.asnumpy() - expect_entropy) < tol).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CrossEntropy(nn.Cell):
|
|
|
|
|
"""
|
|
|
|
|
Test class: cross entropy between categorical distributions.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super(CrossEntropy, self).__init__()
|
|
|
|
|
self.c = msd.Categorical([0.7, 0.3], dtype=dtype.int32)
|
|
|
|
@ -262,6 +296,7 @@ class CrossEntropy(nn.Cell):
|
|
|
|
|
cross_entropy = self.c.cross_entropy('Categorical', x_)
|
|
|
|
|
return h_sum_kl - cross_entropy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cross_entropy():
|
|
|
|
|
"""
|
|
|
|
|
Test cross_entropy.
|
|
|
|
|