No project description provided
Project description
Torch adversarial
Optimizing inputs to models
Installation
To install this small tool from the source code
pip install git+https://github.com/cat-claws/torchadversarial/
Advantages
You can use these attack codes for more tasks than classification. For example, FGSM can be used to attack object detection etc.
How to use
import torch
from torchadversarial import Fgsm
x = torch.rand(4, 3, 112, 112)
z = x.clone()
z.requires_grad = True
opt = Fgsm([z], epsilon = 0.1)
y = z.sum()
y.backward()
opt.step()
To use as an adversarial attack
import torch
from torchadversarial import Fgsm, Attack
x = torch.rand(4, 3, 112, 112)
for x_ in Attack(Fgsm, [x], epsilon = 0.1, foreach = False, maximize = True):
y = torch.sum(x_[0])
y.backward()
# print(x_[0])
Note that x_[0] will be your adversarial example. Like other optimizers in torch.optim, the input parameters, e.g., [x] must be an iterable containing tensors. Thus, we must take x_[0] as our output in this example above.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file torchadversarial-0.0.1.tar.gz.
File metadata
- Download URL: torchadversarial-0.0.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34b63bd14a7f5dcecae827d6ccbae02d0289ea2bd627aece6381efc70256f269
|
|
| MD5 |
c0b56f6fafb847bca77962ae483c02a4
|
|
| BLAKE2b-256 |
514edc2f5749913e6dce92e361d8b05bbf7f8964a0f8fcf2a5bb5440ad2eee8a
|