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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 mindspore import Tensor
  20. from mindspore.common import dtype as mstype
  21. from mindarmour.diff_privacy import GaussianRandom
  22. from mindarmour.diff_privacy import AdaGaussianRandom
  23. from mindarmour.diff_privacy import MechanismsFactory
  24. @pytest.mark.level0
  25. @pytest.mark.platform_x86_ascend_training
  26. @pytest.mark.env_onecard
  27. @pytest.mark.component_mindarmour
  28. def test_graph_gaussian():
  29. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  30. grad = Tensor([3, 2, 4], mstype.float32)
  31. norm_bound = 1.0
  32. initial_noise_multiplier = 0.1
  33. net = GaussianRandom(norm_bound, initial_noise_multiplier)
  34. res = net(grad)
  35. print(res)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_ascend_training
  38. @pytest.mark.env_onecard
  39. @pytest.mark.component_mindarmour
  40. def test_pynative_gaussian():
  41. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  42. grad = Tensor([3, 2, 4], mstype.float32)
  43. norm_bound = 1.0
  44. initial_noise_multiplier = 0.1
  45. net = GaussianRandom(norm_bound, initial_noise_multiplier)
  46. res = net(grad)
  47. print(res)
  48. @pytest.mark.level0
  49. @pytest.mark.platform_x86_ascend_training
  50. @pytest.mark.env_onecard
  51. @pytest.mark.component_mindarmour
  52. def test_graph_ada_gaussian():
  53. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  54. grad = Tensor([3, 2, 4], mstype.float32)
  55. norm_bound = 1.0
  56. initial_noise_multiplier = 0.1
  57. alpha = 0.5
  58. decay_policy = 'Step'
  59. net = AdaGaussianRandom(norm_bound, initial_noise_multiplier,
  60. noise_decay_rate=alpha, decay_policy=decay_policy)
  61. res = net(grad)
  62. print(res)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_ascend_training
  65. @pytest.mark.env_onecard
  66. @pytest.mark.component_mindarmour
  67. def test_graph_factory():
  68. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  69. grad = Tensor([3, 2, 4], mstype.float32)
  70. norm_bound = 1.0
  71. initial_noise_multiplier = 0.1
  72. alpha = 0.5
  73. decay_policy = 'Step'
  74. noise_mechanism = MechanismsFactory()
  75. noise_construct = noise_mechanism.create('Gaussian',
  76. norm_bound,
  77. initial_noise_multiplier)
  78. noise = noise_construct(grad)
  79. print('Gaussian noise: ', noise)
  80. ada_mechanism = MechanismsFactory()
  81. ada_noise_construct = ada_mechanism.create('AdaGaussian',
  82. norm_bound,
  83. initial_noise_multiplier,
  84. noise_decay_rate=alpha,
  85. decay_policy=decay_policy)
  86. ada_noise = ada_noise_construct(grad)
  87. print('ada noise: ', ada_noise)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_ascend_training
  90. @pytest.mark.env_onecard
  91. @pytest.mark.component_mindarmour
  92. def test_pynative_ada_gaussian():
  93. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  94. grad = Tensor([3, 2, 4], mstype.float32)
  95. norm_bound = 1.0
  96. initial_noise_multiplier = 0.1
  97. alpha = 0.5
  98. decay_policy = 'Step'
  99. net = AdaGaussianRandom(norm_bound, initial_noise_multiplier,
  100. noise_decay_rate=alpha, decay_policy=decay_policy)
  101. res = net(grad)
  102. print(res)
  103. @pytest.mark.level0
  104. @pytest.mark.platform_x86_ascend_training
  105. @pytest.mark.env_onecard
  106. @pytest.mark.component_mindarmour
  107. def test_pynative_factory():
  108. context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
  109. grad = Tensor([3, 2, 4], mstype.float32)
  110. norm_bound = 1.0
  111. initial_noise_multiplier = 0.1
  112. alpha = 0.5
  113. decay_policy = 'Step'
  114. noise_mechanism = MechanismsFactory()
  115. noise_construct = noise_mechanism.create('Gaussian',
  116. norm_bound,
  117. initial_noise_multiplier)
  118. noise = noise_construct(grad)
  119. print('Gaussian noise: ', noise)
  120. ada_mechanism = MechanismsFactory()
  121. ada_noise_construct = ada_mechanism.create('AdaGaussian',
  122. norm_bound,
  123. initial_noise_multiplier,
  124. noise_decay_rate=alpha,
  125. decay_policy=decay_policy)
  126. ada_noise = ada_noise_construct(grad)
  127. print('ada noise: ', ada_noise)

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。