当前位置: 首页>>代码示例>>Python>>正文


Python attacks.CarliniWagnerL2方法代码示例

本文整理汇总了Python中cleverhans.attacks.CarliniWagnerL2方法的典型用法代码示例。如果您正苦于以下问题:Python attacks.CarliniWagnerL2方法的具体用法?Python attacks.CarliniWagnerL2怎么用?Python attacks.CarliniWagnerL2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cleverhans.attacks的用法示例。


在下文中一共展示了attacks.CarliniWagnerL2方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cw_attack

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def cw_attack(sess, x, logits, n_ae, final=False):
    cw_attack_obj = CarliniWagnerL2(logits, sess=sess, back='tf')
    if final:
        cw_params = {'binary_search_steps': 9,
                     'max_iterations': 2000,
                     'learning_rate': 0.01,
                     'initial_const': 1.0,
                     'abort_early': True,
                     'batch_size': n_ae
                     }
    else:
        cw_params = {'binary_search_steps': 5,
                     'max_iterations': 500,
                     'learning_rate': 0.01,
                     'initial_const': 1.0,
                     'batch_size': n_ae  # need to specify, since CarliniWagnerL2 is not completely symbolic
                     }
    adv_ex_tensor = cw_attack_obj.generate(x, **cw_params)
    adv_ex_tensor = tf.stop_gradient(adv_ex_tensor)
    return adv_ex_tensor 
开发者ID:max-andr,项目名称:provable-robustness-max-linear-regions,代码行数:22,代码来源:attacks.py

示例2: setUp

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def setUp(self):
        super(TestCarliniWagnerL2, self).setUp()

        self.sess = tf.Session()
        self.model = SimpleModel()
        self.attack = CarliniWagnerL2(self.model, sess=self.sess) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:8,代码来源:test_attacks.py

示例3: test_generate_np_high_confidence_targeted_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def test_generate_np_high_confidence_targeted_examples(self):

        trivial_model = TrivialModel()

        for CONFIDENCE in [0, 2.3]:
            x_val = np.random.rand(10, 1) - .5
            x_val = np.array(x_val, dtype=np.float32)

            feed_labs = np.zeros((10, 2))
            feed_labs[np.arange(10), np.random.randint(0, 2, 10)] = 1
            attack = CarliniWagnerL2(trivial_model, sess=self.sess)
            x_adv = attack.generate_np(x_val,
                                       max_iterations=100,
                                       binary_search_steps=2,
                                       learning_rate=1e-2,
                                       initial_const=1,
                                       clip_min=-10, clip_max=10,
                                       confidence=CONFIDENCE,
                                       y_target=feed_labs,
                                       batch_size=10)

            new_labs = self.sess.run(trivial_model.get_logits(x_adv))

            good_labs = new_labs[np.arange(10), np.argmax(feed_labs, axis=1)]
            bad_labs = new_labs[np.arange(
                10), 1 - np.argmax(feed_labs, axis=1)]

            self.assertClose(CONFIDENCE, np.min(good_labs - bad_labs),
                             atol=1e-1)
            self.assertTrue(np.mean(np.argmax(new_labs, axis=1) ==
                                    np.argmax(feed_labs, axis=1)) > .9) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:33,代码来源:test_attacks.py

示例4: test_generate_np_high_confidence_untargeted_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def test_generate_np_high_confidence_untargeted_examples(self):

        trivial_model = TrivialModel()

        for CONFIDENCE in [0, 2.3]:
            x_val = np.random.rand(10, 1) - .5
            x_val = np.array(x_val, dtype=np.float32)

            orig_labs = np.argmax(self.sess.run(trivial_model.get_logits(x_val)), axis=1)
            attack = CarliniWagnerL2(trivial_model, sess=self.sess)
            x_adv = attack.generate_np(x_val,
                                       max_iterations=100,
                                       binary_search_steps=2,
                                       learning_rate=1e-2,
                                       initial_const=1,
                                       clip_min=-10, clip_max=10,
                                       confidence=CONFIDENCE,
                                       batch_size=10)

            new_labs = self.sess.run(trivial_model.get_logits(x_adv))

            good_labs = new_labs[np.arange(10), 1 - orig_labs]
            bad_labs = new_labs[np.arange(10), orig_labs]

            self.assertTrue(np.mean(np.argmax(new_labs, axis=1) == orig_labs)
                            == 0)
            self.assertTrue(np.isclose(
                0, np.min(good_labs - (bad_labs + CONFIDENCE)), atol=1e-1)) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:30,代码来源:test_attacks.py

示例5: setUp

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def setUp(self):
    super(TestCarliniWagnerL2, self).setUp()

    self.sess = tf.Session()
    self.model = SimpleModel()
    self.attack = CarliniWagnerL2(self.model, sess=self.sess) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:8,代码来源:test_attacks.py

