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_mechanisms.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2019 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. different Privacy test.
  16. """
  17. import pytest
  18. from mindspore import context
  19. from mindarmour.diff_privacy import GaussianRandom
  20. from mindarmour.diff_privacy import AdaGaussianRandom
  21. from mindarmour.diff_privacy import MechanismsFactory
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_ascend_training
  24. @pytest.mark.env_onecard
  25. @pytest.mark.component_mindarmour
  26. def test_gaussian():
  27. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  28. shape = (3, 2, 4)
  29. norm_bound = 1.0
  30. initial_noise_multiplier = 0.1
  31. net = GaussianRandom(norm_bound, initial_noise_multiplier)
  32. res = net(shape)
  33. print(res)
  34. @pytest.mark.level0
  35. @pytest.mark.platform_x86_ascend_training
  36. @pytest.mark.env_onecard
  37. @pytest.mark.component_mindarmour
  38. def test_ada_gaussian():
  39. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  40. shape = (3, 2, 4)
  41. norm_bound = 1.0
  42. initial_noise_multiplier = 0.1
  43. alpha = 0.5
  44. decay_policy = "Step"
  45. net = AdaGaussianRandom(norm_bound, initial_noise_multiplier,
  46. alpha, decay_policy)
  47. res = net(shape)
  48. print(res)
  49. def test_factory():
  50. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  51. shape = (3, 2, 4)
  52. norm_bound = 1.0
  53. initial_noise_multiplier = 0.1
  54. alpha = 0.5
  55. decay_policy = "Step"
  56. noise_mechanism = MechanismsFactory()
  57. noise_construct = noise_mechanism.create('Gaussian',
  58. norm_bound,
  59. initial_noise_multiplier)
  60. noise = noise_construct(shape)
  61. print('Gaussian noise: ', noise)
  62. ada_mechanism = MechanismsFactory()
  63. ada_noise_construct = ada_mechanism.create('AdaGaussian',
  64. norm_bound,
  65. initial_noise_multiplier,
  66. alpha,
  67. decay_policy)
  68. ada_noise = ada_noise_construct(shape)
  69. print('ada noise: ', ada_noise)
  70. if __name__ == '__main__':
  71. # device_target can be "CPU", "GPU" or "Ascend"
  72. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")

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