Exemple #1
0
    def _sample(self, shape=(), mean=None, sd=None):
        """
        Sampling.

        Args:
            shape (tuple): The shape of the sample. Default: ().
            mean (Tensor): The mean of the samples. Default: self._mean_value.
            sd (Tensor): The standard deviation of the samples. Default: self._sd_value.

        Returns:
            Tensor, shape is shape + batch_shape.
        """
        shape = self.checktuple(shape, 'shape')
        mean, sd = self._check_param_type(mean, sd)
        batch_shape = self.shape(mean + sd)
        origin_shape = shape + batch_shape
        if origin_shape == ():
            sample_shape = (1,)
        else:
            sample_shape = origin_shape
        sample_norm = C.normal(sample_shape, mean, sd, self.seed)
        value = self.cast(sample_norm, self.dtype)
        if origin_shape == ():
            value = self.squeeze(value)
        return value
Exemple #2
0
    def _sample(self, shape=(), mean=None, sd=None):
        """
        Sampling.

        Args:
            shape (tuple): shape of the sample. Default: ().
            mean (Tensor): mean of the samples. Default: self._mean_value.
            sd (Tensor): standard deviation of the samples. Default: self._sd_value.

        Returns:
            Tensor, shape is shape + batch_shape.
        """
        mean = self.cast(
            mean,
            self.parameter_type) if mean is not None else self._mean_value
        if mean is None:
            raise_none_error("mean")
        sd = self.cast(
            sd, self.parameter_type) if sd is not None else self._sd_value
        if sd is None:
            raise_none_error("sd")
        batch_shape = self.shape(mean + sd)
        origin_shape = shape + batch_shape
        if origin_shape == ():
            sample_shape = (1, )
        else:
            sample_shape = origin_shape
        sample_norm = C.normal(sample_shape, mean, sd, self.seed)
        value = self.cast(sample_norm, self.dtype)
        if origin_shape == ():
            value = self.squeeze(value)
        return value
Exemple #3
0
    def construct(self, empirical_fraction, norm_bound):
        """
        Update value of norm_bound.

        Args:
            empirical_fraction(Tensor): empirical fraction of samples with the
                value at most `target_unclipped_quantile`.
            norm_bound(Tensor): Clipping bound for the l2 norm of the gradients.

        Returns:
            Tensor, generated noise with shape like given gradients.
        """
        fraction_noise = normal((1,), self._zero, self._fraction_stddev, self._seed)
        empirical_fraction = self._add(empirical_fraction, fraction_noise)
        if self._decay_policy == 'Linear':
            grad_clip = self._sub(empirical_fraction,
                                  self._target_unclipped_quantile)
            next_norm_bound = self._sub(norm_bound,
                                        self._mul(self._learning_rate, grad_clip))

        # decay_policy == 'Geometric'
        else:
            grad_clip = self._sub(empirical_fraction,
                                  self._target_unclipped_quantile)
            grad_clip = self._exp(self._mul(-self._learning_rate, grad_clip))
            next_norm_bound = self._mul(norm_bound, grad_clip)
        return next_norm_bound
Exemple #4
0
    def _sample(self, name, shape=(), probs=None):
        """
        Sampling.

        Args:
            name (str): name of the function. Should always be 'sample' when passed in from construct.
            shape (tuple): shape of the sample. Default: ().
            probs (Tensor): probs1 of the samples. Default: self._probs.

        Returns:
            Tensor, shape is shape + batch_shape.
        """
        if name == 'sample':
            probs1 = self._probs if probs is None else probs
            batch_shape = self.shape(probs1)
            sample_shape = shape + batch_shape
            mean_zero = self.const(0.0)
            sd_one = self.const(1.0)
            sqrt_two = self.sqrt(self.const(2.0))
            sample_norm = C.normal(sample_shape, mean_zero, sd_one, self.seed)
            sample_uniform = 0.5 * (1 + self.erf(self.realdiv(sample_norm, sqrt_two)))
            sample = self.less(sample_uniform, probs1)
            sample = self.cast(sample, self._dtype)
            return sample
        return None
Exemple #5
0
    def construct(self, gradients):
        """
        Generated Gaussian noise.

        Args:
            gradients(Tensor): The gradients.

        Returns:
            Tensor, generated noise with shape like given gradients.
        """
        shape = P.Shape()(gradients)
        stddev = P.Mul()(self._norm_bound, self._initial_noise_multiplier)
        noise = normal(shape, self._mean, stddev, self._seed)
        return noise
Exemple #6
0
    def _sample(self, shape=(), mean=None, sd=None):
        """
        Sampling.

        Args:
            shape (tuple): shape of the sample. Default: ().
            mean (Tensor): mean of the samples. Default: self._mean_value.
            sd (Tensor): standard deviation of the samples. Default: self._sd_value.

        Returns:
            Tensor, shape is shape + batch_shape.
        """
        mean = self._mean_value if mean is None else mean
        sd = self._sd_value if sd is None else sd
        batch_shape = self.shape(self.zeroslike(mean) + self.zeroslike(sd))
        sample_shape = shape + batch_shape
        sample_norm = C.normal(sample_shape, mean, sd, self.seed)
        return sample_norm
Exemple #7
0
    def _sample(self, name, shape=(), mean=None, sd=None):
        """
        Sampling.

        Args:
            name (str): name of the function. Should always be 'sample' when passed in from construct.
            shape (tuple): shape of the sample. Default: ().
            mean (Tensor): mean of the samples. Default: self._mean_value.
            sd (Tensor): standard deviation of the samples. Default: self._sd_value.

        Returns:
            Tensor, shape is shape + batch_shape.
        """
        if name == 'sample':
            mean = self._mean_value if mean is None else mean
            sd = self._sd_value if sd is None else sd
            batch_shape = self.shape(self.add(self.zeroslike(mean), self.zeroslike(sd)))
            sample_shape = shape + batch_shape
            mean_zero = self.const(0.0)
            sd_one = self.const(1.0)
            sample_norm = C.normal(sample_shape, mean_zero, sd_one, self.seed)
            sample = self.add(mean, self.mul(sample_norm, sd))
            return sample
        return None
Exemple #8
0
 def construct(self, mean, stddev):
     return C.normal(self.shape, mean, stddev, self.seed)
Exemple #9
0
 def construct(self, mean, stddev):
     s1 = C.normal(self.shape, mean, stddev, self.seed)
     s2 = C.normal(self.shape, mean, stddev, self.seed)
     s3 = C.normal(self.shape, mean, stddev, self.seed)
     return s1, s2, s3
Exemple #10
0
 def construct(self, mean, stddev):
     C.set_seed(20)
     return C.normal(self.shape, mean, stddev, self.seed)