-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelling.py
94 lines (71 loc) · 2.68 KB
/
modelling.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
# imports
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
from transformers import Trainer, TrainingArguments
training_texts=[]
training_labels=[]
for i in range(0,45000):
training_texts.append(indic_df.iloc[i]['text'])
training_labels.append(indic_df.iloc[i]['sentiment'])
train_texts, val_texts, train_labels, val_labels = train_test_split(training_texts, training_labels, test_size=0.2, shuffle=True)
# train_texts=train_texts[0:30000]
# val_texts=val_texts[0:4000]
# train_labels=train_labels[0:30000]
# val_labels=val_labels[0:4000]
class TweetDataset(Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key,val in self.encodings.items()}
item["labels"] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
#model_name = "distilbert-base-uncased-finetuned-sst-2-english"
train_encodings = tokenizer(train_texts, truncation=True, padding=True)
val_encodings = tokenizer(val_texts, truncation=True, padding=True)
# test_encodings = tokenizer(test_texts, truncation=True, padding=True)
train_dataset = TweetDataset(train_encodings, train_labels)
val_dataset = TweetDataset(val_encodings, val_labels)
# test_dataset = TweetDataset(test_encodings, test_labels)
## define metric
import numpy as np
from datasets import load_metric
metric = load_metric("accuracy")
def compute_metrics(eval_pred):
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
return metric.compute(predictions=predictions, references=labels)
training_args = TrainingArguments(
output_dir='/content/drive/MyDrive/DLNLP_Project/finetuned_models/mbart/results',
num_train_epochs=4,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
warmup_steps=500,
learning_rate=5e-5,
weight_decay=0.01,
logging_dir='./logs',
logging_steps=10,
evaluation_strategy="steps",
save_strategy="steps",
save_steps=2000,
eval_steps=2000,
save_total_limit=2,
load_best_model_at_end=True,
)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
trainer = Trainer(
model = model,
args = training_args,
train_dataset = train_dataset,
eval_dataset = val_dataset,
compute_metrics=compute_metrics
)
# torch.cuda.empty_cache()
print(trainer.train())
# save model
save_directory = "models/mbart"