Skip to content

Commit

Permalink
commit 2: change the parameter name 'alpha' to 'alpha_ratio'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderling committed May 21, 2019
1 parent d4e1181 commit 2c67afa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions Attacks/AttackMethods/RFGSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class RFGSMAttack(Attack):

def __init__(self, model=None, epsilon=None, alpha=None):
def __init__(self, model=None, epsilon=None, alpha_ratio=None):
"""
:param model:
Expand All @@ -31,7 +31,7 @@ def __init__(self, model=None, epsilon=None, alpha=None):
self.model = model

self.epsilon = epsilon
self.alpha = alpha
self.alpha_ratio = alpha_ratio

def perturbation(self, samples, ys, device):
"""
Expand All @@ -45,10 +45,10 @@ def perturbation(self, samples, ys, device):
copy_samples = np.copy(samples)

# add randomized single-step attack
copy_samples = copy_samples + (self.alpha * self.epsilon * np.sign(np.random.randn(*copy_samples.shape)))
copy_samples = copy_samples + (self.alpha_ratio * self.epsilon * np.sign(np.random.randn(*copy_samples.shape)))
copy_samples = np.clip(copy_samples, 0.0, 1.0).astype(np.float32)

eps = (1.0 - self.alpha) * self.epsilon
eps = (1.0 - self.alpha_ratio) * self.epsilon
var_samples = tensor2variable(torch.from_numpy(copy_samples), device=device, requires_grad=True)
var_ys = tensor2variable(torch.LongTensor(ys), device=device)

Expand Down
8 changes: 4 additions & 4 deletions Attacks/AttackMethods/RLLC.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class RLLCAttack(Attack):

def __init__(self, model=None, epsilon=None, alpha=None):
def __init__(self, model=None, epsilon=None, alpha_ratio=None):
"""
:param model:
Expand All @@ -30,7 +30,7 @@ def __init__(self, model=None, epsilon=None, alpha=None):
self.model = model

self.epsilon = epsilon
self.alpha = alpha
self.alpha_ratio = alpha_ratio

def perturbation(self, samples, ys_target, device):
"""
Expand All @@ -42,13 +42,13 @@ def perturbation(self, samples, ys_target, device):
copy_samples = np.copy(samples)

# randomization
copy_samples = np.clip(copy_samples + self.alpha * self.epsilon * np.sign(np.random.randn(*copy_samples.shape)), 0.0, 1.0).astype(
copy_samples = np.clip(copy_samples + self.alpha_ratio * self.epsilon * np.sign(np.random.randn(*copy_samples.shape)), 0.0, 1.0).astype(
np.float32)

var_samples = tensor2variable(torch.from_numpy(copy_samples), device=device, requires_grad=True)
var_ys_target = tensor2variable(torch.from_numpy(ys_target), device)

eps = (1 - self.alpha) * self.epsilon
eps = (1 - self.alpha_ratio) * self.epsilon

self.model.eval()
preds = self.model(var_samples)
Expand Down
11 changes: 5 additions & 6 deletions Attacks/RFGSM_Generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
class RFGSMGeneration(Generation):

def __init__(self, dataset, attack_name, targeted, raw_model_location, clean_data_location, adv_examples_dir, device,
attack_batch_size, eps, alpha):
attack_batch_size, eps, alpha_ratio):
super(RFGSMGeneration, self).__init__(dataset, attack_name, targeted, raw_model_location, clean_data_location, adv_examples_dir, device)
self.attack_batch_size = attack_batch_size

self.epsilon = eps
self.alpha = alpha
self.alpha_ratio = alpha_ratio

def generate(self):
attacker = RFGSMAttack(model=self.raw_model, epsilon=self.epsilon, alpha=self.alpha)

attacker = RFGSMAttack(model=self.raw_model, epsilon=self.epsilon, alpha_ratio=self.alpha_ratio)
adv_samples = attacker.batch_perturbation(xs=self.nature_samples, ys=self.labels_samples, batch_size=self.attack_batch_size,
device=self.device)
# prediction for the adversarial examples
Expand Down Expand Up @@ -71,7 +70,7 @@ def main(args):

rfgsm = RFGSMGeneration(dataset=args.dataset, attack_name=name, targeted=targeted, raw_model_location=args.modelDir,
clean_data_location=args.cleanDir, adv_examples_dir=args.adv_saver, device=device,
eps=args.epsilon, attack_batch_size=args.attack_batch_size, alpha=args.alpha)
eps=args.epsilon, attack_batch_size=args.attack_batch_size, alpha_ratio=args.alpha_ratio)
rfgsm.generate()


Expand All @@ -89,7 +88,7 @@ def main(args):

# arguments for the particular attack
parser.add_argument('--epsilon', type=float, default=0.1, help='the epsilon value of RFGSM')
parser.add_argument('--alpha', type=float, default=0.5, help='the ratio of alpha related to epsilon in RFGSM ')
parser.add_argument('--alpha_ratio', type=float, default=0.5, help='the ratio of alpha related to epsilon in RFGSM')
parser.add_argument('--attack_batch_size', type=int, default=100, help='the default batch size for adversarial example generation')

arguments = parser.parse_args()
Expand Down
10 changes: 5 additions & 5 deletions Attacks/RLLC_Generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

class RLLCGeneration(Generation):
def __init__(self, dataset, attack_name, targeted, raw_model_location, clean_data_location, adv_examples_dir, device,
attack_batch_size, eps, alpha):
attack_batch_size, eps, alpha_ratio):
super(RLLCGeneration, self).__init__(dataset, attack_name, targeted, raw_model_location, clean_data_location, adv_examples_dir, device)
self.attack_batch_size = attack_batch_size

self.epsilon = eps
self.alpha = alpha
self.alpha_ratio = alpha_ratio

def generate(self):
attacker = RLLCAttack(model=self.raw_model, epsilon=self.epsilon, alpha=self.alpha)
attacker = RLLCAttack(model=self.raw_model, epsilon=self.epsilon, alpha_ratio=self.alpha_ratio)

# prepare the Least Likely Class labels
llc_labels = np.argmax(self.targets_samples, 1)
Expand Down Expand Up @@ -73,7 +73,7 @@ def main(args):

rllc = RLLCGeneration(dataset=args.dataset, attack_name=name, targeted=targeted, raw_model_location=args.modelDir,
clean_data_location=args.cleanDir, adv_examples_dir=args.adv_saver, device=device,
eps=args.epsilon, alpha=args.alpha, attack_batch_size=args.attack_batch_size)
eps=args.epsilon, alpha_ratio=args.alpha_ratio, attack_batch_size=args.attack_batch_size)
rllc.generate()


Expand All @@ -91,7 +91,7 @@ def main(args):

# arguments for the particular attack
parser.add_argument('--epsilon', type=float, default=0.1, help='the epsilon value of RLLC')
parser.add_argument('--alpha', type=float, default=0.5, help='the alpha value of RLLC')
parser.add_argument('--alpha_ratio', type=float, default=0.5, help='the ratio of alpha value ralated to epsilon in RLLC')
parser.add_argument('--attack_batch_size', type=int, default=100, help='the default batch size for adversarial example generation')

arguments = parser.parse_args()
Expand Down

0 comments on commit 2c67afa

Please sign in to comment.