Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Defense without Forgetting_Continual Adversarial Defense with Anisotropic and Isotropic Pseudo Replay - Reproduction
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mina Moshfegh
Defense without Forgetting_Continual Adversarial Defense with Anisotropic and Isotropic Pseudo Replay - Reproduction
Commits
affe77ae
Commit
affe77ae
authored
3 weeks ago
by
Mina Moshfegh
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
1edee6ea
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/attacks/fgsm.py
+46
-0
46 additions, 0 deletions
src/attacks/fgsm.py
with
46 additions
and
0 deletions
src/attacks/fgsm.py
0 → 100644
+
46
−
0
View file @
affe77ae
import
torch
import
torch.nn
as
nn
from
.base_attack
import
BaseAttack
# This class implements the basic FGSM (Fast Gradient Sign Method) attack.
# It's a straightforward single-step approach.
class
FGSMAttack
(
BaseAttack
):
def
__init__
(
self
,
model
,
epsilon
=
0.3
,
clamp_min
=
0
,
clamp_max
=
1
):
# We call the parent constructor to store the model
super
().
__init__
(
model
)
# Epsilon controls how big the adversarial step is
self
.
epsilon
=
epsilon
self
.
clamp_min
=
clamp_min
self
.
clamp_max
=
clamp_max
def
generate
(
self
,
x
,
y
):
# We'll find which device the model is currently on,
# so we can move input data there
torch
.
set_grad_enabled
(
True
)
device
=
next
(
self
.
model
.
parameters
()).
device
x
=
x
.
to
(
device
)
y
=
y
.
to
(
device
)
# Make a copy of x that allows gradient calculation
x
=
x
.
clone
().
detach
().
requires_grad_
(
True
)
# Forward pass: get predictions and compute the loss
logits
=
self
.
model
(
x
)
loss_fn
=
nn
.
CrossEntropyLoss
()
loss
=
loss_fn
(
logits
,
y
)
# Backprop to calculate gradient
loss
.
backward
()
# FGSM step: move in the direction of the sign of the gradient
x_adv
=
x
+
self
.
epsilon
*
x
.
grad
.
sign
()
# Clamp values back into valid range
x_adv
=
torch
.
clamp
(
x_adv
,
self
.
clamp_min
,
self
.
clamp_max
)
# Zero out the gradient so it doesn't affect future operations
x
.
grad
.
zero_
()
return
x_adv
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment