-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
66 lines (52 loc) · 2.14 KB
/
train.py
File metadata and controls
66 lines (52 loc) · 2.14 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
import torch
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification, Trainer, TrainingArguments
from datasets import Dataset
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
import pandas as pd
df = pd.read_csv("final_dataset.csv", encoding='latin1')
df = df[['Email', 'Label']].dropna()
label_map = {'NORMAL': 0, 'SPAM': 1, 'FRAUD': 2}
df['label'] = df['Label'].map(label_map)
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42, stratify=df['label'])
train_ds = Dataset.from_pandas(train_df[['Email', 'label']])
test_ds = Dataset.from_pandas(test_df[['Email', 'label']])
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
def tokenize(batch):
return tokenizer(batch['Email'], truncation=True, padding='max_length', max_length=256)
train_ds = train_ds.map(tokenize, batched=True, batch_size=1024)
test_ds = test_ds.map(tokenize, batched=True, batch_size=1024)
train_ds.set_format('torch', columns=['input_ids', 'attention_mask', 'label'])
test_ds.set_format('torch', columns=['input_ids', 'attention_mask', 'label'])
model = DistilBertForSequenceClassification.from_pretrained(
'distilbert-base-uncased', num_labels=3
)
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted')
acc = accuracy_score(labels, preds)
return {'accuracy': acc, 'f1': f1, 'precision': precision, 'recall': recall}
training_args = TrainingArguments(
output_dir='./results',
do_eval=True,
eval_steps=500,
learning_rate=2e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=3,
weight_decay=0.01,
logging_dir='./logs'
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_ds,
eval_dataset=test_ds,
tokenizer=tokenizer,
compute_metrics=compute_metrics
)
trainer.train()
results = trainer.evaluate()
model.save_pretrained("./email_model")
tokenizer.save_pretrained("./email_model")