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
953b4e4c
Commit
953b4e4c
authored
3 weeks ago
by
Mina Moshfegh
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
212f5df2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/defenses/feat_extraction.py
+32
-0
32 additions, 0 deletions
src/defenses/feat_extraction.py
with
32 additions
and
0 deletions
src/defenses/feat_extraction.py
0 → 100644
+
32
−
0
View file @
953b4e4c
import
torch
import
torch.nn.functional
as
F
from
.base_defense
import
BaseDefense
# FeatureExtractionDefense class implements feature extraction for regularizing the student model
class
FeatureExtractionDefense
(
BaseDefense
):
def
__init__
(
self
,
student_model
,
teacher_model
=
None
,
feat_lambda
=
1.0
,
attack
=
None
):
super
().
__init__
(
student_model
,
teacher_model
)
# Initialize base class
self
.
feat_lambda
=
feat_lambda
# Regularization weight for feature extraction loss
self
.
attack
=
attack
# Standard deviation for noise added to inputs
# Check if the student model has the required method for feature extraction
if
not
hasattr
(
student_model
,
"
forward_features
"
):
raise
ValueError
(
"
Student model must define forward_features(x) for feature extraction defense.
"
)
# Loss function combining cross-entropy loss and feature extraction loss
def
loss_function
(
self
,
x
,
y
,
**
kwargs
):
logits_clean
=
self
.
student_model
(
x
)
# Get logits from student model for clean inputs
loss_ce
=
F
.
cross_entropy
(
logits_clean
,
y
)
# Compute cross-entropy loss
x_noisy
=
self
.
attack
.
generate
(
x
,
y
)
# Extract features for both clean and noisy inputs
feats_clean
=
self
.
student_model
.
forward_features
(
x
)
feats_noisy
=
self
.
student_model
.
forward_features
(
x_noisy
)
# Compute feature extraction loss (MSE between clean and noisy features)
loss_feat
=
F
.
mse_loss
(
feats_clean
,
feats_noisy
)
total_loss
=
loss_ce
+
self
.
feat_lambda
*
loss_feat
# Combine the losses
return
total_loss
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