示例6: test_generate_np_high_confidence_targeted_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def test_generate_np_high_confidence_targeted_examples(self):

    trivial_model = TrivialModel()

    for CONFIDENCE in [0, 2.3]:
      x_val = np.random.rand(10, 1) - .5
      x_val = np.array(x_val, dtype=np.float32)

      feed_labs = np.zeros((10, 2))
      feed_labs[np.arange(10), np.random.randint(0, 2, 10)] = 1
      attack = CarliniWagnerL2(trivial_model, sess=self.sess)
      x_adv = attack.generate_np(x_val,
                                 max_iterations=100,
                                 binary_search_steps=2,
                                 learning_rate=1e-2,
                                 initial_const=1,
                                 clip_min=-10, clip_max=10,
                                 confidence=CONFIDENCE,
                                 y_target=feed_labs,
                                 batch_size=10)

      new_labs = self.sess.run(trivial_model.get_logits(x_adv))

      good_labs = new_labs[np.arange(10), np.argmax(feed_labs, axis=1)]
      bad_labs = new_labs[np.arange(
          10), 1 - np.argmax(feed_labs, axis=1)]

      self.assertClose(CONFIDENCE, np.min(good_labs - bad_labs),
                       atol=1e-1)
      self.assertTrue(np.mean(np.argmax(new_labs, axis=1) ==
                              np.argmax(feed_labs, axis=1)) > .9) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:33,代码来源:test_attacks.py

示例7: test_generate_np_high_confidence_untargeted_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def test_generate_np_high_confidence_untargeted_examples(self):

    trivial_model = TrivialModel()

    for CONFIDENCE in [0, 2.3]:
      x_val = np.random.rand(10, 1) - .5
      x_val = np.array(x_val, dtype=np.float32)

      orig_labs = np.argmax(
          self.sess.run(trivial_model.get_logits(x_val)), axis=1)
      attack = CarliniWagnerL2(trivial_model, sess=self.sess)
      x_adv = attack.generate_np(x_val,
                                 max_iterations=100,
                                 binary_search_steps=2,
                                 learning_rate=1e-2,
                                 initial_const=1,
                                 clip_min=-10, clip_max=10,
                                 confidence=CONFIDENCE,
                                 batch_size=10)

      new_labs = self.sess.run(trivial_model.get_logits(x_adv))

      good_labs = new_labs[np.arange(10), 1 - orig_labs]
      bad_labs = new_labs[np.arange(10), orig_labs]

      self.assertTrue(np.mean(np.argmax(new_labs, axis=1) == orig_labs)
                      == 0)
      self.assertTrue(np.isclose(
          0, np.min(good_labs - (bad_labs + CONFIDENCE)), atol=1e-1)) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:31,代码来源:test_attacks.py

示例8: evaluate_ch

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import CarliniWagnerL2 [as 别名]
def evaluate_ch(model, config, sess, norm='l1', bound=None, verbose=True):
    dataset = config['data']
    num_eval_examples = config['num_eval_examples']
    eval_batch_size = config['eval_batch_size']

    if dataset == "mnist":
        from tensorflow.examples.tutorials.mnist import input_data
        mnist = input_data.read_data_sets('MNIST_data', one_hot=False)
        X = mnist.test.images[0:num_eval_examples, :].reshape(-1, 28, 28, 1)
        Y = mnist.test.labels[0:num_eval_examples]
        x_image = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
    else:
        import cifar10_input
        data_path = config["data_path"]
        cifar = cifar10_input.CIFAR10Data(data_path)
        X = cifar.eval_data.xs[0:num_eval_examples, :].astype(np.float32) / 255.0
        Y = cifar.eval_data.ys[0:num_eval_examples]
        x_image = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
        assert norm == 'l1'

    if norm=='l2':
        attack = CarliniWagnerL2(model, sess)
        params = {'batch_size': eval_batch_size, 'binary_search_steps': 9}
    else:
        attack = ElasticNetMethod(model, sess, clip_min=0.0, clip_max=1.0)
        params = {'beta': 1e-2,
                  'decision_rule': 'L1',
                  'batch_size': eval_batch_size,
                  'learning_rate': 1e-2,
                  'max_iterations': 1000}

    if verbose:
        set_log_level(logging.DEBUG, name="cleverhans")
    
    y = tf.placeholder(tf.int64, shape=[None, 10])
    params['y'] = y
    adv_x = attack.generate(x_image, **params)
    preds_adv = model.get_predicted_class(adv_x)
    preds_nat = model.get_predicted_class(x_image)

    all_preds, all_preds_adv, all_adv_x = batch_eval(
        sess, [x_image, y], [preds_nat, preds_adv, adv_x], [X, one_hot(Y, 10)], batch_size=eval_batch_size)

    print('acc nat', np.mean(all_preds == Y))
    print('acc adv', np.mean(all_preds_adv == Y))

    if dataset == "cifar10":
        X *= 255.0
        all_adv_x *= 255.0

    if norm == 'l2':
        lps = np.sqrt(np.sum(np.square(all_adv_x - X), axis=(1,2,3)))
    else:
        lps = np.sum(np.abs(all_adv_x - X), axis=(1,2,3))
    print('mean lp: ', np.mean(lps))
    for b in [bound, bound/2.0, bound/4.0, bound/8.0]:
        print('lp={}, acc={}'.format(b, np.mean((all_preds_adv == Y) | (lps > b))))

    all_corr_adv = (all_preds_adv == Y)
    all_corr_nat = (all_preds == Y)
    return all_corr_nat, all_corr_adv, lps 
开发者ID:ftramer,项目名称:MultiRobustness,代码行数:63,代码来源:eval_ch.py


注:本文中的cleverhans.attacks.CarliniWagnerL2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。