-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_sampling.py
140 lines (120 loc) · 5.52 KB
/
random_sampling.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
import os
import random
import numpy as np
import pandas as pd
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from datasets import Dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
from transformers import DataCollatorWithPadding
from sklearn.model_selection import train_test_split
#----------------------------------------------------------
# Configuration
#----------------------------------------------------------
TRAIN_CSV = "datasets/financial_news_train.csv"
TEST_CSV = "datasets/financial_news_test.csv"
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english"
OUTPUT_DIR = "fine_tuned_models_random_sampling_financial_news"
SAMPLE_SIZES = [100, 300, 500]
BATCH_SIZE = 8
EPOCHS = 2 # Could be adjusted
os.makedirs(OUTPUT_DIR, exist_ok=True)
#----------------------------------------------------------
# Load Data
#----------------------------------------------------------
train_df = pd.read_csv(TRAIN_CSV)
test_df = pd.read_csv(TEST_CSV)
# Convert sentiment to numeric: assume "positive" -> 1, "negative" -> 0
train_df['label'] = train_df['sentiment'].map({'positive': 1, 'negative': 0})
test_df['label'] = test_df['sentiment'].map({'positive': 1, 'negative': 0})
#----------------------------------------------------------
# Prepare Tokenizer
#----------------------------------------------------------
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
def tokenize_function(examples):
return tokenizer(examples["review"], truncation=True, padding=False)
#----------------------------------------------------------
# Fine-tuning function
#----------------------------------------------------------
def fine_tune_and_evaluate(sample_size):
#------------------------------------------------------
# Sample Data
#------------------------------------------------------
sampled_train = train_df.sample(sample_size, random_state=999)
# Create HuggingFace Datasets
train_dataset = Dataset.from_pandas(sampled_train[['review', 'label']])
test_dataset = Dataset.from_pandas(test_df[['review', 'label']])
tokenized_train = train_dataset.map(tokenize_function, batched=True)
tokenized_test = test_dataset.map(tokenize_function, batched=True)
# Set format for pytorch
tokenized_train = tokenized_train.remove_columns(['review'])
tokenized_train = tokenized_train.with_format("torch")
tokenized_test = tokenized_test.remove_columns(['review'])
tokenized_test = tokenized_test.with_format("torch")
#------------------------------------------------------
# Load Base Model
#------------------------------------------------------
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2)
#------------------------------------------------------
# Training Arguments
#------------------------------------------------------
model_output_dir = os.path.join(OUTPUT_DIR, f"model_{sample_size}")
training_args = TrainingArguments(
output_dir=model_output_dir,
num_train_epochs=EPOCHS,
per_device_train_batch_size=BATCH_SIZE,
per_device_eval_batch_size=BATCH_SIZE,
evaluation_strategy="epoch",
save_strategy="epoch",
logging_dir=os.path.join(model_output_dir, "logs"),
logging_steps=10,
load_best_model_at_end=True,
)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
#------------------------------------------------------
# Trainer
#------------------------------------------------------
def compute_metrics(eval_pred):
logits, labels = eval_pred
preds = np.argmax(logits, axis=-1)
return {}
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_train,
eval_dataset=tokenized_test.select(range(100)), # small eval subset during training for speed
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics
)
#------------------------------------------------------
# Train Model
#------------------------------------------------------
trainer.train()
#------------------------------------------------------
# Evaluation
#------------------------------------------------------
# Run predictions on the full test set
test_preds = trainer.predict(tokenized_test)
preds = np.argmax(test_preds.predictions, axis=-1)
labels = test_preds.label_ids
# Classification Report
report = classification_report(labels, preds, target_names=["negative", "positive"], digits=4)
print(f"Sample Size: {sample_size}")
print(report)
# Confusion Matrix
cm = confusion_matrix(labels, preds)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=["negative", "positive"], yticklabels=["negative", "positive"])
plt.title(f"Confusion Matrix (Sample Size: {sample_size})")
plt.xlabel('Predicted')
plt.ylabel('True')
cm_path = os.path.join(model_output_dir, f"confusion_matrix_{sample_size}.png")
plt.savefig(cm_path)
plt.close()
#----------------------------------------------------------
# Run everything for each sample size
#----------------------------------------------------------
for size in SAMPLE_SIZES:
fine_tune_and_evaluate(size)