def test_train_internal(self): mechanism = NoisyHist1() aopt = DPSniper( mechanism, LogisticRegressionFactory(in_dimensions=2, optimizer_factory=AdamOptimizerFactory(), n_test_batches=1), DDConfig(n_train=30, training_batch_size=10)) classifier = aopt._train_classifier(np.array([5, 3]), np.array([4, 3])) self.assertIsInstance(classifier, StableClassifier)
def test_ml_attack_optimizer_nd_nd(self): mechanism = NoisyHist1() aopt = DPSniper( mechanism, LogisticRegressionFactory( in_dimensions=2, optimizer_factory=AdamOptimizerFactory()), DDConfig(n_train=10)) attack = aopt.best_attack(np.array([5, 3]), np.array([4, 3])) res = attack.check(np.array([[5.1, 3.0], [3.2, 4.3], [0.1, 2.8]])) self.assertIsInstance(attack, MlAttack) self.assertEqual((3, ), res.shape)
def test_ml_attack_optimizer_nd_1d(self): mechanism = ReportNoisyMax1() aopt = DPSniper( mechanism, LogisticRegressionFactory( in_dimensions=1, optimizer_factory=AdamOptimizerFactory()), DDConfig(n_train=10)) attack = aopt.best_attack(np.array([0.4, 2.8]), np.array([1.4, 1.8])) res = attack.check(np.array([0])) self.assertIsInstance(attack, MlAttack) self.assertEqual((1, ), res.shape)
def test_run(self): with initialize_parallel_executor(2, get_output_directory()): config = DDConfig(n_train=10, n=10, n_check=10, n_final=10, n_processes=2) mechanism = ConstantMechanism() optimizer_factory = SGDOptimizerFactory() classifier_factory = LogisticRegressionFactory( in_dimensions=1, optimizer_factory=optimizer_factory) attack_optimizer = DPSniper(mechanism, classifier_factory, config) input_generator = SingleInputPairGenerator() optimizer = DDSearch(mechanism, attack_optimizer, input_generator, config) best = optimizer.run() self.assertEqual(best.a1, 20) self.assertEqual(best.a2, 20) self.assertEqual(best.eps, 0)
def run(self): log.info("using configuration %s", self.config) attack_opt = DPSniper(self.mechanism, self.classifier_factory, self.config) with time_measure("time_dd_search"): log.debug("running dd-search...") opt = DDSearch(self.mechanism, attack_opt, self.input_pair_sampler, self.config) res = opt.run() log.debug("finished dd-search, preliminary eps=%f", res.eps) with time_measure("time_final_estimate_eps"): log.debug("computing final eps estimate...") res.compute_eps_high_precision(self.mechanism, self.config) log.info("done!") log.info("> a1 = {}".format(res.a1)) log.info("> a2 = {}".format(res.a2)) log.info("> attack = {}".format(res.attack)) log.info("> eps = {}".format(res.eps)) log.info("> eps lcb = {}".format(res.lower_bound)) log.data("eps", res.eps) log.data("eps_lcb", res.lower_bound)
from dpsniper.input.pattern_generator import PatternGenerator from dpsniper.mechanisms.laplace import LaplaceMechanism from dpsniper.utils.initialization import initialize_dpsniper if __name__ == "__main__": # create mechanism mechanism = LaplaceMechanism() # configuration # use Logistic regression with stochastic gradient descent optimization as the underlying machine-learning algorithm classifier_factory = LogisticRegressionFactory( in_dimensions=1, optimizer_factory=SGDOptimizerFactory()) # consider 1-dimensional floating point input pairs from the range [-10, 10] with maximum distance of 1 input_generator = PatternGenerator( InputDomain(1, InputBaseType.FLOAT, [-10, 10]), False) # adapt the number of processes to fit your machine config = DDConfig(n_processes=2) with initialize_dpsniper(config, out_dir="example_outputs"): # run DD-Search to find the witness witness = DDSearch(mechanism, DPSniper(mechanism, classifier_factory, config), input_generator, config).run() # re-compute the power of the witness using high precision for a tighter lower confidence bound witness.compute_eps_high_precision(mechanism, config) print("eps_lcb = {}".format(witness.lower_bound)) print("witness = ({}, {}, {})".format(witness.a1, witness.a2, witness.attack))
def test_find_threshold_6(self): probs = np.array([0.9, 0.8, 0.6, 0.6, 0.6, 0.6, 0.3]) target = 0.0 thresh, q = DPSniper._find_threshold(probs, target) self.assertEqual(0.9, thresh) self.assertEqual(0.0, q)
def test_find_threshold_1(self): probs = np.array([0.9, 0.8, 0.6, 0.6, 0.6, 0.6, 0.3]) target = 3.2 thresh, q = DPSniper._find_threshold(probs, target) self.assertEqual(0.6, thresh) self.assertAlmostEqual(0.3, q)