Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
d270ac4c
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看板
提交
d270ac4c
编写于
8月 14, 2020
作者:
L
liuluobin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add attacker of MembershipInference
上级
e47ed4e1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
238 addition
and
0 deletion
+238
-0
mindarmour/diff_privacy/evaluation/attacker.py
mindarmour/diff_privacy/evaluation/attacker.py
+130
-0
tests/ut/python/diff_privacy/test_attacker.py
tests/ut/python/diff_privacy/test_attacker.py
+108
-0
未找到文件。
mindarmour/diff_privacy/evaluation/attacker.py
0 → 100644
浏览文件 @
d270ac4c
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Attacker of Membership Inference.
"""
from
sklearn.neighbors
import
KNeighborsClassifier
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.neural_network
import
MLPClassifier
from
sklearn.ensemble
import
RandomForestClassifier
from
sklearn.model_selection
import
GridSearchCV
from
sklearn.model_selection
import
RandomizedSearchCV
def
_attack_knn
(
features
,
labels
,
param_grid
):
"""
Train and return a KNN model.
Args:
features (numpy.ndarray): Loss and logits characteristics of each sample.
labels (numpy.ndarray): Labels of each sample whether belongs to training set.
param_grid (dict): Setting of GridSearchCV.
Returns:
sklearn.neighbors.KNeighborsClassifier, trained model.
"""
knn_model
=
KNeighborsClassifier
()
knn_model
=
GridSearchCV
(
knn_model
,
param_grid
=
param_grid
,
cv
=
3
,
n_jobs
=
1
,
iid
=
False
,
verbose
=
0
,
)
knn_model
.
fit
(
X
=
features
,
y
=
labels
)
return
knn_model
def
_attack_lr
(
features
,
labels
,
param_grid
):
"""
Train and return a LR model.
Args:
features (numpy.ndarray): Loss and logits characteristics of each sample.
labels (numpy.ndarray): Labels of each sample whether belongs to training set.
param_grid (dict): Setting of GridSearchCV.
Returns:
sklearn.linear_model.LogisticRegression, trained model.
"""
lr_model
=
LogisticRegression
(
C
=
1.0
,
penalty
=
"l2"
)
lr_model
=
GridSearchCV
(
lr_model
,
param_grid
=
param_grid
,
cv
=
3
,
n_jobs
=
1
,
iid
=
False
,
verbose
=
0
,
)
lr_model
.
fit
(
X
=
features
,
y
=
labels
)
return
lr_model
def
_attack_mlpc
(
features
,
labels
,
param_grid
):
"""
Train and return a MLPC model.
Args:
features (numpy.ndarray): Loss and logits characteristics of each sample.
labels (numpy.ndarray): Labels of each sample whether belongs to training set.
param_grid (dict): Setting of GridSearchCV.
Returns:
sklearn.neural_network.MLPClassifier, trained model.
"""
mlpc_model
=
MLPClassifier
(
random_state
=
1
,
max_iter
=
300
)
mlpc_model
=
GridSearchCV
(
mlpc_model
,
param_grid
=
param_grid
,
cv
=
3
,
n_jobs
=
1
,
iid
=
False
,
verbose
=
0
,
)
mlpc_model
.
fit
(
features
,
labels
)
return
mlpc_model
def
_attack_rf
(
features
,
labels
,
random_grid
):
"""
Train and return a RF model.
Args:
features (numpy.ndarray): Loss and logits characteristics of each sample.
labels (numpy.ndarray): Labels of each sample whether belongs to training set.
random_grid (dict): Setting of RandomizedSearchCV.
Returns:
sklearn.ensemble.RandomForestClassifier, trained model.
"""
rf_model
=
RandomForestClassifier
(
max_depth
=
2
,
random_state
=
0
)
rf_model
=
RandomizedSearchCV
(
rf_model
,
param_distributions
=
random_grid
,
n_iter
=
7
,
cv
=
3
,
n_jobs
=
1
,
iid
=
False
,
verbose
=
0
,
)
rf_model
.
fit
(
features
,
labels
)
return
rf_model
def
get_attack_model
(
features
,
labels
,
config
):
"""
Get trained attack model specify by config.
Args:
features (numpy.ndarray): Loss and logits characteristics of each sample.
labels (numpy.ndarray): Labels of each sample whether belongs to training set.
config (dict): Config of attacker, with key in ["method", "params"].
Returns:
sklearn.BaseEstimator, trained model specify by config["method"].
"""
method
=
str
.
lower
(
config
[
"method"
])
if
method
==
"knn"
:
return
_attack_knn
(
features
,
labels
,
config
[
"params"
])
if
method
==
"LR"
:
return
_attack_lr
(
features
,
labels
,
config
[
"params"
])
if
method
==
"MLP"
:
return
_attack_mlpc
(
features
,
labels
,
config
[
"params"
])
if
method
==
"RF"
:
return
_attack_rf
(
features
,
labels
,
config
[
"params"
])
raise
ValueError
(
"Method {} is not support."
.
format
(
config
[
"method"
]))
tests/ut/python/diff_privacy/test_attacker.py
0 → 100644
浏览文件 @
d270ac4c
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
attacker test
"""
import
pytest
import
numpy
as
np
from
sklearn.neighbors
import
KNeighborsClassifier
as
knn
from
sklearn.linear_model
import
LogisticRegression
from
sklearn.neural_network
import
MLPClassifier
from
sklearn.ensemble
import
RandomForestClassifier
from
mindarmour.diff_privacy.evaluation.attacker
import
get_attack_model
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_get_knn_model
():
features
=
np
.
random
.
randint
(
0
,
10
,
[
10
,
10
])
labels
=
np
.
random
.
randint
(
0
,
2
,
[
10
])
config_knn
=
{
"method"
:
"KNN"
,
"params"
:
{
"n_neighbors"
:
[
3
,
5
,
7
],
}
}
knn_attacker
=
get_attack_model
(
features
,
labels
,
config_knn
)
assert
isinstance
(
knn_attacker
,
knn
)
pred
=
knn_attacker
.
predict
(
features
)
assert
pred
is
not
None
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_get_lr_model
():
features
=
np
.
random
.
randint
(
0
,
10
,
[
10
,
10
])
labels
=
np
.
random
.
randint
(
0
,
2
,
[
10
])
config_lr
=
{
"method"
:
"LR"
,
"params"
:
{
"C"
:
np
.
logspace
(
-
4
,
2
,
10
),
}
}
lr_attacker
=
get_attack_model
(
features
,
labels
,
config_lr
)
assert
isinstance
(
lr_attacker
,
LogisticRegression
)
pred
=
lr_attacker
.
predict
(
features
)
assert
pred
is
not
None
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_get_mlp_model
():
features
=
np
.
random
.
randint
(
0
,
10
,
[
10
,
10
])
labels
=
np
.
random
.
randint
(
0
,
2
,
[
10
])
config_mlpc
=
{
"method"
:
"MLP"
,
"params"
:
{
"hidden_layer_sizes"
:
[(
64
,),
(
32
,
32
)],
"solver"
:
[
"adam"
],
"alpha"
:
[
0.0001
,
0.001
,
0.01
],
}
}
mlpc_attacker
=
get_attack_model
(
features
,
labels
,
config_mlpc
)
assert
isinstance
(
mlpc_attacker
,
MLPClassifier
)
pred
=
mlpc_attacker
.
predict
(
features
)
assert
pred
is
not
None
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_ascend_training
@
pytest
.
mark
.
env_onecard
@
pytest
.
mark
.
component_mindarmour
def
test_get_rf_model
():
features
=
np
.
random
.
randint
(
0
,
10
,
[
10
,
10
])
labels
=
np
.
random
.
randint
(
0
,
2
,
[
10
])
config_rf
=
{
"method"
:
"RF"
,
"params"
:
{
"n_estimators"
:
[
100
],
"max_features"
:
[
"auto"
,
"sqrt"
],
"max_depth"
:
[
5
,
10
,
20
,
None
],
"min_samples_split"
:
[
2
,
5
,
10
],
"min_samples_leaf"
:
[
1
,
2
,
4
],
}
}
rf_attacker
=
get_attack_model
(
features
,
labels
,
config_rf
)
assert
isinstance
(
rf_attacker
,
RandomForestClassifier
)
pred
=
rf_attacker
.
predict
(
features
)
assert
pred
is
not
None
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录