added app file and enable GPU training
Browse files- train_abuse_model.py +9 -5
train_abuse_model.py
CHANGED
@@ -14,6 +14,7 @@ from torch.utils.data import Dataset
|
|
14 |
# Hugging Face transformers
|
15 |
from transformers import (
|
16 |
AutoTokenizer,
|
|
|
17 |
BertTokenizer,
|
18 |
BertForSequenceClassification,
|
19 |
AutoModelForSequenceClassification,
|
@@ -21,6 +22,9 @@ from transformers import (
|
|
21 |
TrainingArguments
|
22 |
)
|
23 |
|
|
|
|
|
|
|
24 |
# Custom Dataset class
|
25 |
class AbuseDataset(Dataset):
|
26 |
def __init__(self, texts, labels):
|
@@ -31,8 +35,8 @@ class AbuseDataset(Dataset):
|
|
31 |
return len(self.labels)
|
32 |
|
33 |
def __getitem__(self, idx):
|
34 |
-
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
35 |
-
item["labels"] = torch.tensor(self.labels[idx], dtype=torch.float)
|
36 |
return item
|
37 |
|
38 |
|
@@ -151,13 +155,13 @@ label_matrix = df["label_vector"].tolist()
|
|
151 |
#model_name = "onlplab/alephbert-base"
|
152 |
model_name = "microsoft/deberta-v3-base"
|
153 |
|
154 |
-
# Load pretrained
|
155 |
-
tokenizer =
|
156 |
model = AutoModelForSequenceClassification.from_pretrained(
|
157 |
model_name,
|
158 |
num_labels=len(label_columns),
|
159 |
problem_type="multi_label_classification"
|
160 |
-
)
|
161 |
|
162 |
# # Optional: Freeze base model layers (only train classifier head)
|
163 |
# freeze_base = False
|
|
|
14 |
# Hugging Face transformers
|
15 |
from transformers import (
|
16 |
AutoTokenizer,
|
17 |
+
DebertaV2Tokenizer,
|
18 |
BertTokenizer,
|
19 |
BertForSequenceClassification,
|
20 |
AutoModelForSequenceClassification,
|
|
|
22 |
TrainingArguments
|
23 |
)
|
24 |
|
25 |
+
# Check for GPU availability
|
26 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
+
|
28 |
# Custom Dataset class
|
29 |
class AbuseDataset(Dataset):
|
30 |
def __init__(self, texts, labels):
|
|
|
35 |
return len(self.labels)
|
36 |
|
37 |
def __getitem__(self, idx):
|
38 |
+
item = {key: torch.tensor(val[idx]).to(device) for key, val in self.encodings.items()}
|
39 |
+
item["labels"] = torch.tensor(self.labels[idx], dtype=torch.float).to(device)
|
40 |
return item
|
41 |
|
42 |
|
|
|
155 |
#model_name = "onlplab/alephbert-base"
|
156 |
model_name = "microsoft/deberta-v3-base"
|
157 |
|
158 |
+
# Load pretrained model for fine-tuning
|
159 |
+
tokenizer = DebertaV2Tokenizer.from_pretrained(model_name)
|
160 |
model = AutoModelForSequenceClassification.from_pretrained(
|
161 |
model_name,
|
162 |
num_labels=len(label_columns),
|
163 |
problem_type="multi_label_classification"
|
164 |
+
).to(device) # Move model to GPU
|
165 |
|
166 |
# # Optional: Freeze base model layers (only train classifier head)
|
167 |
# freeze_base = False
|