Repository navigation
Expand file tree
/
Copy pathtask_selection_token.py
More file actions
296 lines (197 loc) · 9.4 KB
/
Copy pathtask_selection_token.py
File metadata and controls
296 lines (197 loc) · 9.4 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from datasets import Dataset, DatasetDict
from transformers import AutoTokenizer, get_scheduler
from adapters import AutoAdapterModel, AdapterConfig
import torch
from torch.utils.data import DataLoader
from torch.optim import AdamW
from torch.nn.functional import l1_loss
import pandas as pd
import numpy as np
from tqdm import tqdm
import os
import json
import csv
import matplotlib.pyplot as plt
def load_dataframes():
path = "/pvc/home/apprentissage-profond/data/"
# Load train_test_df and validation
train_test_df = pd.read_parquet(path+"train_test_df.parquet")
validation_df = pd.read_parquet(path+"validation_df.parquet")
# Load mean and std used for normalizing the data
with open(path+"normalization_stats.json") as f:
stats = json.load(f)
mean = stats["mean"]
std = stats["std"]
print("Loading dataset")
print(f"Mean : {mean}, std : {std}")
print("Loading successful")
return (train_test_df, validation_df, mean, std)
def tokenize_data(dataset, platform_tokens):
tokenizer = AutoTokenizer.from_pretrained("roberta-base")
tokenizer.add_special_tokens({'additional_special_tokens': ["<tw>", "<rd>"]})
def add_platform_prefix(row):
tok = platform_tokens[int(row["platform"])]
row["text"] = f"{tok} {row['text']}"
return row
dataset_prefixed = dataset.map(add_platform_prefix)
tokenized_dataset = dataset_prefixed.map(
lambda df: tokenizer(df["text"], truncation=True, padding="max_length", max_length=512),
batched=True,
remove_columns=['text','platform', 'comment_id', '__index_level_0__']
)
return tokenized_dataset, tokenizer
def load_model(red_factor):
print(f'Loading model with reduction factor = {red_factor}')
model = AutoAdapterModel.from_pretrained("roberta-base")
config = AdapterConfig.load("houlsby", reduction_factor=int(red_factor))
model.add_adapter("linear_reg", config=config)
model.add_classification_head("linear_reg", num_labels=1, layers=1, activation_function="identity")
model.train_adapter(["linear_reg"])
model.set_active_adapters("linear_reg")
model.active_head = "linear_reg"
model.get_input_embeddings().weight.requires_grad = True
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"{trainable_params:,} / {total_params:,} parameters are trainable "
f"→ {trainable_params / total_params:.2%} of the model.")
return model
def train_epoch(model, train_dataloader, optimizer, scheduler, device, mean, std):
model.train()
epoch_loss = 0
epoch_mae = 0
n_samples = 0
for batch in tqdm(train_dataloader):
batch = {k: v.to(device) for k, v in batch.items()}
optimizer.zero_grad()
outputs = model(**batch)
loss = outputs.loss
loss.backward()
#prevent update of the embeddings of the original vocab by cancelling their gradients
with torch.no_grad():
grad = model.roberta.embeddings.word_embeddings.weight.grad
if grad is not None:
grad[:-2] = 0
optimizer.step()
scheduler.step()
epoch_loss += outputs.loss.item()
labels = batch['labels']
preds = outputs.logits.squeeze()
mae = l1_loss(preds, labels, reduction='sum')
n_samples += labels.size(0)
epoch_mae += mae.item()
epoch_loss /= len(train_dataloader)
epoch_mae /= n_samples
return epoch_loss, epoch_mae
def evaluate(model, test_dataloader, device, mean, std):
model.eval()
with torch.no_grad():
epoch_loss = 0
epoch_mae = 0
n_samples = 0
for batch in test_dataloader:
batch = {k: v.to(device) for k, v in batch.items()}
outputs = model(**batch)
epoch_loss += outputs.loss.item()
labels = batch['labels']
preds = outputs.logits.squeeze()
#de-normalize hate speech score if necessary
#labels = std*labels + mean
#preds = std*preds + mean
mae = l1_loss(preds, labels, reduction='sum')
n_samples += labels.size(0)
epoch_mae += mae.item()
epoch_loss /= len(test_dataloader)
epoch_mae /= n_samples
return epoch_loss, epoch_mae
def print_inference_samples(model, tokenizer, test_dataset, device):
model.eval()
sample_indices = np.random.choice(len(test_dataset), size=3, replace=False)
samples = [test_dataset[int(i)] for i in sample_indices]
for sample in samples:
input_ids = sample["input_ids"].unsqueeze(0).to(device)
attention_mask = sample["attention_mask"].unsqueeze(0).to(device)
label = sample["labels"].item()
with torch.no_grad():
output = model(input_ids=input_ids, attention_mask=attention_mask)
pred = output.logits.squeeze().item()
input_ids_cpu = input_ids.squeeze().cpu()
pad_token_id = tokenizer.pad_token_id
if pad_token_id in input_ids_cpu:
pad_index = (input_ids_cpu == pad_token_id).nonzero(as_tuple=True)[0][0].item()
input_ids_cpu = input_ids_cpu[:pad_index]
decoded = tokenizer.decode(input_ids_cpu, skip_special_tokens=False)
print("\n---")
print("Comment:", decoded)
print(f"True score: {label:.3f} | Predicted: {pred:.3f}")
def train_cycle(epochs=10, batch_size=100, dataset_size=8000, learning_rate=5e-5, red_factor=12):
platform_tokens = {
0: '<rd>',
2: '<tw>'
}
train_df, validation_df, mean, std = load_dataframes()
train_df = train_df.sample(int(dataset_size*0.9), replace=False)
validation_df = validation_df.sample(int(dataset_size*0.1), replace=False)
train_dataset = Dataset.from_pandas(train_df)
validation_dataset = Dataset.from_pandas(validation_df)
dataset_dict = DatasetDict({
"train": train_dataset,
"test": validation_dataset,
})
tokenized_dataset, tokenizer = tokenize_data(dataset_dict, platform_tokens)
tokenized_dataset = tokenized_dataset.rename_column("hate_speech_score", "labels") #compatibility with auto loss
tokenized_dataset.set_format("torch")
train_dataloader = DataLoader(tokenized_dataset['train'], shuffle=True, batch_size=batch_size)
test_dataloader = DataLoader(tokenized_dataset['test'], batch_size=batch_size)
model = load_model(red_factor)
model.resize_token_embeddings(len(tokenizer)) #adds two new embeddings
with torch.no_grad():
before = model.roberta.embeddings.word_embeddings.weight.detach().cpu().clone()
embed_weight = model.roberta.embeddings.word_embeddings.weight
optimizer = AdamW(
[
{"params": [embed_weight], "weight_decay": 0.0},
{"params": [p for p in model.parameters() if p is not embed_weight],
"weight_decay": 0.01},
],
lr=learning_rate,
)
num_training_steps = epochs*len(train_dataloader)
lr_scheduler = get_scheduler(
name="linear", optimizer=optimizer, num_warmup_steps=1, num_training_steps=num_training_steps
)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
losses = np.zeros((2, epochs))
metrics_df = pd.DataFrame(dtype=float, index=range(epochs), columns=['train_mae', 'validation_mae'])
for i in range(epochs):
train_loss, train_mae = train_epoch(model, train_dataloader, optimizer, lr_scheduler, device, mean, std)
eval_loss, eval_mae = evaluate(model, test_dataloader, device, mean, std)
loss_text_line = f'Epoch {i} eval loss: {eval_loss}, train_loss: {train_loss}\neval MAE: {eval_mae}, train MAE: {train_mae}'
print(loss_text_line)
print_inference_samples(model, tokenizer, tokenized_dataset["test"], device)
metrics_df.loc[i, ["train_mae", "validation_mae"]] = [train_mae, eval_mae]
losses[0,i] = train_mae
losses[1,i] = eval_mae
plt.figure(figsize=(12, 5))
plt.plot(losses[0, :i+1], c='blue', label='train')
plt.plot(losses[1, :i+1], c='orange', label='test')
plt.title('Mean Absolute Error')
plt.legend()
plt.savefig(f"/pvc/home/apprentissage-profond/results/mae_plot_embeddings.png")
plt.close()
results_path = "/pvc/home/apprentissage-profond/results/models"
metrics_df.to_csv(f"/pvc/home/apprentissage-profond/results/domain_tokens_output.csv", index=False)
torch.save(model.state_dict(), results_path + "model.pt")
model.save_adapter(results_path, "linear_reg", with_head=True)
with torch.no_grad():
after = model.roberta.embeddings.word_embeddings.weight.detach().cpu()
unchanged = torch.allclose(before[:-2], after[:-2], atol=0, rtol=0) #verify that the embeddings of the original vocabulary are not trained
print("Old-token embeddings exactly identical? ", unchanged)
def lr_sweep(start, stop, n_steps):
for lr in np.logspace(np.log10(start), np.log10(stop), n_steps):
train_cycle(5, 50, 15000, (lr, 5e-5), 8)
def reduction_sweep(start, stop, step):
for red_factor in np.arange(start, stop+step, step):
train_cycle(10, 100, 15000, red_factor=red_factor)
if __name__ == "__main__":
train_cycle(epochs=10, batch_size=50, dataset_size=15000, learning_rate=5e-5, red_factor=8) # epoch, batch_size, dataset_size per platform