unlearning done
This commit is contained in:
124
Tune_new.py
124
Tune_new.py
@@ -9,11 +9,9 @@ import Util
|
||||
from sets.Data import *
|
||||
from sets.IdentitySubset import IdentitySubset
|
||||
from architectures.Model import Model, Architecture
|
||||
from unlearning.CertifiedRemoval import CertifiedRemoval
|
||||
from unlearning.CertifiedUnlearning import CertifiedUnlearning
|
||||
from unlearning.LinearFiltration import LinearFiltration
|
||||
from unlearning.WeightFiltration import WeightFiltration
|
||||
from unlearning.WF import WeightF
|
||||
|
||||
|
||||
# Global Hyperparameters
|
||||
@@ -140,40 +138,12 @@ def run_unlearning_and_strategy_eval(env_dict, forget_class_idx, strategy, evalu
|
||||
train_data = env_dict["train_data"]
|
||||
test_data = env_dict["test_data"]
|
||||
|
||||
# testing valuse * *
|
||||
#---------------------------------------------------------------------------
|
||||
# S1 50 5 5 5 5 5
|
||||
# S2 1000 200 1000 500 200 300
|
||||
# BS 5 5 5 5 5 5
|
||||
# scale 2000 500 8000 5000 10000 8000
|
||||
# std 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
|
||||
|
||||
# Initialize the strategy hyperparameters matching standard settings
|
||||
# increase s2, decrease scale ---sweet spot
|
||||
'''certified_removal = CertifiedRemoval(
|
||||
target_class_index=forget_class_idx,
|
||||
s1=4,
|
||||
s2=350, # 350 best
|
||||
unlearn_bs=5,
|
||||
scale=6000.0, # 6000 was good
|
||||
std=0.00001
|
||||
)'''
|
||||
'''certified_removal = CertifiedUnlearning(
|
||||
target_class_index=0,
|
||||
l2_reg=0.0005,
|
||||
gamma=0.1,
|
||||
scale=7000.0,
|
||||
s1=2,
|
||||
s2=350,
|
||||
std=1e-5,
|
||||
unlearn_bs=2
|
||||
)'''
|
||||
|
||||
# Segment specific unlearning loaders using class index boundaries
|
||||
forget_train_loader, retain_train_loader = get_unlearning_loaders(
|
||||
retain_train_loader , forget_train_loader= get_unlearning_loaders(
|
||||
dataset=train_data, forget_class_idx=forget_class_idx, batch_size=BATCH_SIZE
|
||||
)
|
||||
forget_test_loader, retain_test_loader = get_unlearning_loaders(
|
||||
retain_test_loader, forget_test_loader = get_unlearning_loaders(
|
||||
dataset=test_data, forget_class_idx=forget_class_idx, batch_size=BATCH_SIZE
|
||||
)
|
||||
|
||||
@@ -189,9 +159,16 @@ def run_unlearning_and_strategy_eval(env_dict, forget_class_idx, strategy, evalu
|
||||
print("fine tunned model loaded into evaluation sandbox")
|
||||
|
||||
# Execute strategic parameter unlearning step
|
||||
strategy.apply(reloaded.model, forget_train_loader, retain_train_loader)
|
||||
unlearned = strategy.apply(reloaded.model, train_data)
|
||||
strategy_in_use = strategy.__class__.__name__
|
||||
|
||||
if isinstance(unlearned,nn.Module):
|
||||
reloaded.model = unlearned
|
||||
else:
|
||||
reloaded = unlearned
|
||||
|
||||
|
||||
|
||||
# Define validation tracking steps dynamically
|
||||
evaluation_domains = [
|
||||
{"loader": retain_test_loader, "mode": "retain", "label": "\n--- Performance on Retained Classes"},
|
||||
@@ -215,66 +192,63 @@ def run_unlearning_and_strategy_eval(env_dict, forget_class_idx, strategy, evalu
|
||||
# entry
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Run Data Infrastructure and Architecture Builder
|
||||
runtime_environment = prepare_data_and_model_environment()
|
||||
|
||||
# Baseline Evaluation
|
||||
finetuning = False
|
||||
# switch finetuning for tests on strategies only
|
||||
run_finetuning_or_baseline_eval(runtime_environment, run_training=finetuning)
|
||||
|
||||
finetuning = True
|
||||
# Unlearning Iterations
|
||||
for i in range(0, 1):
|
||||
|
||||
# strategies
|
||||
#
|
||||
#certified_removal = CertifiedRemoval(
|
||||
# target_class_index=i,
|
||||
# s1=4,
|
||||
# s2=350, # 350 best
|
||||
# unlearn_bs=5,
|
||||
# scale=6000.0, # 6000 was good
|
||||
# std=0.00009
|
||||
# )
|
||||
try:
|
||||
# Run Data Infrastructure and Architecture Builder
|
||||
runtime_environment = prepare_data_and_model_environment()
|
||||
|
||||
|
||||
|
||||
# Baseline Evaluation
|
||||
finetuning = False
|
||||
# switch finetuning for tests on strategies only
|
||||
run_finetuning_or_baseline_eval(runtime_environment, run_training = finetuning)
|
||||
|
||||
|
||||
# strategies
|
||||
certified_unlearning = CertifiedUnlearning(
|
||||
target_class_index=i,
|
||||
target_class_index=0,
|
||||
l2_reg=0.000002,
|
||||
gamma=0.1,
|
||||
scale= 20000,# 16400.0, # took ages to reach this sweet spot
|
||||
scale= 16400.0,# 16400.0, # took ages to reach this sweet spot
|
||||
s1=2,
|
||||
s2=300,
|
||||
std=0.00001,
|
||||
unlearn_bs=16
|
||||
unlearn_bs=8
|
||||
)
|
||||
|
||||
# works perfectly
|
||||
linear_filtration = LinearFiltration(
|
||||
|
||||
target_class_index=i
|
||||
target_class_index=0
|
||||
)
|
||||
|
||||
weight_filtration = WeightF( #WeightFiltration(
|
||||
target_class_index=i,
|
||||
epochs=3,
|
||||
lr=0.05,
|
||||
gamma=5
|
||||
weight_filtration = WeightFiltration(
|
||||
target_class_index=0,
|
||||
epochs=6,
|
||||
lr=150.0,
|
||||
gamma=0.001
|
||||
)
|
||||
|
||||
strategies = [
|
||||
# certified_unlearning,
|
||||
certified_unlearning,
|
||||
weight_filtration,
|
||||
# linear_filtration
|
||||
linear_filtration
|
||||
]
|
||||
# Unlearning Iteration
|
||||
for i in range(0, CLASS_SIZE):
|
||||
|
||||
print(f"\n>>> Executing Unlearning Framework for Target Identity Index: {i} <<<")
|
||||
for strategy in strategies:
|
||||
run_unlearning_and_strategy_eval(
|
||||
runtime_environment,
|
||||
forget_class_idx=i,
|
||||
strategy=strategy,
|
||||
evaluate= not finetuning
|
||||
)
|
||||
for strategy in strategies:
|
||||
|
||||
# update target class to be unlearned
|
||||
strategy.set_target_class(i)
|
||||
print(f"Unlearning class {i} with {strategy.strategy_name}")
|
||||
|
||||
# forget
|
||||
run_unlearning_and_strategy_eval(
|
||||
runtime_environment,
|
||||
forget_class_idx=i,
|
||||
strategy=strategy,
|
||||
evaluate = not finetuning
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("program interrupted. Exit!")
|
||||
|
||||
Reference in New Issue
Block a user