test_attacker.py 3.1 KB
Newer Older
L
liuluobin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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 mindarmour.diff_privacy.evaluation.attacker import get_attack_model


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
26
@pytest.mark.platform_arm_ascend_training
L
liuluobin 已提交
27 28 29 30 31 32 33 34
@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": {
35
            "n_neighbors": [3],
L
liuluobin 已提交
36 37
        }
    }
38
    knn_attacker = get_attack_model(features, labels, config_knn, -1)
L
liuluobin 已提交
39 40 41 42 43 44
    pred = knn_attacker.predict(features)
    assert pred is not None


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
45
@pytest.mark.platform_arm_ascend_training
L
liuluobin 已提交
46 47 48 49 50 51 52 53 54 55 56
@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),
        }
    }
57
    lr_attacker = get_attack_model(features, labels, config_lr, -1)
L
liuluobin 已提交
58 59 60 61 62 63
    pred = lr_attacker.predict(features)
    assert pred is not None


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
64
@pytest.mark.platform_arm_ascend_training
L
liuluobin 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
@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],
        }
    }
78
    mlpc_attacker = get_attack_model(features, labels, config_mlpc, -1)
L
liuluobin 已提交
79 80 81 82 83 84
    pred = mlpc_attacker.predict(features)
    assert pred is not None


@pytest.mark.level0
@pytest.mark.platform_x86_ascend_training
85
@pytest.mark.platform_arm_ascend_training
L
liuluobin 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
@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],
        }
    }
101
    rf_attacker = get_attack_model(features, labels, config_rf, -1)
L
liuluobin 已提交
102 103
    pred = rf_attacker.predict(features)
    assert pred is not None