-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlearning_algorithm.py
79 lines (72 loc) · 3.67 KB
/
unlearning_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import torch
import torch.nn as nn
import torch.optim as optim
from transformers import AutoModelForSequenceClassification, AutoTokenizer
class LLMUnlearning:
def __init__(self, model_name):
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.criterion = nn.CrossEntropyLoss()
def fine_tune(self, train_dataloader, epochs=3, learning_rate=1e-5):
optimizer = optim.AdamW(self.model.parameters(), lr=learning_rate)
self.model.train()
for epoch in range(epochs):
for batch in train_dataloader:
inputs = self.tokenizer(batch['text'], return_tensors='pt', padding=True, truncation=True)
labels = batch['labels']
outputs = self.model(**inputs)
loss = self.criterion(outputs.logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def gradient_descent_unlearning(self, data_to_unlearn, learning_rate=1e-5):
optimizer = optim.AdamW(self.model.parameters(), lr=learning_rate)
self.model.train()
for data in data_to_unlearn:
inputs = self.tokenizer(data['text'], return_tensors='pt', padding=True, truncation=True)
labels = data['labels']
outputs = self.model(**inputs)
loss = self.criterion(outputs.logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def data_augmentation(self, augmented_data):
augmented_dataloader = torch.utils.data.DataLoader(augmented_data, batch_size=32, shuffle=True)
self.fine_tune(augmented_dataloader)
def regularization(self, train_dataloader, epochs=3, learning_rate=1e-5, weight_decay=0.01):
optimizer = optim.AdamW(self.model.parameters(), lr=learning_rate, weight_decay=weight_decay)
self.model.train()
for epoch in range(epochs):
for batch in train_dataloader:
inputs = self.tokenizer(batch['text'], return_tensors='pt', padding=True, truncation=True)
labels = batch['labels']
outputs = self.model(**inputs)
loss = self.criterion(outputs.logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def knowledge_distillation(self, teacher_model, student_model, train_dataloader, epochs=3, learning_rate=1e-5):
teacher_model.eval()
student_model.train()
optimizer = optim.AdamW(student_model.parameters(), lr=learning_rate)
for epoch in range(epochs):
for batch in train_dataloader:
inputs = self.tokenizer(batch['text'], return_tensors='pt', padding=True, truncation=True)
with torch.no_grad():
teacher_outputs = teacher_model(**inputs)
student_outputs = student_model(**inputs)
loss = self.criterion(student_outputs.logits, teacher_outputs.logits)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def selective_unlearning(self, data_to_unlearn, learning_rate=1e-5):
optimizer = optim.AdamW(self.model.parameters(), lr=learning_rate)
self.model.train()
for data in data_to_unlearn:
inputs = self.tokenizer(data['text'], return_tensors='pt', padding=True, truncation=True)
labels = data['labels']
outputs = self.model(**inputs)
loss = self.criterion(outputs.logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()