|
@@ -14,6 +14,8 @@ |
|
|
""" |
|
|
""" |
|
|
Noise Mechanisms. |
|
|
Noise Mechanisms. |
|
|
""" |
|
|
""" |
|
|
|
|
|
from abc import abstractmethod |
|
|
|
|
|
|
|
|
from mindspore import Tensor |
|
|
from mindspore import Tensor |
|
|
from mindspore.nn import Cell |
|
|
from mindspore.nn import Cell |
|
|
from mindspore.ops import operations as P |
|
|
from mindspore.ops import operations as P |
|
@@ -22,8 +24,11 @@ from mindspore.common import dtype as mstype |
|
|
|
|
|
|
|
|
from mindarmour.utils._check_param import check_param_type |
|
|
from mindarmour.utils._check_param import check_param_type |
|
|
from mindarmour.utils._check_param import check_value_positive |
|
|
from mindarmour.utils._check_param import check_value_positive |
|
|
from mindarmour.utils._check_param import check_value_non_negative |
|
|
|
|
|
from mindarmour.utils._check_param import check_param_in_range |
|
|
from mindarmour.utils._check_param import check_param_in_range |
|
|
|
|
|
from mindarmour.utils.logger import LogUtil |
|
|
|
|
|
|
|
|
|
|
|
LOGGER = LogUtil.get_instance() |
|
|
|
|
|
TAG = 'Defense' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MechanismsFactory: |
|
|
class MechanismsFactory: |
|
@@ -98,6 +103,7 @@ class Mechanisms(Cell): |
|
|
Basic class of noise generated mechanism. |
|
|
Basic class of noise generated mechanism. |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod |
|
|
def construct(self, gradients): |
|
|
def construct(self, gradients): |
|
|
""" |
|
|
""" |
|
|
Construct function. |
|
|
Construct function. |
|
@@ -114,8 +120,9 @@ class GaussianRandom(Mechanisms): |
|
|
initial_noise_multiplier(float): Ratio of the standard deviation of |
|
|
initial_noise_multiplier(float): Ratio of the standard deviation of |
|
|
Gaussian noise divided by the norm_bound, which will be used to |
|
|
Gaussian noise divided by the norm_bound, which will be used to |
|
|
calculate privacy spent. Default: 1.5. |
|
|
calculate privacy spent. Default: 1.5. |
|
|
mean(float): Average value of random noise. Default: 0.0. |
|
|
|
|
|
seed(int): Original random seed. Default: 0. |
|
|
|
|
|
|
|
|
seed(int): Original random seed, if seed=0 random normal will use secure |
|
|
|
|
|
random number. IF seed!=0 random normal will generate values using |
|
|
|
|
|
given seed. Default: 0. |
|
|
|
|
|
|
|
|
Returns: |
|
|
Returns: |
|
|
Tensor, generated noise with shape like given gradients. |
|
|
Tensor, generated noise with shape like given gradients. |
|
@@ -129,16 +136,14 @@ class GaussianRandom(Mechanisms): |
|
|
>>> print(res) |
|
|
>>> print(res) |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
def __init__(self, norm_bound=0.5, initial_noise_multiplier=1.5, mean=0.0, seed=0): |
|
|
|
|
|
|
|
|
def __init__(self, norm_bound=0.5, initial_noise_multiplier=1.5, seed=0): |
|
|
super(GaussianRandom, self).__init__() |
|
|
super(GaussianRandom, self).__init__() |
|
|
self._norm_bound = check_value_positive('norm_bound', norm_bound) |
|
|
self._norm_bound = check_value_positive('norm_bound', norm_bound) |
|
|
self._norm_bound = Tensor(norm_bound, mstype.float32) |
|
|
self._norm_bound = Tensor(norm_bound, mstype.float32) |
|
|
self._initial_noise_multiplier = check_value_positive('initial_noise_multiplier', |
|
|
self._initial_noise_multiplier = check_value_positive('initial_noise_multiplier', |
|
|
initial_noise_multiplier) |
|
|
initial_noise_multiplier) |
|
|
self._initial_noise_multiplier = Tensor(initial_noise_multiplier, mstype.float32) |
|
|
self._initial_noise_multiplier = Tensor(initial_noise_multiplier, mstype.float32) |
|
|
mean = check_param_type('mean', mean, float) |
|
|
|
|
|
mean = check_value_non_negative('mean', mean) |
|
|
|
|
|
self._mean = Tensor(mean, mstype.float32) |
|
|
|
|
|
|
|
|
self._mean = Tensor(0, mstype.float32) |
|
|
self._normal = P.Normal(seed=seed) |
|
|
self._normal = P.Normal(seed=seed) |
|
|
|
|
|
|
|
|
def construct(self, gradients): |
|
|
def construct(self, gradients): |
|
@@ -159,8 +164,8 @@ class GaussianRandom(Mechanisms): |
|
|
|
|
|
|
|
|
class AdaGaussianRandom(Mechanisms): |
|
|
class AdaGaussianRandom(Mechanisms): |
|
|
""" |
|
|
""" |
|
|
Adaptive Gaussian noise generated mechanism. Noise would be decayed with training. Decay mode could be 'Time' |
|
|
|
|
|
mode or 'Step' mode. |
|
|
|
|
|
|
|
|
Adaptive Gaussian noise generated mechanism. Noise would be decayed with |
|
|
|
|
|
training. Decay mode could be 'Time' mode or 'Step' mode. |
|
|
|
|
|
|
|
|
Args: |
|
|
Args: |
|
|
norm_bound(float): Clipping bound for the l2 norm of the gradients. |
|
|
norm_bound(float): Clipping bound for the l2 norm of the gradients. |
|
@@ -191,7 +196,7 @@ class AdaGaussianRandom(Mechanisms): |
|
|
>>> print(res) |
|
|
>>> print(res) |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
def __init__(self, norm_bound=1.0, initial_noise_multiplier=1.5, mean=0.0, |
|
|
|
|
|
|
|
|
def __init__(self, norm_bound=1.0, initial_noise_multiplier=1.5, |
|
|
noise_decay_rate=6e-4, decay_policy='Time', seed=0): |
|
|
noise_decay_rate=6e-4, decay_policy='Time', seed=0): |
|
|
super(AdaGaussianRandom, self).__init__() |
|
|
super(AdaGaussianRandom, self).__init__() |
|
|
norm_bound = check_value_positive('norm_bound', norm_bound) |
|
|
norm_bound = check_value_positive('norm_bound', norm_bound) |
|
@@ -205,9 +210,7 @@ class AdaGaussianRandom(Mechanisms): |
|
|
self._stddev = P.Mul()(self._norm_bound, self._initial_noise_multiplier) |
|
|
self._stddev = P.Mul()(self._norm_bound, self._initial_noise_multiplier) |
|
|
self._noise_multiplier = Parameter(initial_noise_multiplier, |
|
|
self._noise_multiplier = Parameter(initial_noise_multiplier, |
|
|
name='noise_multiplier') |
|
|
name='noise_multiplier') |
|
|
mean = check_param_type('mean', mean, float) |
|
|
|
|
|
mean = check_value_non_negative('mean', mean) |
|
|
|
|
|
self._mean = Tensor(mean, mstype.float32) |
|
|
|
|
|
|
|
|
self._mean = Tensor(0, mstype.float32) |
|
|
noise_decay_rate = check_param_type('noise_decay_rate', noise_decay_rate, float) |
|
|
noise_decay_rate = check_param_type('noise_decay_rate', noise_decay_rate, float) |
|
|
check_param_in_range('noise_decay_rate', noise_decay_rate, 0.0, 1.0) |
|
|
check_param_in_range('noise_decay_rate', noise_decay_rate, 0.0, 1.0) |
|
|
self._noise_decay_rate = Tensor(noise_decay_rate, mstype.float32) |
|
|
self._noise_decay_rate = Tensor(noise_decay_rate, mstype.float32) |
|
|