-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_multi_head_38.py
306 lines (271 loc) · 14.1 KB
/
train_multi_head_38.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import torch
import torch.nn as nn
from torch.nn import CrossEntropyLoss,BCEWithLogitsLoss
from transformers import RobertaModel, RobertaTokenizer
from transformers.modeling_outputs import TokenClassifierOutput,SequenceClassifierOutput
from torch.utils.data import DataLoader, Dataset
import datasets
import numpy
import os
import pandas
import sys
import tempfile
import torch
import transformers
import wandb
import argparse
from sklearn.metrics import f1_score, accuracy_score
from transformers import EvalPrediction
from tqdm.auto import tqdm
from typing import Optional, Tuple, Union
import torch.utils.checkpoint
from torch import nn
from torch.nn import CrossEntropyLoss,BCEWithLogitsLoss
from transformers.modeling_outputs import TokenClassifierOutput,SequenceClassifierOutput
from transformers.models.roberta.modeling_roberta import RobertaPreTrainedModel, RobertaModel, RobertaClassificationHead
from custom_models.multi_head import MultiHead_MultiLabel,MultiHead_MultiLabel_XL, MultiHead_MultiLabel_DebertaV2
wandb.init(
# set the wandb project where this run will be logged
project='touche',name='test')
#################################################################################################################################################################
#################################################################################################################################################################
def preprocess_function(examples):
batch = tokenizer(examples["Text"], padding='max_length', max_length=512, truncation=True,)
return batch
def load_dataset(directory, tokenizer, load_labels=True):
sentences_file_path = os.path.join(directory, "sentences.tsv")
labels_file_path = os.path.join(directory, "labels.tsv")
data_frame = pandas.read_csv(sentences_file_path, encoding="utf-8", sep="\t", header=0).dropna()
data_frame = datasets.Dataset.from_dict(data_frame)
encoded_sentences = data_frame.map( preprocess_function, batched=True, load_from_cache_file=False)
data_frame = pandas.DataFrame({'Text-ID':data_frame['Text-ID'],'Sentence-ID':data_frame['Sentence-ID'],'Text':data_frame['Text']})
if load_labels and os.path.isfile(labels_file_path):
labels_frame = pandas.read_csv(labels_file_path, encoding="utf-8", sep="\t", header=0)
labels_frame = pandas.merge(data_frame, labels_frame, on=["Text-ID", "Sentence-ID"])
###################################### FOR TASK 1 #########################################
# merged_df = pandas.DataFrame()
# merged_df['Text-ID'] = labels_frame['Text-ID']
# merged_df['Sentence-ID'] = labels_frame['Sentence-ID']
# merged_df['Text'] = labels_frame['Text']
# for col_name in labels_frame.columns:
# if col_name.endswith('attained'):
# prefix = col_name[:-9]
# constrained_col_name = f'{prefix} constrained'
# merged_df[prefix] = labels_frame[col_name] + labels_frame[constrained_col_name]
##############################################################################################
labels_matrix = numpy.zeros((labels_frame.shape[0], len(labels)))
for idx, label in enumerate(labels):
if label in labels_frame.columns:
labels_matrix[:, idx] = (labels_frame[label] >= 0.5).astype(int)
encoded_sentences = encoded_sentences.add_column("labels", labels_matrix.tolist())
# encoded_sentences["labels"] = labels_matrix.tolist()
# encoded_sentences = datasets.Dataset.from_dict(encoded_sentences)
return encoded_sentences, data_frame["Text-ID"].to_list(), data_frame["Sentence-ID"].to_list()
#################################################################################################################################################################
#################################################################################################################################################################
values = [ "Self-direction: thought", "Self-direction: action", "Stimulation", "Hedonism", "Achievement", "Power: dominance", "Power: resources", "Face", "Security: personal", "Security: societal", "Tradition", "Conformity: rules", "Conformity: interpersonal", "Humility", "Benevolence: caring", "Benevolence: dependability", "Universalism: concern", "Universalism: nature", "Universalism: tolerance" ]
labels = sum([[value + " attained", value + " constrained"] for value in values], [])
id2label = {idx:label for idx, label in enumerate(labels)}
label2id = {label:idx for idx, label in enumerate(labels)}
pretrained_model = 'facebook/xlm-roberta-xl'
# pretrained_model = 'xlm-roberta-large'
# pretrained_model = 'microsoft/deberta-v2-xxlarge'
tokenizer = transformers.AutoTokenizer.from_pretrained(pretrained_model)
training_dataset, training_text_ids, training_sentence_ids = load_dataset('/home/vasters/touche24/touche-Sotiris/data/final_training', tokenizer)
validation_dataset, validation_text_ids, validation_sentence_ids = load_dataset('/home/vasters/touche24/touche-Sotiris/data/final_validation', tokenizer)
#################################################################################################################################################################
#################################################################################################################################################################
def context_data_2(data,sep=' </s> '):
data=data.to_pandas()
progress_bar = tqdm(range(len(data)))
new_text=[]
for i in range(len(data)):
if data['Sentence-ID'][i]==2:
one_indices = [j for j, label in enumerate(data['labels'][i-1]) if label == 1]
if one_indices:
text = data['Text'][i-1]
for k in one_indices:
text=text+f' <{id2label[k]}>'
text = text+sep+data['Text'][i]
else:
text = data['Text'][i-1] + ' <NONE>'+ sep+data['Text'][i]
new_text.append(text)
elif data['Sentence-ID'][i]>2:
one_indices = [j for j, label in enumerate(data['labels'][i-1]) if label == 1]
two_indices = [j for j, label in enumerate(data['labels'][i-2]) if label == 1]
if two_indices:
text = data['Text'][i-2]
for k in two_indices:
text=text+f' <{id2label[k]}>'
else:
text = data['Text'][i-2] + ' <NONE>'
if one_indices:
text=text+ sep+ data['Text'][i-1]
for k in one_indices:
text=text+f' <{id2label[k]}>'
text=text+sep+data['Text'][i]
else:
text = text +sep+data['Text'][i-1]+' <NONE>'+sep+data['Text'][i]
new_text.append(text)
else:
new_text.append(data['Text'][i])
progress_bar.update(1)
data = data[['Text-ID', 'Sentence-ID', 'Text','labels']]
data['Text']=new_text
data=datasets.Dataset.from_pandas(data)
data = data.map(preprocess_function, batched=True, load_from_cache_file=False)
return data
def context_data_2_without_tokens(data):
data=data.to_pandas()
progress_bar = tqdm(range(len(data)))
new_text=[]
for i in range(len(data)):
if data['Sentence-ID'][i]==2:
text = data['Text'][i-1] +' </s> '+data['Text'][i]
new_text.append(text)
elif data['Sentence-ID'][i]>2:
text = data['Text'][i-2] +' </s> '+data['Text'][i-1]+' </s> '+data['Text'][i]
new_text.append(text)
else:
new_text.append(data['Text'][i])
progress_bar.update(1)
data = data[['Text-ID', 'Sentence-ID', 'Text','labels']]
data['Text']=new_text
data=datasets.Dataset.from_pandas(data)
data = data.map(preprocess_function, batched=True, load_from_cache_file=False)
return data
print('START CONTEXT TWO')
print(training_dataset)
print(validation_dataset)
additional_special_tokens = ["<NONE>"]+['<'+label+'>' for label in labels]
tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens})
print('START TRAIN DATA')
training_dataset=context_data_2(training_dataset)
print('START VALIDATION DATA')
validation_dataset=context_data_2(validation_dataset)
#################################################################################################################################################################
#################################################################################################################################################################
def multi_label_metrics(predictions, labels, thresholds=[0.1, 0.15, 0.20, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]):
sigmoid = torch.nn.Sigmoid()
probs = sigmoid(torch.Tensor(predictions))
y_true = labels
metrics = {}
best_scores = {str(label_idx): 0.0 for label_idx in range(labels.shape[1])}
best_threshold = {'threshold_'+str(label_idx): 0.0 for label_idx in range(labels.shape[1])}
max_f1=0
for threshold in thresholds:
# threshold_metrics = {}
y_pred = numpy.zeros(probs.shape)
y_pred[numpy.where(probs >= threshold)] = 1
f1=f1_score(y_true=y_true, y_pred=y_pred, average='macro')
metrics[f'f1_macro_{threshold}'] = f1
if f1>max_f1:
max_f1=f1
for label_idx in range(labels.shape[1]):
y_pred = (probs[:, label_idx] >= threshold).to(torch.int)
f1 = f1_score(y_true[:, label_idx], y_pred,average='binary')
if f1>best_scores[str(label_idx)]:
best_threshold['threshold_'+str(label_idx)]=threshold
best_scores[str(label_idx)] = max(best_scores[str(label_idx)], f1)
# threshold_metrics[f'label_{label_idx}_f1_{threshold}'] = f1
# metrics.update(threshold_metrics)
metrics.update(best_threshold)
metrics.update(best_scores)
mean_f1 = numpy.mean(list(best_scores.values()))
metrics['max_f1']=max_f1
metrics['mean_f1'] = mean_f1
return metrics
# def multi_label_metrics(predictions, labels, threshold=0.5):
# # first, apply sigmoid on predictions which are of shape (batch_size, num_labels)
# sigmoid = torch.nn.Sigmoid()
# probs = sigmoid(torch.Tensor(predictions))
# y_true = labels
# metrics = {}
# # next, use threshold to turn them into integer predictions
# max_f1=0
# for threshold in [0.1, 0.15, 0.20, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]:
# y_pred = numpy.zeros(probs.shape)
# y_pred[numpy.where(probs >= threshold)] = 1
# f1=f1_score(y_true=y_true, y_pred=y_pred, average='macro')
# metrics[f'f1_macro_{threshold}'] = f1
# if f1>max_f1:
# max_f1=f1
# metrics['max_f1']=max_f1
# return metrics
def compute_metrics(p: EvalPrediction):
preds = p.predictions[0] if isinstance(p.predictions,
tuple) else p.predictions
result = multi_label_metrics(
predictions=preds,
labels=p.label_ids)
return result
#################################################################################################################################################################
#################################################################################################################################################################
training_dataset=training_dataset.to_pandas()
validation_dataset=validation_dataset.to_pandas()
lang_dict={ 'EN': 0,
'EL': 1,
'DE': 2,
'TR': 3,
'FR': 4,
'BG': 5,
'HE': 6,
'IT': 7,
'NL': 8 }
id2lang_dict={v: k for k, v in lang_dict.items()}
training_dataset['language'] = training_dataset['Text-ID'].apply(lambda x: lang_dict[x[:2]])
validation_dataset['language'] = validation_dataset['Text-ID'].apply(lambda x: lang_dict[x[:2]])
training_dataset=datasets.Dataset.from_pandas(training_dataset)
# training_dataset=training_dataset.shuffle(seed=2024)
validation_dataset=datasets.Dataset.from_pandas(validation_dataset)
# training_dataset=datasets.concatenate_datasets([training_dataset,validation_dataset])
#################################################################################################################################################################
#################################################################################################################################################################
train_args = transformers.TrainingArguments(
output_dir='test-dir',
evaluation_strategy= 'epoch',
save_strategy= 'epoch',
save_total_limit = 2,
learning_rate=5e-6,
num_train_epochs=20,
push_to_hub=False,
metric_for_best_model = 'mean_f1',
greater_is_better=True,
weight_decay=0.01,
load_best_model_at_end=True,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
overwrite_output_dir=True,
bf16=True,
bf16_full_eval=True,
lr_scheduler_type='linear',
warmup_ratio=0.01,
seed=2024,
# fsdp='full_shard'
)
training_dataset=training_dataset.remove_columns(['Text-ID', 'Sentence-ID', 'Text'])
validation_dataset=validation_dataset.remove_columns(['Text-ID', 'Sentence-ID', 'Text'])
model = MultiHead_MultiLabel_XL.from_pretrained(pretrained_model, problem_type="multi_label_classification",num_labels=len(labels), id2label=id2label, label2id=label2id)
model.resize_token_embeddings(len(tokenizer))
trainer = transformers.Trainer(
model=model,
args=train_args,
train_dataset=training_dataset,
eval_dataset=validation_dataset,
compute_metrics=compute_metrics,
tokenizer=tokenizer,
#data_collator=data_collator,
callbacks=[transformers.EarlyStoppingCallback(5)],
)
train_result = trainer.train(resume_from_checkpoint=None)
metrics = train_result.metrics
metrics["train_samples"] = len(training_dataset)
trainer.save_model()
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
evaluation = trainer.evaluate(eval_dataset=validation_dataset)
max_eval_samples = len(validation_dataset)
evaluation["eval_samples"] = max_eval_samples
trainer.log_metrics("eval", evaluation)
trainer.save_metrics("eval", evaluation)