Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
e8a3d925
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看板
提交
e8a3d925
编写于
9月 07, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
9月 07, 2020
浏览文件
操作
浏览文件
下载
差异文件
!107 Remove is_grad of nn.SoftmaxCrossEntropyWithLogits method; Fix changes of parallel method
Merge pull request !107 from pkuliuliu/master
上级
a4d2c8ea
b38cfbc3
变更
31
隐藏空白更改
内联
并排
Showing
31 changed file
with
44 addition
and
55 deletion
+44
-55
example/membership_inference_demo/eval.py
example/membership_inference_demo/eval.py
+1
-1
example/membership_inference_demo/main.py
example/membership_inference_demo/main.py
+1
-1
example/membership_inference_demo/train.py
example/membership_inference_demo/train.py
+1
-1
example/mnist_demo/lenet5_dp.py
example/mnist_demo/lenet5_dp.py
+1
-2
example/mnist_demo/lenet5_dp_ada_gaussian.py
example/mnist_demo/lenet5_dp_ada_gaussian.py
+1
-2
example/mnist_demo/lenet5_dp_ada_sgd_graph.py
example/mnist_demo/lenet5_dp_ada_sgd_graph.py
+1
-2
example/mnist_demo/lenet5_dp_optimizer.py
example/mnist_demo/lenet5_dp_optimizer.py
+1
-1
example/mnist_demo/lenet5_mnist_coverage.py
example/mnist_demo/lenet5_mnist_coverage.py
+1
-1
example/mnist_demo/mnist_attack_fgsm.py
example/mnist_demo/mnist_attack_fgsm.py
+1
-1
example/mnist_demo/mnist_attack_lbfgs.py
example/mnist_demo/mnist_attack_lbfgs.py
+1
-1
example/mnist_demo/mnist_attack_mdi2fgsm.py
example/mnist_demo/mnist_attack_mdi2fgsm.py
+1
-1
example/mnist_demo/mnist_attack_pgd.py
example/mnist_demo/mnist_attack_pgd.py
+1
-1
example/mnist_demo/mnist_defense_nad.py
example/mnist_demo/mnist_defense_nad.py
+1
-1
example/mnist_demo/mnist_evaluation.py
example/mnist_demo/mnist_evaluation.py
+1
-1
example/mnist_demo/mnist_train.py
example/mnist_demo/mnist_train.py
+1
-2
mindarmour/attacks/gradient_method.py
mindarmour/attacks/gradient_method.py
+1
-2
mindarmour/attacks/iterative_gradient_method.py
mindarmour/attacks/iterative_gradient_method.py
+1
-1
mindarmour/attacks/lbfgs.py
mindarmour/attacks/lbfgs.py
+1
-1
mindarmour/defenses/adversarial_defense.py
mindarmour/defenses/adversarial_defense.py
+2
-2
mindarmour/diff_privacy/mechanisms/mechanisms.py
mindarmour/diff_privacy/mechanisms/mechanisms.py
+1
-1
mindarmour/diff_privacy/train/model.py
mindarmour/diff_privacy/train/model.py
+4
-4
tests/ut/python/attacks/test_gradient_method.py
tests/ut/python/attacks/test_gradient_method.py
+1
-1
tests/ut/python/defenses/mock_net.py
tests/ut/python/defenses/mock_net.py
+1
-1
tests/ut/python/defenses/test_ad.py
tests/ut/python/defenses/test_ad.py
+1
-1
tests/ut/python/defenses/test_ead.py
tests/ut/python/defenses/test_ead.py
+1
-1
tests/ut/python/defenses/test_nad.py
tests/ut/python/defenses/test_nad.py
+1
-1
tests/ut/python/defenses/test_pad.py
tests/ut/python/defenses/test_pad.py
+1
-1
tests/ut/python/diff_privacy/test_membership_inference.py
tests/ut/python/diff_privacy/test_membership_inference.py
+3
-3
tests/ut/python/diff_privacy/test_model_train.py
tests/ut/python/diff_privacy/test_model_train.py
+3
-3
tests/ut/python/diff_privacy/test_monitor.py
tests/ut/python/diff_privacy/test_monitor.py
+6
-12
tests/ut/python/fuzzing/test_coverage_metrics.py
tests/ut/python/fuzzing/test_coverage_metrics.py
+1
-1
未找到文件。
example/membership_inference_demo/eval.py
浏览文件 @
e8a3d925
...
...
@@ -116,7 +116,7 @@ def test(cloud_args=None):
net
=
vgg16
(
num_classes
=
args
.
num_classes
,
args
=
args
)
opt
=
Momentum
(
filter
(
lambda
x
:
x
.
requires_grad
,
net
.
get_parameters
()),
0.01
,
args
.
momentum
,
weight_decay
=
args
.
weight_decay
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
'mean'
,
is_grad
=
False
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
'mean'
)
model
=
Model
(
net
,
loss_fn
=
loss
,
optimizer
=
opt
,
metrics
=
{
'acc'
})
param_dict
=
load_checkpoint
(
args
.
pre_trained
)
...
...
example/membership_inference_demo/main.py
浏览文件 @
e8a3d925
...
...
@@ -59,7 +59,7 @@ if __name__ == "__main__":
# load the pretrained model
net
=
vgg16
(
args
.
num_classes
,
args
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
opt
=
nn
.
Momentum
(
params
=
get_param_groups
(
net
),
learning_rate
=
0.1
,
momentum
=
0.9
,
weight_decay
=
args
.
weight_decay
,
loss_scale
=
args
.
loss_scale
)
load_param_into_net
(
net
,
load_checkpoint
(
args
.
pre_trained
))
...
...
example/membership_inference_demo/train.py
浏览文件 @
e8a3d925
...
...
@@ -182,7 +182,7 @@ if __name__ == '__main__':
weight_decay
=
args
.
weight_decay
,
loss_scale
=
args
.
loss_scale
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
'mean'
,
is_grad
=
False
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
'mean'
)
model
=
Model
(
network
,
loss_fn
=
loss
,
optimizer
=
opt
,
metrics
=
{
'acc'
},
amp_level
=
"O2"
,
keep_batchnorm_fp32
=
False
,
loss_scale_manager
=
None
)
...
...
example/mnist_demo/lenet5_dp.py
浏览文件 @
e8a3d925
...
...
@@ -91,8 +91,7 @@ if __name__ == "__main__":
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
cfg
.
device_target
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
cfg
.
save_checkpoint_steps
,
keep_checkpoint_max
=
cfg
.
keep_checkpoint_max
)
...
...
example/mnist_demo/lenet5_dp_ada_gaussian.py
浏览文件 @
e8a3d925
...
...
@@ -90,8 +90,7 @@ if __name__ == "__main__":
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
cfg
.
device_target
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
cfg
.
save_checkpoint_steps
,
keep_checkpoint_max
=
cfg
.
keep_checkpoint_max
)
...
...
example/mnist_demo/lenet5_dp_ada_sgd_graph.py
浏览文件 @
e8a3d925
...
...
@@ -90,8 +90,7 @@ if __name__ == "__main__":
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
cfg
.
device_target
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
cfg
.
save_checkpoint_steps
,
keep_checkpoint_max
=
cfg
.
keep_checkpoint_max
)
...
...
example/mnist_demo/lenet5_dp_optimizer.py
浏览文件 @
e8a3d925
...
...
@@ -89,7 +89,7 @@ def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1,
if
__name__
==
"__main__"
:
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
cfg
.
device_target
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
cfg
.
save_checkpoint_steps
,
keep_checkpoint_max
=
cfg
.
keep_checkpoint_max
)
ckpoint_cb
=
ModelCheckpoint
(
prefix
=
"checkpoint_lenet"
,
...
...
example/mnist_demo/lenet5_mnist_coverage.py
浏览文件 @
e8a3d925
...
...
@@ -73,7 +73,7 @@ def test_lenet_mnist_coverage():
LOGGER
.
info
(
TAG
,
'SNAC of this test is : %s'
,
model_fuzz_test
.
get_snac
())
# generate adv_data
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
FastGradientSignMethod
(
net
,
eps
=
0.3
,
loss_fn
=
loss
)
adv_data
=
attack
.
batch_generate
(
test_images
,
test_labels
,
batch_size
=
32
)
model_fuzz_test
.
calculate_coverage
(
adv_data
,
bias_coefficient
=
0.5
)
...
...
example/mnist_demo/mnist_attack_fgsm.py
浏览文件 @
e8a3d925
...
...
@@ -75,7 +75,7 @@ def test_fast_gradient_sign_method():
LOGGER
.
info
(
TAG
,
"prediction accuracy before attacking is : %s"
,
accuracy
)
# attacking
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
FastGradientSignMethod
(
net
,
eps
=
0.3
,
loss_fn
=
loss
)
start_time
=
time
.
clock
()
adv_data
=
attack
.
batch_generate
(
np
.
concatenate
(
test_images
),
...
...
example/mnist_demo/mnist_attack_lbfgs.py
浏览文件 @
e8a3d925
...
...
@@ -83,7 +83,7 @@ def test_lbfgs_attack():
targeted_labels
[
i
]
=
(
targeted_labels
[
i
]
+
1
)
%
10
else
:
targeted_labels
=
true_labels
.
astype
(
np
.
int32
)
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
LBFGS
(
net
,
is_targeted
=
is_targeted
,
loss_fn
=
loss
)
start_time
=
time
.
clock
()
adv_data
=
attack
.
batch_generate
(
np
.
concatenate
(
test_images
),
...
...
example/mnist_demo/mnist_attack_mdi2fgsm.py
浏览文件 @
e8a3d925
...
...
@@ -77,7 +77,7 @@ def test_momentum_diverse_input_iterative_method():
LOGGER
.
info
(
TAG
,
"prediction accuracy before attacking is : %s"
,
accuracy
)
# attacking
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
MomentumDiverseInputIterativeMethod
(
net
,
loss_fn
=
loss
)
start_time
=
time
.
clock
()
adv_data
=
attack
.
batch_generate
(
np
.
concatenate
(
test_images
),
...
...
example/mnist_demo/mnist_attack_pgd.py
浏览文件 @
e8a3d925
...
...
@@ -75,7 +75,7 @@ def test_projected_gradient_descent_method():
LOGGER
.
info
(
TAG
,
"prediction accuracy before attacking is : %s"
,
accuracy
)
# attacking
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
ProjectedGradientDescent
(
net
,
eps
=
0.3
,
loss_fn
=
loss
)
start_time
=
time
.
clock
()
adv_data
=
attack
.
batch_generate
(
np
.
concatenate
(
test_images
),
...
...
example/mnist_demo/mnist_defense_nad.py
浏览文件 @
e8a3d925
...
...
@@ -48,7 +48,7 @@ def test_nad_method():
ds_train
=
generate_mnist_dataset
(
os
.
path
.
join
(
mnist_path
,
"train"
),
batch_size
=
batch_size
,
repeat_size
=
1
)
net
=
LeNet5
()
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
opt
=
nn
.
Momentum
(
net
.
trainable_params
(),
0.01
,
0.09
)
model
=
Model
(
net
,
loss
,
opt
,
metrics
=
None
)
model
.
train
(
10
,
ds_train
,
callbacks
=
[
LossMonitor
()],
...
...
example/mnist_demo/mnist_evaluation.py
浏览文件 @
e8a3d925
...
...
@@ -164,7 +164,7 @@ def test_black_defense():
wb_model
=
ModelToBeAttacked
(
wb_net
)
# gen white-box adversarial examples of test data
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
wb_attack
=
FastGradientSignMethod
(
wb_net
,
eps
=
0.3
,
loss_fn
=
loss
)
wb_adv_sample
=
wb_attack
.
generate
(
attacked_sample
,
attacked_true_label
)
...
...
example/mnist_demo/mnist_train.py
浏览文件 @
e8a3d925
...
...
@@ -38,8 +38,7 @@ def mnist_train(epoch_size, batch_size, lr, momentum):
batch_size
=
batch_size
,
repeat_size
=
1
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
lr
,
momentum
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
1875
,
keep_checkpoint_max
=
10
)
...
...
mindarmour/attacks/gradient_method.py
浏览文件 @
e8a3d925
...
...
@@ -73,8 +73,7 @@ class GradientMethod(Attack):
else
:
self
.
_alpha
=
alpha
if
loss_fn
is
None
:
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
False
)
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
False
)
with_loss_cell
=
WithLossCell
(
self
.
_network
,
loss_fn
)
self
.
_grad_all
=
GradWrapWithLoss
(
with_loss_cell
)
self
.
_grad_all
.
set_train
()
...
...
mindarmour/attacks/iterative_gradient_method.py
浏览文件 @
e8a3d925
...
...
@@ -129,7 +129,7 @@ class IterativeGradientMethod(Attack):
for
b
in
self
.
_bounds
:
_
=
check_param_multi_types
(
'bound'
,
b
,
[
int
,
float
])
if
loss_fn
is
None
:
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
False
)
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
False
)
self
.
_loss_grad
=
GradWrapWithLoss
(
WithLossCell
(
self
.
_network
,
loss_fn
))
self
.
_loss_grad
.
set_train
()
...
...
mindarmour/attacks/lbfgs.py
浏览文件 @
e8a3d925
...
...
@@ -66,7 +66,7 @@ class LBFGS(Attack):
self
.
_nb_iter
=
check_int_positive
(
'nb_iter'
,
nb_iter
)
self
.
_search_iters
=
check_int_positive
(
'search_iters'
,
search_iters
)
if
loss_fn
is
None
:
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
False
)
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
False
)
with_loss_cell
=
WithLossCell
(
self
.
_network
,
loss_fn
)
self
.
_grad_all
=
GradWrapWithLoss
(
with_loss_cell
)
self
.
_dtype
=
None
...
...
mindarmour/defenses/adversarial_defense.py
浏览文件 @
e8a3d925
...
...
@@ -58,7 +58,7 @@ class AdversarialDefense(Defense):
>>> net = Net()
>>> lr = 0.0001
>>> momentum = 0.9
>>> loss_fn = SoftmaxCrossEntropyWithLogits(
is_grad=False,
sparse=True)
>>> loss_fn = SoftmaxCrossEntropyWithLogits(sparse=True)
>>> optimizer = Momentum(net.trainable_params(), lr, momentum)
>>> adv_defense = AdversarialDefense(net, loss_fn, optimizer)
>>> inputs = np.random.rand(32, 1, 28, 28).astype(np.float32)
...
...
@@ -70,7 +70,7 @@ class AdversarialDefense(Defense):
super
(
AdversarialDefense
,
self
).
__init__
(
network
)
network
=
check_model
(
'network'
,
network
,
Cell
)
if
loss_fn
is
None
:
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss_fn
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
if
optimizer
is
None
:
optimizer
=
Momentum
(
...
...
mindarmour/diff_privacy/mechanisms/mechanisms.py
浏览文件 @
e8a3d925
...
...
@@ -123,7 +123,7 @@ class NoiseMechanismsFactory:
>>> batch_size = 32
>>> batches = 128
>>> epochs = 1
>>> loss = nn.SoftmaxCrossEntropyWithLogits(
is_grad=False,
sparse=True)
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
>>> noise_mech = NoiseMechanismsFactory().create('Gaussian',
>>> norm_bound=norm_bound,
>>> initial_noise_multiplier=initial_noise_multiplier)
...
...
mindarmour/diff_privacy/train/model.py
浏览文件 @
e8a3d925
...
...
@@ -39,7 +39,7 @@ from mindspore.ops.operations import NPUClearFloatStatus
from
mindspore.ops.operations
import
ReduceSum
from
mindspore.ops.operations
import
LessEqual
from
mindspore.ops.operations
import
ControlDepend
from
mindspore.parallel._utils
import
_get_
mirror
_mean
from
mindspore.parallel._utils
import
_get_
gradients
_mean
from
mindspore.parallel._utils
import
_get_device_num
from
mindspore.nn.wrap.grad_reducer
import
DistributedGradReducer
from
mindspore.common.parameter
import
Parameter
...
...
@@ -93,7 +93,7 @@ class DPModel(Model):
>>> batches = 128
>>> epochs = 1
>>> micro_batches = 2
>>> loss = nn.SoftmaxCrossEntropyWithLogits(
is_grad=False,
sparse=True)
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
>>> factory_opt = DPOptimizerClassFactory(micro_batches=micro_batches)
>>> factory_opt.set_mechanisms('Gaussian',
>>> norm_bound=norm_bound,
...
...
@@ -405,7 +405,7 @@ class _TrainOneStepWithLossScaleCell(Cell):
self
.
reducer_flag
=
self
.
parallel_mode
in
[
ParallelMode
.
DATA_PARALLEL
,
ParallelMode
.
HYBRID_PARALLEL
]
if
self
.
reducer_flag
:
mean
=
_get_
mirror
_mean
()
mean
=
_get_
gradients
_mean
()
degree
=
_get_device_num
()
self
.
grad_reducer
=
DistributedGradReducer
(
optimizer
.
parameters
,
mean
,
degree
)
...
...
@@ -611,7 +611,7 @@ class _TrainOneStepCell(Cell):
ParallelMode
.
DATA_PARALLEL
,
ParallelMode
.
HYBRID_PARALLEL
):
self
.
reducer_flag
=
True
if
self
.
reducer_flag
:
mean
=
_get_
mirror
_mean
()
mean
=
_get_
gradients
_mean
()
degree
=
_get_device_num
()
self
.
grad_reducer
=
DistributedGradReducer
(
optimizer
.
parameters
,
mean
,
degree
)
...
...
tests/ut/python/attacks/test_gradient_method.py
浏览文件 @
e8a3d925
...
...
@@ -111,7 +111,7 @@ def test_fast_gradient_method_cpu():
input_np
=
np
.
asarray
([[
0.1
,
0.2
,
0.7
]],
np
.
float32
)
label
=
np
.
asarray
([
2
],
np
.
int32
)
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
FastGradientMethod
(
Net
(),
loss_fn
=
loss
)
ms_adv_x
=
attack
.
generate
(
input_np
,
label
)
...
...
tests/ut/python/defenses/mock_net.py
浏览文件 @
e8a3d925
...
...
@@ -95,7 +95,7 @@ if __name__ == '__main__':
attack
.
generate
(
inputs_np
,
labels_np
)
# test train ops
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
sparse
)
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
sparse
)
optimizer
=
Momentum
(
filter
(
lambda
x
:
x
.
requires_grad
,
net
.
get_parameters
()),
0.01
,
0.9
)
loss_net
=
WithLossCell
(
net
,
loss_fn
)
...
...
tests/ut/python/defenses/test_ad.py
浏览文件 @
e8a3d925
...
...
@@ -52,7 +52,7 @@ def test_ad():
labels
=
np
.
eye
(
num_classes
)[
labels
].
astype
(
np
.
float32
)
net
=
Net
()
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
sparse
)
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
sparse
)
optimizer
=
Momentum
(
learning_rate
=
Tensor
(
np
.
array
([
0.001
],
np
.
float32
)),
momentum
=
0.9
,
params
=
net
.
trainable_params
())
...
...
tests/ut/python/defenses/test_ead.py
浏览文件 @
e8a3d925
...
...
@@ -54,7 +54,7 @@ def test_ead():
labels
=
np
.
eye
(
num_classes
)[
labels
].
astype
(
np
.
float32
)
net
=
Net
()
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
sparse
)
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
sparse
)
optimizer
=
Momentum
(
net
.
trainable_params
(),
0.001
,
0.9
)
net
=
Net
()
...
...
tests/ut/python/defenses/test_nad.py
浏览文件 @
e8a3d925
...
...
@@ -52,7 +52,7 @@ def test_nad():
labels
=
np
.
eye
(
num_classes
)[
labels
].
astype
(
np
.
float32
)
net
=
Net
()
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
sparse
)
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
sparse
)
optimizer
=
Momentum
(
net
.
trainable_params
(),
0.001
,
0.9
)
# defense
...
...
tests/ut/python/defenses/test_pad.py
浏览文件 @
e8a3d925
...
...
@@ -53,7 +53,7 @@ def test_pad():
# construct network
net
=
Net
()
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
sparse
)
loss_fn
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
sparse
)
optimizer
=
Momentum
(
net
.
trainable_params
(),
0.001
,
0.9
)
# defense
...
...
tests/ut/python/diff_privacy/test_membership_inference.py
浏览文件 @
e8a3d925
...
...
@@ -48,7 +48,7 @@ def dataset_generator(batch_size, batches):
@
pytest
.
mark
.
component_mindarmour
def
test_get_membership_inference_object
():
net
=
Net
()
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
opt
=
nn
.
Momentum
(
params
=
net
.
trainable_params
(),
learning_rate
=
0.1
,
momentum
=
0.9
)
model
=
Model
(
network
=
net
,
loss_fn
=
loss
,
optimizer
=
opt
)
inference_model
=
MembershipInference
(
model
)
...
...
@@ -62,7 +62,7 @@ def test_get_membership_inference_object():
@
pytest
.
mark
.
component_mindarmour
def
test_membership_inference_object_train
():
net
=
Net
()
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
opt
=
nn
.
Momentum
(
params
=
net
.
trainable_params
(),
learning_rate
=
0.1
,
momentum
=
0.9
)
model
=
Model
(
network
=
net
,
loss_fn
=
loss
,
optimizer
=
opt
)
inference_model
=
MembershipInference
(
model
)
...
...
@@ -92,7 +92,7 @@ def test_membership_inference_object_train():
@
pytest
.
mark
.
component_mindarmour
def
test_membership_inference_eval
():
net
=
Net
()
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
opt
=
nn
.
Momentum
(
params
=
net
.
trainable_params
(),
learning_rate
=
0.1
,
momentum
=
0.9
)
model
=
Model
(
network
=
net
,
loss_fn
=
loss
,
optimizer
=
opt
)
inference_model
=
MembershipInference
(
model
)
...
...
tests/ut/python/diff_privacy/test_model_train.py
浏览文件 @
e8a3d925
...
...
@@ -53,7 +53,7 @@ def test_dp_model_with_pynative_mode():
batches
=
128
epochs
=
1
micro_batches
=
2
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
factory_opt
=
DPOptimizerClassFactory
(
micro_batches
=
micro_batches
)
factory_opt
.
set_mechanisms
(
'Gaussian'
,
norm_bound
=
norm_bound
,
...
...
@@ -92,7 +92,7 @@ def test_dp_model_with_graph_mode():
batch_size
=
32
batches
=
128
epochs
=
1
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
noise_mech
=
NoiseMechanismsFactory
().
create
(
'Gaussian'
,
norm_bound
=
norm_bound
,
initial_noise_multiplier
=
initial_noise_multiplier
)
...
...
@@ -131,7 +131,7 @@ def test_dp_model_with_graph_mode_ada_gaussian():
batches
=
128
epochs
=
1
alpha
=
0.8
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
noise_mech
=
NoiseMechanismsFactory
().
create
(
'AdaGaussian'
,
norm_bound
=
norm_bound
,
initial_noise_multiplier
=
initial_noise_multiplier
,
...
...
tests/ut/python/diff_privacy/test_monitor.py
浏览文件 @
e8a3d925
...
...
@@ -58,8 +58,7 @@ def test_dp_monitor():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
@@ -88,8 +87,7 @@ def test_dp_monitor_gpu():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
@@ -118,8 +116,7 @@ def test_dp_monitor_cpu():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
@@ -149,8 +146,7 @@ def test_dp_monitor_zcdp():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
@@ -179,8 +175,7 @@ def test_dp_monitor_zcdp_gpu():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
@@ -209,8 +204,7 @@ def test_dp_monitor_zcdp_cpu():
LOGGER
.
info
(
TAG
,
'The recommended maximum training epochs is: %s'
,
suggest_epoch
)
network
=
LeNet5
()
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
)
...
...
tests/ut/python/fuzzing/test_coverage_metrics.py
浏览文件 @
e8a3d925
...
...
@@ -83,7 +83,7 @@ def test_lenet_mnist_coverage_cpu():
LOGGER
.
info
(
TAG
,
'SNAC of this test is : %s'
,
model_fuzz_test
.
get_snac
())
# generate adv_data
loss
=
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
)
loss
=
SoftmaxCrossEntropyWithLogits
(
sparse
=
True
)
attack
=
FastGradientSignMethod
(
net
,
eps
=
0.3
,
loss_fn
=
loss
)
adv_data
=
attack
.
batch_generate
(
test_data
,
test_labels
,
batch_size
=
32
)
model_fuzz_test
.
calculate_coverage
(
adv_data
,
bias_coefficient
=
0.5
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录