def __init__(self, probs=None, seed=None, dtype=mstype.int32, name="Bernoulli"): """ Constructor of Bernoulli. """ param = dict(locals()) param['param_dict'] = {'probs': probs} valid_dtype = mstype.int_type + mstype.uint_type + mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Bernoulli, self).__init__(seed, dtype, name, param) self._probs = self._add_parameter(probs, 'probs') if self._probs is not None: check_prob(self.probs) # ops needed for the class self.exp = exp_generic self.log = log_generic self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.const = P.ScalarToArray() self.floor = P.Floor() self.fill = P.Fill() self.less = P.Less() self.shape = P.Shape() self.select = P.Select() self.uniform = C.uniform
def __init__(self, rate=None, seed=None, dtype=mstype.float32, name="Poisson"): """ Constructor of Poisson. """ param = dict(locals()) param['param_dict'] = {'rate': rate} valid_dtype = mstype.int_type + mstype.uint_type + mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Poisson, self).__init__(seed, dtype, name, param) self._rate = self._add_parameter(rate, 'rate') if self.rate is not None: check_greater_zero(self.rate, 'rate') # ops needed for the class self.exp = exp_generic self.log = log_generic self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.floor = P.Floor() self.dtypeop = P.DType() self.shape = P.Shape() self.fill = P.Fill() self.less = P.Less() self.equal = P.Equal() self.select = P.Select() self.lgamma = nn.LGamma() self.igamma = nn.IGamma() self.poisson = C.poisson
def _add_parameter(self, value, name): """ Cast `value` to a tensor and add it to `self.default_parameters`. Add `name` into and `self.parameter_names`. """ # initialize the attributes if they do not exist yet if not hasattr(self, 'default_parameters'): self.default_parameters = [] self.parameter_names = [] self.common_dtype = None # cast value to a tensor if it is not None if isinstance(value, bool) or value is None: raise TypeError(f"{name} cannot be type {type(value)}") value_t = Tensor(value) # if the bijector's dtype is not specified if self.dtype is None: if self.common_dtype is None: self.common_dtype = value_t.dtype elif value_t.dtype != self.common_dtype: raise TypeError( f"{name} should have the same dtype as other arguments.") # check if the parameters are casted into float-type tensors validator.check_type_name( f"dtype of {name}", value_t.dtype, mstype.float_type, type(self).__name__) # check if the dtype of the input_parameter agrees with the bijector's dtype elif value_t.dtype != self.dtype: raise TypeError( f"{name} should have the same dtype as the bijector's dtype.") self.default_parameters += [value,] self.parameter_names += [name,] return value_t
def __init__(self, rate=None, seed=None, dtype=mstype.float32, name="Exponential"): """ Constructor of Exponential. """ param = dict(locals()) param['param_dict'] = {'rate': rate} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Exponential, self).__init__(seed, dtype, name, param) self._rate = self._add_parameter(rate, 'rate') if self.rate is not None: check_greater_zero(self.rate, 'rate') self.minval = np.finfo(np.float).tiny # ops needed for the class self.exp = exp_generic self.log = log_generic self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.const = P.ScalarToArray() self.dtypeop = P.DType() self.fill = P.Fill() self.less = P.Less() self.select = P.Select() self.shape = P.Shape() self.uniform = C.uniform
def __init__(self, mean=None, sd=None, seed=None, dtype=mstype.float32, name="Normal"): """ Constructor of Normal. """ param = dict(locals()) param['param_dict'] = {'mean': mean, 'sd': sd} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Normal, self).__init__(seed, dtype, name, param) self._mean_value = self._add_parameter(mean, 'mean') self._sd_value = self._add_parameter(sd, 'sd') if self._sd_value is not None: check_greater_zero(self._sd_value, "Standard deviation") # ops needed for the class self.exp = exp_generic self.expm1 = P.Expm1() self.log = log_generic self.erf = P.Erf() self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.const = P.ScalarToArray() self.shape = P.Shape() self.sq = P.Square() self.sqrt = P.Sqrt()
def __init__(self, loc, scale, seed=0, dtype=mstype.float32, name="Gumbel"): """ Constructor of Gumbel distribution. """ valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) gumbel_cdf = msb.GumbelCDF(loc, scale) super(Gumbel, self).__init__( distribution=msd.Uniform(0.0, 1.0, dtype=dtype), bijector=msb.Invert(gumbel_cdf), seed=seed, name=name) # overwrite default_parameters and parameter_names self._reset_parameters() self._loc = self._add_parameter(loc, 'loc') self._scale = self._add_parameter(scale, 'scale') self._gumbel_bijector = gumbel_cdf # ops needed for the class self.cast = P.Cast() self.const = P.ScalarToArray() self.exp = exp_generic self.expm1 = expm1_generic self.fill = P.Fill() self.lgamma = nn.LGamma() self.log = log_generic self.shape = P.Shape() self.sqrt = P.Sqrt()
def __init__(self, probs=None, seed=None, dtype=mstype.int32, name="Categorical"): param = dict(locals()) param['param_dict'] = {'probs': probs} valid_dtype = mstype.uint_type + mstype.int_type + mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Categorical, self).__init__(seed, dtype, name, param) self._probs = self._add_parameter(probs, 'probs') if self.probs is not None: check_rank(self.probs) check_prob(self.probs) check_sum_equal_one(probs) # update is_scalar_batch and broadcast_shape # drop one dimension if self.probs.shape[:-1] == (): self._is_scalar_batch = True self._broadcast_shape = self._broadcast_shape[:-1] self.argmax = P.ArgMaxWithValue(axis=-1) self.broadcast = broadcast_to self.cast = P.Cast() self.clip_by_value = C.clip_by_value self.concat = P.Concat(-1) self.cumsum = P.CumSum() self.dtypeop = P.DType() self.exp = exp_generic self.expand_dim = P.ExpandDims() self.fill = P.Fill() self.gather = P.GatherNd() self.greater = P.Greater() self.issubclass = P.IsSubClass() self.less = P.Less() self.log = log_generic self.log_softmax = P.LogSoftmax() self.logicor = P.LogicalOr() self.logicand = P.LogicalAnd() self.multinomial = P.Multinomial(seed=self.seed) self.reshape = P.Reshape() self.reduce_sum = P.ReduceSum(keep_dims=True) self.select = P.Select() self.shape = P.Shape() self.softmax = P.Softmax() self.squeeze = P.Squeeze() self.squeeze_first_axis = P.Squeeze(0) self.squeeze_last_axis = P.Squeeze(-1) self.square = P.Square() self.transpose = P.Transpose() self.is_nan = P.IsNan() self.index_type = mstype.int32 self.nan = np.nan
def __init__(self, concentration1=None, concentration0=None, seed=None, dtype=mstype.float32, name="Beta"): """ Constructor of Beta. """ param = dict(locals()) param['param_dict'] = { 'concentration1': concentration1, 'concentration0': concentration0 } valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) # As some operators can't accept scalar input, check the type here if isinstance(concentration0, float): raise TypeError("Input concentration0 can't be scalar") if isinstance(concentration1, float): raise TypeError("Input concentration1 can't be scalar") super(Beta, self).__init__(seed, dtype, name, param) self._concentration1 = self._add_parameter(concentration1, 'concentration1') self._concentration0 = self._add_parameter(concentration0, 'concentration0') if self._concentration1 is not None: check_greater_zero(self._concentration1, "concentration1") if self._concentration0 is not None: check_greater_zero(self._concentration0, "concentration0") # ops needed for the class self.log = log_generic self.log1p = P.Log1p() self.neg = P.Neg() self.pow = P.Pow() self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.fill = P.Fill() self.shape = P.Shape() self.select = P.Select() self.logicaland = P.LogicalAnd() self.greater = P.Greater() self.digamma = nn.DiGamma() self.lbeta = nn.LBeta()
def __init__(self, loc=None, scale=None, seed=None, dtype=mstype.float32, name="Logistic"): """ Constructor of Logistic. """ param = dict(locals()) param['param_dict'] = {'loc': loc, 'scale': scale} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Logistic, self).__init__(seed, dtype, name, param) self._loc = self._add_parameter(loc, 'loc') self._scale = self._add_parameter(scale, 'scale') if self._scale is not None: check_greater_zero(self._scale, "scale") # ops needed for the class self.cast = P.Cast() self.const = P.ScalarToArray() self.consttensor = P.ScalarToTensor() self.dtypeop = P.DType() self.exp = exp_generic self.expm1 = P.Expm1() self.fill = P.Fill() self.less = P.Less() self.log = log_generic self.log1p = P.Log1p() self.logicalor = P.LogicalOr() self.erf = P.Erf() self.greater = P.Greater() self.sigmoid = P.Sigmoid() self.squeeze = P.Squeeze(0) self.select = P.Select() self.shape = P.Shape() self.softplus = self._softplus self.sqrt = P.Sqrt() self.uniform = C.uniform self.threshold = np.log(np.finfo(np.float32).eps) + 1. self.tiny = np.finfo(np.float).tiny self.sd_const = np.pi / np.sqrt(3)
def __init__(self, concentration=None, rate=None, seed=None, dtype=mstype.float32, name="Gamma"): """ Constructor of Gamma. """ param = dict(locals()) param['param_dict'] = {'concentration': concentration, 'rate': rate} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) # As some operators can't accept scalar input, check the type here if isinstance(concentration, (int, float)): raise TypeError("Input concentration can't be scalar") if isinstance(rate, (int, float)): raise TypeError("Input rate can't be scalar") super(Gamma, self).__init__(seed, dtype, name, param) self._concentration = self._add_parameter(concentration, 'concentration') self._rate = self._add_parameter(rate, 'rate') if self._concentration is not None: check_greater_zero(self._concentration, "concentration") if self._rate is not None: check_greater_zero(self._rate, "rate") # ops needed for the class self.log = log_generic self.square = P.Square() self.sqrt = P.Sqrt() self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.dtypeop = P.DType() self.fill = P.Fill() self.shape = P.Shape() self.select = P.Select() self.greater = P.Greater() self.lgamma = nn.LGamma() self.digamma = nn.DiGamma() self.igamma = nn.IGamma()
def __init__(self, bijector, distribution, seed=None, name="transformed_distribution"): """ Constructor of transformed_distribution class. """ param = dict(locals()) validator.check_value_type('bijector', bijector, [nn.probability.bijector.Bijector], type(self).__name__) validator.check_value_type('distribution', distribution, [Distribution], type(self).__name__) validator.check_type_name("dtype", distribution.dtype, mstype.float_type, type(self).__name__) super(TransformedDistribution, self).__init__(seed, distribution.dtype, name, param) self._bijector = bijector self._distribution = distribution # set attributes self._is_linear_transformation = self.bijector.is_constant_jacobian self._dtype = self.distribution.dtype self._is_scalar_batch = self.distribution.is_scalar_batch and self.bijector.is_scalar_batch self._batch_shape = self.distribution.batch_shape self.default_parameters = self.distribution.default_parameters self.parameter_names = self.distribution.parameter_names # by default, set the parameter_type to be the distribution's parameter_type self.parameter_type = self.distribution.parameter_type self.exp = exp_generic self.log = log_generic self.isnan = P.IsNan() self.cast_base = P.Cast() self.equal_base = P.Equal() self.select_base = P.Select() self.fill_base = P.Fill() # broadcast bijector batch_shape and distribution batch_shape self._broadcast_shape = self._broadcast_bijector_dist()
def __init__(self, is_constant_jacobian=False, is_injective=True, name=None, dtype=None, param=None): """ Constructor of Bijector class. """ super(Bijector, self).__init__() validator.check_value_type('name', name, [str], type(self).__name__) validator.check_value_type('is_constant_jacobian', is_constant_jacobian, [bool], name) validator.check_value_type('is_injective', is_injective, [bool], name) if dtype is not None: validator.check_type_name("dtype", dtype, mstype.float_type, type(self).__name__) self._name = name self._dtype = dtype self._parameters = {} # parsing parameters for k in param.keys(): if k == 'param': continue if not (k == 'self' or k.startswith('_')): self._parameters[k] = param[k] # if no bijector is used as an argument during initilization if 'bijector' not in param.keys(): self._batch_shape = self._calc_batch_shape() self._is_scalar_batch = self._check_is_scalar_batch() self._is_constant_jacobian = is_constant_jacobian self._is_injective = is_injective self.context_mode = context.get_context('mode') self.checktensor = CheckTensor() # ops needed for the base class self.cast_base = P.Cast() self.dtype_base = P.DType() self.shape_base = P.Shape() self.fill_base = P.Fill() self.sametypeshape_base = P.SameTypeShape() self.issubclass_base = P.IsSubClass()
def __init__(self, loc=None, scale=None, seed=None, dtype=mstype.float32, name="Cauchy"): """ Constructor of Cauchy. """ param = dict(locals()) param['param_dict'] = {'loc': loc, 'scale': scale} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Cauchy, self).__init__(seed, dtype, name, param) self._loc = self._add_parameter(loc, 'loc') self._scale = self._add_parameter(scale, 'scale') if self._scale is not None: check_greater_zero(self._scale, "scale") # ops needed for the class self.atan = P.Atan() self.cast = P.Cast() self.const = P.ScalarToArray() self.dtypeop = P.DType() self.exp = exp_generic self.fill = P.Fill() self.less = P.Less() self.log = log_generic self.log1p = log1p_generic self.squeeze = P.Squeeze(0) self.shape = P.Shape() self.sq = P.Square() self.sqrt = P.Sqrt() self.tan = P.Tan() self.uniform = C.uniform self.entropy_const = np.log(4 * np.pi)
def __init__(self, low=None, high=None, seed=None, dtype=mstype.float32, name="Uniform"): """ Constructor of Uniform distribution. """ param = dict(locals()) param['param_dict'] = {'low': low, 'high': high} valid_dtype = mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Uniform, self).__init__(seed, dtype, name, param) self._low = self._add_parameter(low, 'low') self._high = self._add_parameter(high, 'high') if self.low is not None and self.high is not None: check_greater(self.low, self.high, 'low', 'high') # ops needed for the class self.exp = exp_generic self.log = log_generic self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.const = P.ScalarToArray() self.dtypeop = P.DType() self.fill = P.Fill() self.less = P.Less() self.lessequal = P.LessEqual() self.logicaland = P.LogicalAnd() self.select = P.Select() self.shape = P.Shape() self.sq = P.Square() self.zeroslike = P.ZerosLike() self.uniform = C.uniform
def __init__(self, probs=None, seed=None, dtype=mstype.int32, name="Geometric"): """ Constructor of Geometric distribution. """ param = dict(locals()) param['param_dict'] = {'probs': probs} valid_dtype = mstype.int_type + mstype.uint_type + mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Geometric, self).__init__(seed, dtype, name, param) self._probs = self._add_parameter(probs, 'probs') if self._probs is not None: check_prob(self.probs) self.minval = np.finfo(np.float).tiny # ops needed for the class self.exp = exp_generic self.log = log_generic self.squeeze = P.Squeeze(0) self.cast = P.Cast() self.const = P.ScalarToArray() self.dtypeop = P.DType() self.fill = P.Fill() self.floor = P.Floor() self.issubclass = P.IsSubClass() self.less = P.Less() self.pow = P.Pow() self.select = P.Select() self.shape = P.Shape() self.sq = P.Square() self.uniform = C.uniform
def _check_label_dtype(labels_dtype, cls_name): validator.check_type_name("labels", labels_dtype, [mstype.int32, mstype.int64], cls_name)
def __init__(self, op, to_type=mstype.float16): super(OutputTo, self).__init__(auto_prefix=False) self._op = op validator.check_type_name('to_type', to_type, [mstype.float16, mstype.float32], None) self.to_type = to_type
def _check_input_dtype(input_dtype, param_name, allow_dtypes, cls_name): validator.check_type_name(param_name, input_dtype, allow_dtypes, cls_name)
def _check_input_dtype(targets_dtype, cls_name): validator.check_type_name( "targets", targets_dtype, [mstype.int32, mstype.int64, mstype.float16, mstype.float32], cls_name)