You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_model_train.py 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. DP-Model test.
  16. """
  17. import pytest
  18. import numpy as np
  19. from mindspore import nn
  20. from mindspore.nn import SGD
  21. from mindspore.model_zoo.lenet import LeNet5
  22. from mindspore import context
  23. import mindspore.dataset as ds
  24. from mindarmour.diff_privacy import DPOptimizerClassFactory
  25. from mindarmour.diff_privacy import DPModel
  26. def dataset_generator(batch_size, batches):
  27. data = np.random.random((batches * batch_size, 1, 32, 32)).astype(np.float32)
  28. label = np.random.randint(0, 10, batches * batch_size).astype(np.int32)
  29. for i in range(batches):
  30. yield data[i * batch_size:(i + 1) * batch_size], label[i * batch_size:(i + 1) * batch_size]
  31. @pytest.mark.level0
  32. @pytest.mark.platform_arm_ascend_training
  33. @pytest.mark.platform_x86_ascend_training
  34. @pytest.mark.env_card
  35. @pytest.mark.component_mindarmour
  36. def test_dp_model():
  37. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  38. l2_norm_bound = 1.0
  39. initial_noise_multiplier = 0.01
  40. net = LeNet5()
  41. batch_size = 32
  42. batches = 128
  43. epochs = 1
  44. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  45. optim = SGD(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
  46. gaussian_mech = DPOptimizerClassFactory()
  47. gaussian_mech.set_mechanisms('Gaussian',
  48. norm_bound=l2_norm_bound,
  49. initial_noise_multiplier=initial_noise_multiplier)
  50. model = DPModel(micro_batches=2,
  51. norm_clip=l2_norm_bound,
  52. dp_mech=gaussian_mech.mech,
  53. network=net,
  54. loss_fn=loss,
  55. optimizer=optim,
  56. metrics=None)
  57. ms_ds = ds.GeneratorDataset(dataset_generator(batch_size, batches), ['data', 'label'])
  58. ms_ds.set_dataset_size(batch_size * batches)
  59. model.train(epochs, ms_ds)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。