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(type(self).__name__, dtype, valid_dtype) 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(type(self).__name__, dtype, valid_dtype) 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 = expm1_generic 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, 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(type(self).__name__, dtype, valid_dtype) 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 _quant_export(network, *inputs, file_format, **kwargs): """ Exports MindSpore quantization predict model to deploy with AIR and MINDIR. """ if not kwargs.get('quant_mode', None): return network supported_device = ["Ascend", "GPU"] supported_formats = ['AIR', 'MINDIR'] quant_mode_formats = ['AUTO', 'MANUAL'] mean = 127.5 if kwargs.get('mean', None) is None else kwargs['mean'] std_dev = 127.5 if kwargs.get('std_dev', None) is None else kwargs['std_dev'] quant_mode = kwargs['quant_mode'] if quant_mode not in quant_mode_formats: raise KeyError( f'Quant_mode input is wrong, Please choose the right mode of the quant_mode.' ) mean = Validator.check_type("mean", mean, (int, float)) std_dev = Validator.check_type("std_dev", std_dev, (int, float)) if context.get_context('device_target') not in supported_device: raise KeyError("Unsupported {} device target.".format( context.get_context('device_target'))) if file_format not in supported_formats: raise ValueError('Illegal file format {}.'.format(file_format)) network.set_train(False) if file_format == "MINDIR": if quant_mode == 'MANUAL': exporter = quant_export.ExportManualQuantNetwork(network, mean, std_dev, *inputs, is_mindir=True) else: exporter = quant_export.ExportToQuantInferNetwork(network, mean, std_dev, *inputs, is_mindir=True) else: if quant_mode == 'MANUAL': exporter = quant_export.ExportManualQuantNetwork( network, mean, std_dev, *inputs) else: exporter = quant_export.ExportToQuantInferNetwork( network, mean, std_dev, *inputs) deploy_net = exporter.run() return deploy_net
def __init__(self, probs=None, seed=None, dtype=mstype.int32, name="Categorical"): param = dict(locals()) param['param_dict'] = {'probs': probs} valid_dtype = mstype.int_type Validator.check_type("Categorical", dtype, valid_dtype) 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(self.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.Argmax() 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.floor = P.Floor() self.gather = P.GatherNd() self.less = P.Less() self.log = log_generic self.log_softmax = P.LogSoftmax() self.logicor = P.LogicalOr() 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.index_type = mstype.int32
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(type(self).__name__, dtype, valid_dtype) 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.dtypeop = P.DType() self.exp = exp_generic self.expm1 = expm1_generic self.fill = P.Fill() self.less = P.Less() self.log = log_generic self.log1p = log1p_generic 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
def __init__(self, loc, scale, seed=0, dtype=mstype.float32, name="Gumbel"): """ Constructor of Gumbel distribution. """ valid_dtype = mstype.float_type Validator.check_type(type(self).__name__, dtype, valid_dtype) gumbel_cdf = msb.GumbelCDF(loc, scale, dtype) super(Gumbel, self).__init__(distribution=msd.Uniform(0.0, 1.0, dtype=dtype), bijector=msb.Invert(gumbel_cdf), seed=seed, name=name) self.parameter_type = gumbel_cdf.parameter_type self._broadcast_shape = gumbel_cdf.event_shape if self._broadcast_shape != (): self._is_scalar_batch = False # 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, loc=0.0, scale=1.0, dtype=mstype.float32, name='GumbelCDF'): """ Constructor of GumbelCDF Bijector. """ param = dict(locals()) valid_dtype = mstype.float_type + mstype.int_type + mstype.uint_type Validator.check_type(type(self).__name__, dtype, valid_dtype) parameter_type = set_param_type({'loc': loc, "scale": scale}, dtype) super(GumbelCDF, self).__init__(name=name, dtype=dtype, param=param) self._parameter_type = parameter_type self._loc = self._add_parameter(loc, 'loc') self._scale = self._add_parameter(scale, 'scale') check_greater_zero(self._scale, "scale") self._event_shape = self._calc_event_shape() self.cast = P.Cast() self.exp = exp_generic self.log = log_generic
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(type(self).__name__, dtype, valid_dtype) 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(type(self).__name__, dtype, valid_dtype) 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