From 1ae473c1925544c0b43a0124402b7b9554cbff56 Mon Sep 17 00:00:00 2001 From: Xun Deng Date: Mon, 14 Dec 2020 23:31:24 -0500 Subject: [PATCH] improved the docs in the distribution classes --- .../nn/probability/distribution/bernoulli.py | 3 ++- mindspore/nn/probability/distribution/beta.py | 6 ++++-- .../probability/distribution/categorical.py | 2 +- .../nn/probability/distribution/cauchy.py | 4 ++-- .../probability/distribution/exponential.py | 2 +- .../nn/probability/distribution/gamma.py | 6 ++++-- .../nn/probability/distribution/geometric.py | 3 ++- .../nn/probability/distribution/gumbel.py | 6 ++++++ .../nn/probability/distribution/log_normal.py | 21 +++++++++++++++---- .../nn/probability/distribution/logistic.py | 4 ++-- .../nn/probability/distribution/poisson.py | 4 +--- .../nn/probability/distribution/uniform.py | 4 ++-- 12 files changed, 44 insertions(+), 21 deletions(-) diff --git a/mindspore/nn/probability/distribution/bernoulli.py b/mindspore/nn/probability/distribution/bernoulli.py index 28c4dff493..559d6b06fc 100644 --- a/mindspore/nn/probability/distribution/bernoulli.py +++ b/mindspore/nn/probability/distribution/bernoulli.py @@ -163,7 +163,8 @@ class Bernoulli(Distribution): @property def probs(self): """ - Return the probability of that the outcome is 1. + Return the probability of that the outcome is 1 + after casting to self.dtype. """ return self._probs diff --git a/mindspore/nn/probability/distribution/beta.py b/mindspore/nn/probability/distribution/beta.py index ff9ced01cd..6b5b68dca4 100644 --- a/mindspore/nn/probability/distribution/beta.py +++ b/mindspore/nn/probability/distribution/beta.py @@ -190,14 +190,16 @@ class Beta(Distribution): @property def concentration1(self): """ - Return the concentration1, also know as the alpha of the Beta distribution. + Return the concentration1, also know as the alpha of the Beta distribution, + after casting to self.dtype. """ return self._concentration1 @property def concentration0(self): """ - Return the concentration0, also know as the beta of the Beta distribution. + Return the concentration0, also know as the beta of the Beta distribution, + after casting to self.dtype. """ return self._concentration0 diff --git a/mindspore/nn/probability/distribution/categorical.py b/mindspore/nn/probability/distribution/categorical.py index 8229ae4028..7115e7a34e 100644 --- a/mindspore/nn/probability/distribution/categorical.py +++ b/mindspore/nn/probability/distribution/categorical.py @@ -190,7 +190,7 @@ class Categorical(Distribution): @property def probs(self): """ - Return the probability. + Return the probability after casting to self.dtype. """ return self._probs diff --git a/mindspore/nn/probability/distribution/cauchy.py b/mindspore/nn/probability/distribution/cauchy.py index d0229f14ef..0941f69e4c 100644 --- a/mindspore/nn/probability/distribution/cauchy.py +++ b/mindspore/nn/probability/distribution/cauchy.py @@ -181,14 +181,14 @@ class Cauchy(Distribution): @property def loc(self): """ - Return the location of the distribution. + Return the location of the distribution after casting to self.dtype. """ return self._loc @property def scale(self): """ - Return the scale of the distribution. + Return the scale of the distribution after casting to self.dtype. """ return self._scale diff --git a/mindspore/nn/probability/distribution/exponential.py b/mindspore/nn/probability/distribution/exponential.py index 00d72c49e2..13ba82db9d 100644 --- a/mindspore/nn/probability/distribution/exponential.py +++ b/mindspore/nn/probability/distribution/exponential.py @@ -167,7 +167,7 @@ class Exponential(Distribution): @property def rate(self): """ - Return `rate` of the distribution. + Return `rate` of the distribution after casting to self.dtype. """ return self._rate diff --git a/mindspore/nn/probability/distribution/gamma.py b/mindspore/nn/probability/distribution/gamma.py index 0bbb7e7015..0200356ec2 100644 --- a/mindspore/nn/probability/distribution/gamma.py +++ b/mindspore/nn/probability/distribution/gamma.py @@ -188,14 +188,16 @@ class Gamma(Distribution): @property def concentration(self): """ - Return the concentration, also know as the alpha of the Gamma distribution. + Return the concentration, also know as the alpha of the Gamma distribution, + after casting to self.dtype. """ return self._concentration @property def rate(self): """ - Return the rate, also know as the beta of the Gamma distribution. + Return the rate, also know as the beta of the Gamma distribution, + after casting to self.dtype. """ return self._rate diff --git a/mindspore/nn/probability/distribution/geometric.py b/mindspore/nn/probability/distribution/geometric.py index 4f52e5a0c3..14c6333beb 100644 --- a/mindspore/nn/probability/distribution/geometric.py +++ b/mindspore/nn/probability/distribution/geometric.py @@ -172,7 +172,8 @@ class Geometric(Distribution): @property def probs(self): """ - Return the probability of success of the Bernoulli trail. + Return the probability of success of the Bernoulli trail, + after casting to self.dtype. """ return self._probs diff --git a/mindspore/nn/probability/distribution/gumbel.py b/mindspore/nn/probability/distribution/gumbel.py index 4cf6ef28e7..8460b4c1ac 100644 --- a/mindspore/nn/probability/distribution/gumbel.py +++ b/mindspore/nn/probability/distribution/gumbel.py @@ -127,10 +127,16 @@ class Gumbel(TransformedDistribution): @property def loc(self): + """ + Return the location of the distribution after casting to self.dtype. + """ return self._loc @property def scale(self): + """ + Return the scale of the distribution after casting to self.dtype. + """ return self._scale def extend_repr(self): diff --git a/mindspore/nn/probability/distribution/log_normal.py b/mindspore/nn/probability/distribution/log_normal.py index 991efb8347..6e6824242b 100644 --- a/mindspore/nn/probability/distribution/log_normal.py +++ b/mindspore/nn/probability/distribution/log_normal.py @@ -165,27 +165,35 @@ class LogNormal(msd.TransformedDistribution): self.log_2pi = np.log(2 * np.pi) #ops needed for the class + self.dtypeop = P.DType() self.exp = exp_generic self.expm1 = P.Expm1() self.log = log_generic self.const = P.ScalarToArray() self.erf = P.Erf() self.fill = P.Fill() + self.greater = P.Greater() + self.select = P.Select() self.shape = P.Shape() self.sq = P.Square() self.sqrt = P.Sqrt() self.cast = P.Cast() self.squeeze = P.Squeeze(0) - self.zeroslike = P.ZerosLike() @property def loc(self): - """Distribution parameter for the pre-transformed mean.""" + """ + Distribution parameter for the pre-transformed mean + after casting to self.dtype. + """ return self._loc @property def scale(self): - """Distribution parameter for the pre-transformed standard deviation.""" + """ + Distribution parameter for the pre-transformed standard deviation + after casting to self.dtype. + """ return self._scale def _get_dist_type(self): @@ -254,7 +262,12 @@ class LogNormal(msd.TransformedDistribution): """ mean, sd = self._check_param_type(loc, scale) inverse_value = self.bijector("inverse", value) - return self.distribution("cdf", inverse_value, mean, sd) + cdf = self.distribution("cdf", inverse_value, mean, sd) + + # to increase numerical stability, set cdf = 0 when value <= 0 + zeros = self.fill(self.dtypeop(cdf), self.shape(cdf), 0.0) + + return self.select(self.greater(value, 0.), cdf, zeros) def _log_prob(self, value, loc=None, scale=None): r""" diff --git a/mindspore/nn/probability/distribution/logistic.py b/mindspore/nn/probability/distribution/logistic.py index f0b71ec77f..646bff2555 100644 --- a/mindspore/nn/probability/distribution/logistic.py +++ b/mindspore/nn/probability/distribution/logistic.py @@ -181,14 +181,14 @@ class Logistic(Distribution): @property def loc(self): """ - Return the location of the distribution. + Return the location of the distribution after casting to self.dtype. """ return self._loc @property def scale(self): """ - Return the scale of the distribution. + Return the scale of the distribution after casting to self.dtype. """ return self._scale diff --git a/mindspore/nn/probability/distribution/poisson.py b/mindspore/nn/probability/distribution/poisson.py index a543e5b4c0..589e2249f8 100644 --- a/mindspore/nn/probability/distribution/poisson.py +++ b/mindspore/nn/probability/distribution/poisson.py @@ -157,7 +157,7 @@ class Poisson(Distribution): @property def rate(self): """ - Return `rate` of the distribution. + Return `rate` of the distribution after casting to self.dtype. """ return self._rate @@ -212,7 +212,6 @@ class Poisson(Distribution): """ value = self._check_value(value, "value") value = self.cast(value, self.dtype) - value = self.floor(value) rate = self._check_param_type(rate) log_rate = self.log(rate) zeros = self.fill(self.dtypeop(value), self.shape(value), 0.0) @@ -240,7 +239,6 @@ class Poisson(Distribution): """ value = self._check_value(value, 'value') value = self.cast(value, self.dtype) - value = self.floor(value) rate = self._check_param_type(rate) zeros = self.fill(self.dtypeop(value), self.shape(value), 0.0) comp = self.less(value, zeros) diff --git a/mindspore/nn/probability/distribution/uniform.py b/mindspore/nn/probability/distribution/uniform.py index a09fc146a2..7099c76f84 100644 --- a/mindspore/nn/probability/distribution/uniform.py +++ b/mindspore/nn/probability/distribution/uniform.py @@ -181,14 +181,14 @@ class Uniform(Distribution): @property def low(self): """ - Return the lower bound of the distribution. + Return the lower bound of the distribution after casting to self.dtype. """ return self._low @property def high(self): """ - Return the upper bound of the distribution. + Return the upper bound of the distribution after casting to self.dtype.. """ return self._high