提交 92165efc 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!42 Add example of MechanismsFactory and add example of mech=None in dpmodel

Merge pull request !42 from ZhidanLiu/master
...@@ -46,8 +46,44 @@ class MechanismsFactory: ...@@ -46,8 +46,44 @@ class MechanismsFactory:
Raises: Raises:
NameError: `policy` must be in ['Gaussian', 'AdaGaussian']. NameError: `policy` must be in ['Gaussian', 'AdaGaussian'].
Returns: Returns:
Mechanisms, class of noise generated Mechanism. Mechanisms, class of noise generated Mechanism.
Examples:
>>> class Net(nn.Cell):
>>> def __init__(self):
>>> super(Net, self).__init__()
>>> self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal')
>>> self.bn = nn.BatchNorm2d(64)
>>> self.relu = nn.ReLU()
>>> self.flatten = nn.Flatten()
>>> self.fc = nn.Dense(64*224*224, 12) # padding=0
>>>
>>> def construct(self, x):
>>> x = self.conv(x)
>>> x = self.bn(x)
>>> x = self.relu(x)
>>> x = self.flatten(x)
>>> out = self.fc(x)
>>> return out
>>> norm_clip = 1.0
>>> initial_noise_multiplier = 1.5
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
>>> net_opt = Momentum(params=net.trainable_params(), learning_rate=0.01, momentum=0.9)
>>> mech = MechanismsFactory().create('Gaussian',
>>> norm_bound=norm_clip,
>>> initial_noise_multiplier=initial_noise_multiplier)
>>> model = DPModel(micro_batches=2,
>>> norm_clip=1.0,
>>> mech=mech,
>>> network=net,
>>> loss_fn=loss,
>>> optimizer=net_opt,
>>> metrics=None)
>>> dataset = get_dataset()
>>> model.train(2, dataset)
""" """
if policy == 'Gaussian': if policy == 'Gaussian':
return GaussianRandom(*args, **kwargs) return GaussianRandom(*args, **kwargs)
......
...@@ -72,38 +72,29 @@ class DPModel(Model): ...@@ -72,38 +72,29 @@ class DPModel(Model):
mech (Mechanisms): The object can generate the different type of noise. Default: None. mech (Mechanisms): The object can generate the different type of noise. Default: None.
Examples: Examples:
>>> class Net(nn.Cell): >>> norm_clip = 1.0
>>> def __init__(self): >>> initial_noise_multiplier = 0.01
>>> super(Net, self).__init__() >>> network = LeNet5()
>>> self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal') >>> batch_size = 32
>>> self.bn = nn.BatchNorm2d(64) >>> batches = 128
>>> self.relu = nn.ReLU() >>> epochs = 1
>>> self.flatten = nn.Flatten() >>> micro_batches = 2
>>> self.fc = nn.Dense(64*224*224, 12) # padding=0
>>>
>>> def construct(self, x):
>>> x = self.conv(x)
>>> x = self.bn(x)
>>> x = self.relu(x)
>>> x = self.flatten(x)
>>> out = self.fc(x)
>>> return out
>>>
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True) >>> loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
>>> net_opt = Momentum(params=net.trainable_params(), learning_rate=0.01, momentum=0.9) >>> factory_opt = DPOptimizerClassFactory(micro_batches=micro_batches)
>>> mech = MechanismsFactory().create('Gaussian', >>> factory_opt.set_mechanisms('Gaussian',
>>> norm_bound=args.norm_clip, >>> norm_bound=norm_clip,
>>> initial_noise_multiplier=args.initial_noise_multiplier) >>> initial_noise_multiplier=initial_noise_multiplier)
>>> model = DPModel(micro_batches=2, >>> net_opt = factory_opt.create('Momentum')(network.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> norm_clip=1.0, >>> model = DPModel(micro_batches=micro_batches,
>>> mech=mech, >>> norm_clip=norm_clip,
>>> network=net, >>> mech=None,
>>> network=network,
>>> loss_fn=loss, >>> loss_fn=loss,
>>> optimizer=net_opt, >>> optimizer=net_opt,
>>> metrics=None) >>> metrics=None)
>>> dataset = get_dataset() >>> ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches), ['data', 'label'])
>>> model.train(2, dataset) >>> ms_ds.set_dataset_size(batch_size * batches)
>>> model.train(epochs, ms_ds, dataset_sink_mode=False)
""" """
def __init__(self, micro_batches=2, norm_clip=1.0, mech=None, **kwargs): def __init__(self, micro_batches=2, norm_clip=1.0, mech=None, **kwargs):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册