class torchattacks.attack.Attack(name, model)[source]

Base class for all attacks.


It automatically set device to the device where given model is. It basically changes training mode to eval during attack process. To change this, please see set_model_training_mode.

forward(inputs, labels=None, *args, **kwargs)[source]

It defines the computation performed at every call. Should be overridden by all subclasses.


Get attack mode.

get_target_label(inputs, labels=None)[source]

Function for changing the attack mode. Return input labels.

save(data_loader, save_path=None, verbose=True, return_verbose=False, save_predictions=False, save_clean_inputs=False, save_type='float')[source]

Save adversarial inputs as torch.tensor from given

  • save_path (str) – save_path.

  • data_loader ( – data loader.

  • verbose (bool) – True for displaying detailed information. (Default: True)

  • return_verbose (bool) – True for returning detailed information. (Default: False)

  • save_predictions (bool) – True for saving predicted labels (Default: False)

  • save_clean_inputs (bool) – True for saving clean inputs (Default: False)

static to_type(inputs, type)[source]

Return inputs as int if float is given.