def _run_threshold_entropy_attack(attack_input: AttackInputData): ntrain, ntest = attack_input.get_train_size(), attack_input.get_test_size() fpr, tpr, thresholds = metrics.roc_curve( np.concatenate((np.zeros(ntrain), np.ones(ntest))), np.concatenate((attack_input.get_entropy_train(), attack_input.get_entropy_test()))) roc_curve = RocCurve(tpr=tpr, fpr=fpr, thresholds=thresholds) return SingleAttackResult( slice_spec=_get_slice_spec(attack_input), data_size=DataSize(ntrain=ntrain, ntest=ntest), attack_type=AttackType.THRESHOLD_ENTROPY_ATTACK, membership_scores_train=-attack_input.get_entropy_train(), membership_scores_test=-attack_input.get_entropy_test(), roc_curve=roc_curve)
def test_get_entropy(self): attack_input = AttackInputData( logits_train=np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]), logits_test=np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]), labels_train=np.array([0, 2]), labels_test=np.array([0, 2])) np.testing.assert_equal(attack_input.get_entropy_train().tolist(), [0, 0]) np.testing.assert_equal(attack_input.get_entropy_test().tolist(), [2 * _log_value(0), 0]) attack_input = AttackInputData( logits_train=np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]), logits_test=np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])) np.testing.assert_equal(attack_input.get_entropy_train().tolist(), [0, 0]) np.testing.assert_equal(attack_input.get_entropy_test().tolist(), [0, 0])
def test_get_entropy_explicitly_provided(self): attack_input = AttackInputData( entropy_train=np.array([0.0, 2.0, 1.0]), entropy_test=np.array([0.5, 3.0, 5.0])) np.testing.assert_equal(attack_input.get_entropy_train().tolist(), [0.0, 2.0, 1.0]) np.testing.assert_equal(attack_input.get_entropy_test().tolist(), [0.5, 3.0, 5.0])