forked from InseeFrLab/torchTextClassifiers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusing_additional_features.py
More file actions
284 lines (231 loc) Β· 9.64 KB
/
using_additional_features.py
File metadata and controls
284 lines (231 loc) Β· 9.64 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
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
"""
Categorical Features Comparison Example
This example demonstrates the performance difference between:
1. A classifier using only text features
2. A classifier using both text and categorical features
"""
import os
import random
import time
import warnings
import numpy as np
import pandas as pd
import torch
from pytorch_lightning import seed_everything
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from torchTextClassifiers import ModelConfig, TrainingConfig, torchTextClassifiers
from torchTextClassifiers.tokenizers import WordPieceTokenizer
# Note: SimpleTextWrapper is not available in the current version
# from torchTextClassifiers.classifiers.simple_text_classifier import SimpleTextConfig, SimpleTextWrapper
def stratified_split_rare_labels(X, y, test_size=0.2, min_train_samples=1):
# Get unique labels and their frequencies
unique_labels, label_counts = np.unique(y, return_counts=True)
# Separate rare and common labels
rare_labels = unique_labels[label_counts == 1]
# Create initial mask for rare labels to go into training set
rare_label_mask = np.isin(y, rare_labels)
# Separate data into rare and common label datasets
X_rare = X[rare_label_mask]
y_rare = y[rare_label_mask]
X_common = X[~rare_label_mask]
y_common = y[~rare_label_mask]
# Split common labels stratified
X_common_train, X_common_test, y_common_train, y_common_test = train_test_split(
X_common, y_common, test_size=test_size, stratify=y_common
)
# Combine rare labels with common labels split
X_train = np.concatenate([X_rare, X_common_train])
y_train = np.concatenate([y_rare, y_common_train])
X_test = X_common_test
y_test = y_common_test
return X_train, X_test, y_train, y_test
def merge_cat(cat):
if cat in ['World', 'Top News', 'Europe', 'Italia', 'U.S.', 'Top Stories']:
return 'World News'
if cat in ['Sci/Tech', 'Software and Developement', 'Toons', 'Health', 'Music Feeds']:
return 'Tech and Stuff'
return cat
def load_and_prepare_data():
"""Load and prepare data"""
print("π Using AG news dataset sample for demonstration...")
df = pd.read_parquet("https://minio.lab.sspcloud.fr/h4njlg/public/ag_news_full_1M.parquet")
df = df.sample(10000, random_state=42) # Smaller sample to avoid disk space issues
print(f"β
Loaded {len(df)} samples from AG NEWS dataset")
df['category_final'] = df['category'].apply(lambda x: merge_cat(x))
df['title_headline'] = df['title'] + '\n####\n' + df['description']
# categorical_features = None
# text_feature = "title_headline"
# y = "category"
# textual_features = ["source", "url"]
source_encoder = LabelEncoder()
df["title_headline_processed"] = df["title_headline"]
df["source_encoded"] = source_encoder.fit_transform(df['source'])
X_text_only = df[['title_headline_processed']].values
X_mixed = df[['title_headline_processed', "source_encoded"]].values
y = df['category_final'].values
encoder = LabelEncoder()
y = encoder.fit_transform(y)
return X_text_only, X_mixed, y, encoder
def train_and_evaluate_model(X, y, model_name, use_categorical=False, use_simple=False):
"""Train and evaluate a FastText model"""
print(f"\nπ― Training {model_name}...")
# Split data twice: first for train/temp, then temp into validation/test
X_train, X_temp, y_train, y_temp = stratified_split_rare_labels(
X, y, test_size=0.1 # 40% for validation + test
)
X_val, X_test, y_val, y_test = stratified_split_rare_labels(
X_temp, y_temp, test_size=0.5 # Split temp 50/50 into validation and test
)
# Note: SimpleTextWrapper is not available in the current version
# The use_simple branch has been disabled
if use_simple:
raise NotImplementedError(
"SimpleTextWrapper is not available in the current version. "
"Please use the standard torchTextClassifiers with WordPieceTokenizer instead."
)
# Create and train tokenizer
print(" ποΈ Creating and training tokenizer...")
tokenizer = WordPieceTokenizer(vocab_size=5000, output_dim=128)
# Extract text column for tokenizer training
if use_categorical:
text_data = X_train[:, 0].tolist() # First column is text
else:
text_data = X_train.tolist() # All data is text
tokenizer.train(text_data)
print(" β
Tokenizer trained successfully!")
# Model configuration
if use_categorical:
# For mixed model - get vocabulary sizes from data
cat_data = X_train[:, 1:].astype(int) # Categorical features
vocab_sizes = [int(np.max(cat_data[:, i]) + 1) for i in range(cat_data.shape[1])]
model_config = ModelConfig(
embedding_dim=50,
categorical_vocabulary_sizes=vocab_sizes,
categorical_embedding_dims=10,
num_classes=5
)
print(f" Categorical vocabulary sizes: {vocab_sizes}")
else:
# For text-only model
model_config = ModelConfig(
embedding_dim=50,
num_classes=5
)
# Create classifier
print(" π¨ Creating classifier...")
classifier = torchTextClassifiers(
tokenizer=tokenizer,
model_config=model_config
)
print(" β
Classifier created successfully!")
# Training configuration
training_config = TrainingConfig(
num_epochs=50,
batch_size=128,
lr=0.001,
patience_early_stopping=3,
num_workers=0,
trainer_params={
'enable_progress_bar': True,
'deterministic': True
}
)
# Create and build model
start_time = time.time()
# Train model
print(" π― Training model...")
classifier.train(
X_train, y_train,
training_config=training_config,
X_val=X_val, y_val=y_val,
verbose=True
)
training_time = time.time() - start_time
# Handle predictions based on model type
if use_categorical:
print(" β
Running validation for text-with-categorical-variables model...")
try:
result = classifier.predict(X_test)
predictions = result["prediction"].squeeze().numpy()
test_accuracy = (predictions == y_test).mean()
print(f" Test accuracy: {test_accuracy:.3f}")
except Exception as e:
print(f" β οΈ Validation failed: {e}")
test_accuracy = 0.0
predictions = np.zeros(len(y_test))
else:
# Text-only model works fine for predictions
print(" β
Running validation for text-only model...")
try:
result = classifier.predict(X_test)
predictions = result["prediction"].squeeze().numpy()
test_accuracy = (predictions == y_test).mean()
print(f" Test accuracy: {test_accuracy:.3f}")
except Exception as e:
print(f" β οΈ Validation failed: {e}")
test_accuracy = 0.0
predictions = np.zeros(len(y_test))
return {
'model_name': model_name,
'test_accuracy': test_accuracy,
'training_time': training_time,
'predictions': predictions,
'y_test': y_test,
'classifier': classifier
}
def main():
# Set seed for reproducibility
SEED = 42
# Set environment variables for full reproducibility
os.environ['PYTHONHASHSEED'] = str(SEED)
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
# Use PyTorch Lightning's seed_everything for comprehensive seeding
seed_everything(SEED, workers=True)
# Make PyTorch operations deterministic
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True, warn_only=True)
# Suppress PyTorch Lightning warnings for cleaner output
warnings.filterwarnings(
'ignore',
message='.*',
category=UserWarning,
module='pytorch_lightning'
)
print("π Classifier: Categorical Features Comparison")
print("=" * 60)
print("Comparing performance with and without categorical features")
print()
# Load and prepare data (same as notebook)
X_text_only, X_mixed, y, encoder = load_and_prepare_data()
# Train models
print(f"\nπ Training Models:")
print("-" * 40)
# Text-only model
results_text_only = train_and_evaluate_model(
X_text_only, y, "Text-Only Classifier", use_categorical=False
)
# Mixed model (text + categorical)
results_mixed = train_and_evaluate_model(
X_mixed, y, "Mixed Features Classifier", use_categorical=True
)
# Note: TF-IDF classifier (SimpleTextWrapper) is not available in the current version
# results_tfidf = train_and_evaluate_model(X_text_only, y, "TF-IDF classifier", use_categorical=False, use_simple=True)
# Compare results
print(f"\nπ Results Comparison:")
print("=" * 50)
print(f"{'Model':<25}{'Test Acc':<11} {'Time (s)':<10}")
print("-" * 50)
print(f"{'Text-Only':<25} "
f"{results_text_only['test_accuracy']:<11.3f} {results_text_only['training_time']:<10.1f}")
print(f"{'Mixed Features':<25} "
f"{results_mixed['test_accuracy']:<11.3f} {results_mixed['training_time']:<10.1f}")
# Calculate improvements
acc_improvement = results_mixed['test_accuracy'] - results_text_only['test_accuracy']
time_overhead = results_mixed['training_time'] - results_text_only['training_time']
print("-" * 50)
print(f"Test Accuracy Improvement: {acc_improvement:+.3f}")
print(f"Training Time Overhead: {time_overhead:+.1f}s")
if __name__ == "__main__":
main()