File size: 10,651 Bytes
4af2eee | 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import seaborn as sns
from torch.utils.data import Dataset
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from transformers import(
DebertaV2Tokenizer,
DebertaV2ForSequenceClassification,
TrainingArguments,
Trainer,
EarlyStoppingCallback
)
# Set seed for reproducibility
SEED = 42
torch.manual_seed(SEED)
np.random.seed(SEED)
class WeightedTrainer(Trainer):
"""Custom trainer that uses weighted loss function for imbaalance data."""
def __init__(self, *args, class_weights=None, **kwargs):
"""Initialize trainer with class weights.
Args:
class_weights: Tensor of weights for each class
*args, **kwargs: Arguments for parent Trainer class
"""
super().__init__(*args, **kwargs)
self.class_weights = class_weights.to(self.model.device)
self.loss_fn = nn.CrossEntropyLoss(
weight=self.class_weights,
label_smoothing=0.1
)
def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
"""Compute loss using the weighted loss function
args:
model: Model to train
inputs: Input to batch
return_outputs: whether to return output along with loss
Returns:
Loss value or tuple of (loss, outputs)
"""
labels = inputs.get("labels")
outputs = model(**inputs)
logits = outputs.get("logits")
loss = self.loss_fn(logits, labels)
return (loss, outputs) if return_outputs else loss
class EmailClassification(Dataset):
def __init__(self, texts, labels, tokenizer, max_length):
"""Intialize dataset with texts, labels and tokenizer settings.
Args:
texts: List of email Text Strings
labels: List of corresponding Labels
tokenizer: Pretrained tokenizer
max_length: Maximum sequence length
"""
self.texts = texts
self.labels = labels
self.tokenizer = tokenizer
self.max_length = max_length
def __len__(self):
"""Return the number of sample in the dataset."""
return len(self.texts)
def __getitem__(self, idx):
"""Get a single item from the dataset
Args:
idx: Index of the Item
Returns:
Dict containing input_ids, attention_mask and labels
"""
text = str(self.texts[idx])
label = self.labels[idx]
encoding = self.tokenizer(
text,
add_special_tokens=True,
max_length=self.max_length,
truncation=True,
return_attention_mask=True,
return_tensors='pt',
padding='max_length'
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
def compute_class_weights(labels):
"""Compute class weights inversly proportional to class frequencies.
Args:
labels: Array of labels
return:
Tensor of Class weights
"""
class_counts = np.bincount(labels)
total_sample = len(labels)
class_weights = total_sample / (len(class_counts) * class_counts)
return torch.tensor(class_weights, dtype=torch.float)
def compute_metrics(pred):
"""Compute metrics for evaluation.
Args:
pred: Prediction object from trainer
Returns:
Dict of metrics
"""
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
# Calculate metrics
report = classification_report(
labels,
preds,
target_names=list(label2id.keys()),
output_dict=True
)
# Extract Metrics
results = {
'accuracy': report['accuracy'],
'f1_macro': report['macro avg']['f1-score'],
'f1_weighted': report['weighted avg']['f1-score'],
'precision_weighted': report['weighted avg']['precision'],
'recall_weighted': report['weighted avg']['recall']
}
# Add class specific f1 scores
for cls_name, cls_id in label2id.items():
results[f'f1_{cls_name}'] = report[cls_name]['f1-score']
return results
def train(df, model_name, label2id, id2label, text_column='email_processed',
max_length=256, batch_size=16, epochs=5,seed=42):
"""Train and evaluate model using train/val/test split.
Args:
df: DataFrame containing the dataset
model_name: Name of the pretrained model
label2id: Dictionary mapping label names to IDs
id2label: Dictionary mapping IDs to label names
text_column: Column name containing processed email text
max_length: Maximum sequence length for tokenizer
batch_size: Batch size for training
epochs: Number of training epochs
seed: Random seed for reproducibility
Returns:
Tuple of (trained model, tokenizer)
"""
# Map labels to IDs
df['label_id'] = df['type'].map(label2id)
# Load tokenizer
tokenizer = DebertaV2Tokenizer.from_pretrained(model_name)
# Prepare Data
texts = df[text_column].values
labels = df['label_id'].values
# Compute class weights
class_weights = compute_class_weights(labels)
print("\nClass weights for handling imbalance: ")
for label, weight in zip(label2id.keys(), class_weights):
print(f"{label}: {weight.item():.4f}")
# First, split data into train and temp (val+test combined)
train_texts, temp_texts, train_labels, temp_labels = train_test_split(
texts,
labels,
test_size=0.3, # 70% train, 30% for val+test
stratify=labels, # Maintain class distribution
random_state=seed
)
# Further split temp into val and test
val_texts, test_texts, val_labels, test_labels = train_test_split(
temp_texts,
temp_labels,
test_size=0.5, # 15% val, 15% test (of the total dataset)
stratify=temp_labels, # Maintain class distribution
random_state=seed
)
# Create Datasets
train_dataset = EmailClassification(
train_texts, train_labels, tokenizer, max_length
)
val_dataset = EmailClassification(
val_texts, val_labels, tokenizer, max_length
)
test_dataset = EmailClassification(
test_texts, test_labels, tokenizer, max_length
)
# Load Model
model = DebertaV2ForSequenceClassification.from_pretrained(
model_name,
num_labels=len(label2id),
id2label=id2label,
label2id=label2id,
)
model.gradient_checkpointing_enable()
# Training arguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=epochs,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
learning_rate=5e-5, # Starting learning rate
lr_scheduler_type="cosine",
warmup_steps=120,
weight_decay=0.01,
logging_dir="./logs",
logging_steps=100,
eval_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
metric_for_best_model="f1_weighted",
greater_is_better=True,
save_total_limit=1,
seed=seed,
report_to=None,
fp16=True, # Enable mixed precision training
gradient_accumulation_steps=4,
)
# Define Trainer
trainer = WeightedTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
compute_metrics=compute_metrics,
callbacks=[EarlyStoppingCallback(early_stopping_patience=2)],
class_weights=class_weights,
)
# Train the model
trainer.train()
# Evaluate on validation set
print("\nEvaluating on validation set:")
val_results = trainer.evaluate()
for metric_name, value in val_results.items():
print(f"{metric_name}: {value:.4f}")
# Evaluate on test set
print("\nEvaluating on test set:")
test_results = trainer.predict(test_dataset)
test_metrics = compute_metrics(test_results)
for metric_name, value in test_metrics.items():
print(f"{metric_name}: {value:.4f}")
# Get predictions for test set
test_preds = np.argmax(test_results.predictions, axis=1)
# Print classification report for test set
print("\nTest Set Classification Report:")
print(classification_report(
test_labels,
test_preds,
target_names=list(label2id.keys())
))
# Plot confusion matrix for test set
plt.figure(figsize=(10, 8))
cm = confusion_matrix(test_labels, test_preds)
sns.heatmap(
cm,
annot=True,
fmt='d',
cmap='Blues',
xticklabels=list(label2id.keys()),
yticklabels=list(label2id.keys())
)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix - Test Set')
plt.tight_layout()
plt.savefig('confusion_matrix_test_set.png')
plt.close()
model.save_pretrained('./model')
tokenizer.save_pretrained("./model")
return model, tokenizer
if __name__ == "__main__":
"""Main function to run the complete pipeline."""
print("Email Classification Training Pipeline")
print(f"{'='*40}")
# Label Mapping
label2id = {
'Incident': 0,
'Request': 1,
'Problem': 2,
'Change': 3
}
id2label = {v: k for k, v in label2id.items()}
# Model configuration
MODEL_NAME = "microsoft/mdeberta-v3-base"
MAX_LENGTH = 128
BATCH_SIZE = 4
EPOCHS = 5
SEED = 42
# Load your dataset (example)
df = pd.read_csv("/content/processed_emails.csv")
# Train and evaluate
model, tokenizer = train(
df=df,
model_name=MODEL_NAME,
label2id=label2id,
id2label=id2label,
text_column='email_processed',
max_length=MAX_LENGTH,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
seed=SEED
)
|