Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
ac39d193
M
mindarmour
项目概览
MindSpore
/
mindarmour
通知
4
Star
2
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindarmour
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ac39d193
编写于
7月 13, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
7月 13, 2020
浏览文件
操作
浏览文件
下载
差异文件
!50 add exponential noise decay
Merge pull request !50 from zheng-huanhuan/master
上级
1d3a5726
3fa256e7
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
127 addition
and
70 deletion
+127
-70
mindarmour/diff_privacy/mechanisms/mechanisms.py
mindarmour/diff_privacy/mechanisms/mechanisms.py
+17
-12
mindarmour/diff_privacy/optimizer/optimizer.py
mindarmour/diff_privacy/optimizer/optimizer.py
+3
-2
mindarmour/diff_privacy/train/model.py
mindarmour/diff_privacy/train/model.py
+59
-50
tests/ut/python/diff_privacy/test_mechanisms.py
tests/ut/python/diff_privacy/test_mechanisms.py
+48
-6
未找到文件。
mindarmour/diff_privacy/mechanisms/mechanisms.py
浏览文件 @
ac39d193
...
...
@@ -214,8 +214,8 @@ class AdaGaussianRandom(Mechanisms):
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
)
self
.
_noise_decay_rate
=
Tensor
(
noise_decay_rate
,
mstype
.
float32
)
if
decay_policy
not
in
[
'Time'
,
'Step'
]:
raise
NameError
(
"The decay_policy must be in ['Time', 'Step'], but "
if
decay_policy
not
in
[
'Time'
,
'Step'
,
'Exp'
]:
raise
NameError
(
"The decay_policy must be in ['Time', 'Step'
, 'Exp'
], but "
"get {}"
.
format
(
decay_policy
))
self
.
_decay_policy
=
decay_policy
self
.
_mul
=
P
.
Mul
()
...
...
@@ -243,18 +243,18 @@ class _MechanismsParamsUpdater(Cell):
Args:
policy(str): Pass in by the mechanisms class, mechanisms parameters update policy.
decay_rate(Tensor): Pass in by the mechanisms class, hyper parameter for controlling the decay size.
cur_
params
(Parameter): Pass in by the mechanisms class, current params value in this time.
init_
params
(Parameter):Pass in by the mechanisms class, initial params value to be updated.
cur_
noise_multiplier
(Parameter): Pass in by the mechanisms class, current params value in this time.
init_
noise_multiplier
(Parameter):Pass in by the mechanisms class, initial params value to be updated.
Returns:
Tuple, next params value.
"""
def
__init__
(
self
,
policy
,
decay_rate
,
cur_
params
,
init_params
):
def
__init__
(
self
,
policy
,
decay_rate
,
cur_
noise_multiplier
,
init_noise_multiplier
):
super
(
_MechanismsParamsUpdater
,
self
).
__init__
()
self
.
_policy
=
policy
self
.
_decay_rate
=
decay_rate
self
.
_cur_
params
=
cur_params
self
.
_init_
params
=
init_params
self
.
_cur_
noise_multiplier
=
cur_noise_multiplier
self
.
_init_
noise_multiplier
=
init_noise_multiplier
self
.
_div
=
P
.
Sub
()
self
.
_add
=
P
.
TensorAdd
()
...
...
@@ -262,6 +262,7 @@ class _MechanismsParamsUpdater(Cell):
self
.
_sub
=
P
.
Sub
()
self
.
_one
=
Tensor
(
1
,
mstype
.
float32
)
self
.
_mul
=
P
.
Mul
()
self
.
_exp
=
P
.
Exp
()
def
construct
(
self
):
"""
...
...
@@ -271,10 +272,14 @@ class _MechanismsParamsUpdater(Cell):
Tuple, next step parameters value.
"""
if
self
.
_policy
==
'Time'
:
temp
=
self
.
_div
(
self
.
_init_
params
,
self
.
_cur_params
)
temp
=
self
.
_div
(
self
.
_init_
noise_multiplier
,
self
.
_cur_noise_multiplier
)
temp
=
self
.
_add
(
temp
,
self
.
_decay_rate
)
next_params
=
self
.
_assign
(
self
.
_cur_params
,
self
.
_div
(
self
.
_init_params
,
temp
))
else
:
next_noise_multiplier
=
self
.
_assign
(
self
.
_cur_noise_multiplier
,
self
.
_div
(
self
.
_init_noise_multiplier
,
temp
))
elif
self
.
_policy
==
'Step'
:
temp
=
self
.
_sub
(
self
.
_one
,
self
.
_decay_rate
)
next_params
=
self
.
_assign
(
self
.
_cur_params
,
self
.
_mul
(
temp
,
self
.
_cur_params
))
return
next_params
next_noise_multiplier
=
self
.
_assign
(
self
.
_cur_noise_multiplier
,
self
.
_mul
(
temp
,
self
.
_cur_noise_multiplier
))
else
:
next_noise_multiplier
=
self
.
_assign
(
self
.
_cur_noise_multiplier
,
self
.
_div
(
self
.
_one
,
self
.
_exp
(
self
.
_one
)))
return
next_noise_multiplier
mindarmour/diff_privacy/optimizer/optimizer.py
浏览文件 @
ac39d193
...
...
@@ -130,8 +130,9 @@ class DPOptimizerClassFactory:
if
self
.
_mech
is
not
None
and
self
.
_mech
.
_decay_policy
is
not
None
:
self
.
_mech_param_updater
=
_MechanismsParamsUpdater
(
policy
=
self
.
_mech
.
_decay_policy
,
decay_rate
=
self
.
_mech
.
_noise_decay_rate
,
cur_params
=
self
.
_mech
.
_noise_multiplier
,
init_params
=
cur_noise_multiplier
=
self
.
_mech
.
_noise_multiplier
,
init_noise_multiplier
=
self
.
_mech
.
_initial_noise_multiplier
)
def
construct
(
self
,
gradients
):
...
...
mindarmour/diff_privacy/train/model.py
浏览文件 @
ac39d193
...
...
@@ -195,8 +195,7 @@ class DPModel(Model):
mech
=
self
.
_mech
).
set_train
()
return
network
def
_build_train_network
(
self
):
def
_build_train_network
(
self
):
"""Build train network"""
network
=
self
.
_network
if
self
.
_micro_batches
:
...
...
@@ -376,8 +375,10 @@ class _TrainOneStepWithLossScaleCell(Cell):
if
self
.
_mech
is
not
None
and
self
.
_mech
.
_decay_policy
is
not
None
:
self
.
_mech_param_updater
=
_MechanismsParamsUpdater
(
policy
=
self
.
_mech
.
_decay_policy
,
decay_rate
=
self
.
_mech
.
_noise_decay_rate
,
cur_params
=
self
.
_mech
.
_noise_multiplier
,
init_params
=
self
.
_mech
.
_initial_noise_multiplier
)
cur_noise_multiplier
=
self
.
_mech
.
_noise_multiplier
,
init_noise_multiplier
=
self
.
_mech
.
_initial_noise_multiplier
)
def
construct
(
self
,
data
,
label
,
sens
=
None
):
"""
...
...
@@ -416,8 +417,11 @@ class _TrainOneStepWithLossScaleCell(Cell):
loss
=
P
.
Div
()(
total_loss
,
self
.
_micro_float
)
if
self
.
_mech
is
not
None
:
grad_noise
=
self
.
_hyper_map
(
self
.
_mech
,
grads
)
grads
=
self
.
_tuple_add
(
grads
,
grad_noise
)
grad_noise_tuple
=
()
for
grad_item
in
grads
:
grad_noise
=
self
.
_mech
(
grad_item
)
grad_noise_tuple
=
grad_noise_tuple
+
(
grad_noise
,)
grads
=
self
.
_tuple_add
(
grads
,
grad_noise_tuple
)
grads
=
self
.
_hyper_map
(
F
.
partial
(
_grad_scale
,
self
.
_micro_float
),
grads
)
# update mech parameters
if
self
.
_mech_param_updater
is
not
None
:
...
...
@@ -517,8 +521,10 @@ class _TrainOneStepCell(Cell):
if
self
.
_mech
is
not
None
and
self
.
_mech
.
_decay_policy
is
not
None
:
self
.
_mech_param_updater
=
_MechanismsParamsUpdater
(
policy
=
self
.
_mech
.
_decay_policy
,
decay_rate
=
self
.
_mech
.
_noise_decay_rate
,
cur_params
=
self
.
_mech
.
_noise_multiplier
,
init_params
=
self
.
_mech
.
_initial_noise_multiplier
)
cur_noise_multiplier
=
self
.
_mech
.
_noise_multiplier
,
init_noise_multiplier
=
self
.
_mech
.
_initial_noise_multiplier
)
def
construct
(
self
,
data
,
label
):
"""
...
...
@@ -543,8 +549,11 @@ class _TrainOneStepCell(Cell):
loss
=
P
.
Div
()(
total_loss
,
self
.
_micro_float
)
if
self
.
_mech
is
not
None
:
grad_noise
=
self
.
_hyper_map
(
self
.
_mech
,
grads
)
grads
=
self
.
_tuple_add
(
grads
,
grad_noise
)
grad_noise_tuple
=
()
for
grad_item
in
grads
:
grad_noise
=
self
.
_mech
(
grad_item
)
grad_noise_tuple
=
grad_noise_tuple
+
(
grad_noise
,)
grads
=
self
.
_tuple_add
(
grads
,
grad_noise_tuple
)
grads
=
self
.
_hyper_map
(
F
.
partial
(
_grad_scale
,
self
.
_micro_float
),
grads
)
# update mech parameters
if
self
.
_mech_param_updater
is
not
None
:
...
...
tests/ut/python/diff_privacy/test_mechanisms.py
浏览文件 @
ac39d193
...
...
@@ -30,7 +30,7 @@ from mindarmour.diff_privacy import MechanismsFactory
@
pytest
.
mark
.
component_mindarmour
def
test_graph_gaussian
():
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
net
=
GaussianRandom
(
norm_bound
,
initial_noise_multiplier
)
...
...
@@ -44,7 +44,7 @@ def test_graph_gaussian():
@
pytest
.
mark
.
component_mindarmour
def
test_pynative_gaussian
():
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
net
=
GaussianRandom
(
norm_bound
,
initial_noise_multiplier
)
...
...
@@ -58,7 +58,7 @@ def test_pynative_gaussian():
@
pytest
.
mark
.
component_mindarmour
def
test_graph_ada_gaussian
():
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
...
...
@@ -75,7 +75,7 @@ def test_graph_ada_gaussian():
@
pytest
.
mark
.
component_mindarmour
def
test_graph_factory
():
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
...
...
@@ -102,7 +102,7 @@ def test_graph_factory():
@
pytest
.
mark
.
component_mindarmour
def
test_pynative_ada_gaussian
():
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
...
...
@@ -119,7 +119,7 @@ def test_pynative_ada_gaussian():
@
pytest
.
mark
.
component_mindarmour
def
test_pynative_factory
():
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
3
,
2
,
4
],
mstype
.
float32
)
grad
=
Tensor
([
0.3
,
0.2
,
0.
4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
...
...
@@ -138,3 +138,45 @@ def test_pynative_factory():
decay_policy
=
decay_policy
)
ada_noise
=
ada_noise_construct
(
grad
)
print
(
'ada noise: '
,
ada_noise
)
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_pynative_exponential
():
context
.
set_context
(
mode
=
context
.
PYNATIVE_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
0.3
,
0.2
,
0.4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
decay_policy
=
'Exp'
ada_mechanism
=
MechanismsFactory
()
ada_noise_construct
=
ada_mechanism
.
create
(
'AdaGaussian'
,
norm_bound
,
initial_noise_multiplier
,
noise_decay_rate
=
alpha
,
decay_policy
=
decay_policy
)
ada_noise
=
ada_noise_construct
(
grad
)
print
(
'ada noise: '
,
ada_noise
)
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_graph_exponential
():
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"Ascend"
)
grad
=
Tensor
([
0.3
,
0.2
,
0.4
],
mstype
.
float32
)
norm_bound
=
1.0
initial_noise_multiplier
=
0.1
alpha
=
0.5
decay_policy
=
'Exp'
ada_mechanism
=
MechanismsFactory
()
ada_noise_construct
=
ada_mechanism
.
create
(
'AdaGaussian'
,
norm_bound
,
initial_noise_multiplier
,
noise_decay_rate
=
alpha
,
decay_policy
=
decay_policy
)
ada_noise
=
ada_noise_construct
(
grad
)
print
(
'ada noise: '
,
ada_noise
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录