-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclassifier.py
More file actions
1416 lines (1154 loc) · 53.3 KB
/
Copy pathclassifier.py
File metadata and controls
1416 lines (1154 loc) · 53.3 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from transformers import AutoModel, AutoTokenizer
from typing import List, Dict, Optional, Tuple, Any, Set, Union
import logging
import copy
from pathlib import Path
from safetensors.torch import save_file, load_file
import json
from sklearn.cluster import KMeans
from huggingface_hub import ModelHubMixin, hf_hub_download
import os
import shutil
from .models import Example, AdaptiveHead, ModelConfig
from .memory import PrototypeMemory
from .ewc import EWC
from .strategic import (
StrategicCostFunction, CostFunctionFactory, StrategicOptimizer, StrategicEvaluator
)
logger = logging.getLogger(__name__)
class AdaptiveClassifier(ModelHubMixin):
"""A flexible classifier that can adapt to new classes and examples."""
def __init__(
self,
model_name: str,
device: Optional[str] = None,
config: Optional[Dict[str, Any]] = None,
seed: int = 42 # Add seed parameter
):
"""Initialize the adaptive classifier.
Args:
model_name: Name of the HuggingFace transformer model
device: Device to run the model on (default: auto-detect)
config: Optional configuration dictionary
"""
# Set seed for initialization
torch.manual_seed(seed)
self.config = ModelConfig(config)
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
# Initialize transformer model and tokenizer
self.model = AutoModel.from_pretrained(model_name).to(self.device)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
# Initialize memory system
self.embedding_dim = self.model.config.hidden_size
self.memory = PrototypeMemory(
self.embedding_dim,
config=self.config
)
# Initialize adaptive head
self.adaptive_head = None
# Label mappings
self.label_to_id = {}
self.id_to_label = {}
# Statistics
self.train_steps = 0
self.training_history = {} # Track cumulative training examples per class
# Strategic classification components
self.strategic_cost_function = None
self.strategic_optimizer = None
self.strategic_evaluator = None
# Initialize strategic components if enabled
if self.config.enable_strategic_mode:
self._initialize_strategic_components()
def add_examples(self, texts: List[str], labels: List[str]):
"""Add new examples with special handling for new classes."""
if not texts or not labels:
raise ValueError("Empty input lists")
if len(texts) != len(labels):
raise ValueError("Mismatched text and label lists")
# Check if classifier has any existing classes (before updating mappings)
has_existing_classes = len(self.label_to_id) > 0
# Check for new classes
new_classes = set(labels) - set(self.label_to_id.keys())
is_adding_new_classes = len(new_classes) > 0
# Update label mappings - sort new classes alphabetically for consistent IDs
for label in sorted(new_classes):
idx = len(self.label_to_id)
self.label_to_id[label] = idx
self.id_to_label[idx] = label
# Get embeddings for all texts
embeddings = self._get_embeddings(texts)
# Add examples to memory and update training history
for text, embedding, label in zip(texts, embeddings, labels):
example = Example(text, label, embedding)
self.memory.add_example(example, label)
# Update training history
if label not in self.training_history:
self.training_history[label] = 0
self.training_history[label] += 1
# Determine training strategy: only use special new class handling for incremental learning
is_incremental_learning = is_adding_new_classes and has_existing_classes
if is_incremental_learning:
# Adding new classes to existing classifier - use special handling
# Store old head for EWC before modifying structure
old_head = copy.deepcopy(self.adaptive_head) if self.adaptive_head is not None else None
# Expand existing head to accommodate new classes (preserves weights)
num_classes = len(self.label_to_id)
self.adaptive_head.update_num_classes(num_classes)
# Move to correct device after update
self.adaptive_head = self.adaptive_head.to(self.device)
# Train with focus on new classes
self._train_new_classes(old_head, new_classes)
else:
# Initial training or regular updates - use normal training
# Initialize head if needed
if self.adaptive_head is None:
self._initialize_adaptive_head()
elif is_adding_new_classes:
# Edge case: expanding head for new classes but treating as regular training
num_classes = len(self.label_to_id)
self.adaptive_head.update_num_classes(num_classes)
self.adaptive_head = self.adaptive_head.to(self.device)
# Regular training
self._train_adaptive_head()
# Strategic training step if enabled
if self.strategic_mode and self.train_steps % self.config.strategic_training_frequency == 0:
self._perform_strategic_training()
# Ensure FAISS index is up to date after adding examples
self.memory._rebuild_index()
def _train_new_classes(self, old_head: Optional[nn.Module], new_classes: Set[str]):
"""Train the model with focus on new classes while preserving old class knowledge."""
if not self.memory.examples:
return
# Prepare training data with balanced sampling
all_embeddings = []
all_labels = []
examples_per_class = {}
# Count examples per class
for label in self.memory.examples:
examples_per_class[label] = len(self.memory.examples[label])
# Improved sampling strategy for many-class scenarios
min_examples = min(examples_per_class.values())
max_examples = max(examples_per_class.values())
# For many-class scenarios, use a more balanced approach
num_classes = len(examples_per_class)
target_samples_per_class = max(5, min(10, min_examples * 2)) # Adaptive target
if num_classes > 20: # Many-class scenario
# Use stratified sampling to ensure all classes get representation
for label, examples in self.memory.examples.items():
if label in new_classes:
# Give new classes more representation, but not excessive
num_samples = min(len(examples), target_samples_per_class * 2)
else:
# Ensure old classes maintain representation
num_samples = min(len(examples), target_samples_per_class)
# Sample without replacement first, then with if needed
if num_samples <= len(examples):
indices = np.random.choice(len(examples), size=num_samples, replace=False)
else:
indices = np.random.choice(len(examples), size=num_samples, replace=True)
for idx in indices:
example = examples[idx]
all_embeddings.append(example.embedding)
all_labels.append(self.label_to_id[label])
else:
# Original strategy for fewer classes
sampling_weights = {}
for label, count in examples_per_class.items():
if label in new_classes:
# Oversample new classes
sampling_weights[label] = 2.0
else:
# Sample old classes proportionally
sampling_weights[label] = min_examples / count
# Sample examples with weights
for label, examples in self.memory.examples.items():
weight = sampling_weights[label]
num_samples = max(min_examples, int(len(examples) * weight))
# Randomly sample with replacement if needed
indices = np.random.choice(
len(examples),
size=num_samples,
replace=num_samples > len(examples)
)
for idx in indices:
example = examples[idx]
all_embeddings.append(example.embedding)
all_labels.append(self.label_to_id[label])
all_embeddings = torch.stack(all_embeddings)
all_labels = torch.tensor(all_labels)
# Create dataset and initialize EWC with lower penalty for new classes
dataset = torch.utils.data.TensorDataset(all_embeddings, all_labels)
ewc = None
if old_head is not None:
# Create a dataset for EWC that only includes examples from old classes
old_embeddings = []
old_labels = []
old_label_to_id = {label: idx for idx, label in enumerate(self.id_to_label.values())
if label not in new_classes}
for label, examples in self.memory.examples.items():
if label not in new_classes: # Only old classes
for example in examples[:5]: # Limit to representative examples
old_embeddings.append(example.embedding)
old_labels.append(old_label_to_id[label])
if old_embeddings: # Only create EWC if we have old examples
old_embeddings = torch.stack(old_embeddings)
old_labels = torch.tensor(old_labels, dtype=torch.long)
old_dataset = torch.utils.data.TensorDataset(old_embeddings, old_labels)
ewc = EWC(
old_head,
old_dataset,
device=self.device,
ewc_lambda=5.0 # Balanced EWC penalty
)
# Training setup
self.adaptive_head.train()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.AdamW(
self.adaptive_head.parameters(),
lr=0.001,
weight_decay=0.01
)
# Create data loader
loader = torch.utils.data.DataLoader(
dataset,
batch_size=32,
shuffle=True,
generator=torch.Generator().manual_seed(42)
)
# Training loop
best_loss = float('inf')
patience = 3
patience_counter = 0
for epoch in range(15): # More epochs for new classes
total_loss = 0
for batch_embeddings, batch_labels in loader:
batch_embeddings = batch_embeddings.to(self.device)
batch_labels = batch_labels.to(self.device)
optimizer.zero_grad()
outputs = self.adaptive_head(batch_embeddings)
# Compute task loss
task_loss = criterion(outputs, batch_labels)
# Add EWC loss if applicable
if ewc is not None:
ewc_loss = ewc.ewc_loss(batch_size=len(batch_embeddings))
loss = task_loss + ewc_loss
else:
loss = task_loss
loss.backward()
torch.nn.utils.clip_grad_norm_(
self.adaptive_head.parameters(),
max_norm=1.0
)
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(loader)
# Early stopping check
if avg_loss < best_loss:
best_loss = avg_loss
patience_counter = 0
else:
patience_counter += 1
if patience_counter >= patience:
logger.debug(f"Early stopping at epoch {epoch + 1}")
break
self.train_steps += 1
def _perform_strategic_training(self):
"""Perform strategic training on current examples."""
if not self.strategic_mode or not self.memory.examples:
return
# Prepare training data
all_embeddings = []
all_labels = []
for label in self.memory.examples:
for example in self.memory.examples[label]:
all_embeddings.append(example.embedding)
all_labels.append(self.label_to_id[label])
if all_embeddings:
all_embeddings = torch.stack(all_embeddings)
all_labels = torch.tensor(all_labels, dtype=torch.long, device=self.device)
# Perform strategic training step
self._strategic_training_step(all_embeddings, all_labels)
logger.debug("Performed strategic training step")
def predict(self, text: str, k: int = 5) -> List[Tuple[str, float]]:
"""Predict with dual prediction system - blends strategic and regular predictions.
If no cost function is provided, uses existing prediction logic (zero changes).
If cost function is provided, blends strategic and regular predictions.
Args:
text: Input text to classify
k: Number of top predictions to return
Returns:
List of (label, confidence) tuples
"""
if not text:
raise ValueError("Empty input text")
# If strategic mode is not enabled, use regular prediction
if not self.strategic_mode:
return self._predict_regular(text, k)
# Dual prediction system: blend strategic and regular predictions
return self._predict_dual(text, k)
def _predict_regular(self, text: str, k: int = 5) -> List[Tuple[str, float]]:
"""Regular prediction logic (original implementation)."""
# Ensure deterministic behavior
with torch.no_grad():
# Get embedding
embedding = self._get_embeddings([text])[0]
# Get prototype predictions for ALL classes (not limited by k)
# This ensures complete scoring information for proper combination
max_classes = len(self.id_to_label) if self.id_to_label else k
proto_preds = self.memory.get_nearest_prototypes(embedding, k=max_classes)
# Get neural predictions if available for ALL classes (not limited by k)
if self.adaptive_head is not None:
self.adaptive_head.eval() # Ensure eval mode
# Add batch dimension and move to device
input_embedding = embedding.unsqueeze(0).to(self.device)
logits = self.adaptive_head(input_embedding)
# Squeeze batch dimension
logits = logits.squeeze(0)
probs = F.softmax(logits, dim=0)
# Get predictions for ALL classes for proper scoring combination
values, indices = torch.topk(probs, len(self.id_to_label))
head_preds = [
(self.id_to_label[idx.item()], val.item())
for val, idx in zip(values, indices)
]
else:
head_preds = []
# Combine predictions with adjusted weights
combined_scores = {}
# Use training history to determine weights
for label, score in proto_preds:
# Check training history instead of current storage
trained_examples = self.training_history.get(label, 0)
if trained_examples < 10:
# For newer classes (fewer training examples), trust neural predictions more
weight = 0.3 # Lower prototype weight for new classes
else:
weight = 0.7 # Higher prototype weight for established classes
combined_scores[label] = score * weight
for label, score in head_preds:
# Use training history for neural weights too
trained_examples = self.training_history.get(label, 0)
if trained_examples < 10:
weight = 0.7 # Higher neural weight for new classes
else:
weight = 0.3 # Lower neural weight for established classes
combined_scores[label] = combined_scores.get(label, 0) + score * weight
# Normalize scores
predictions = sorted(
combined_scores.items(),
key=lambda x: x[1],
reverse=True
)
total = sum(score for _, score in predictions)
if total > 0:
predictions = [(label, score/total) for label, score in predictions]
return predictions[:k]
def _predict_dual(self, text: str, k: int = 5) -> List[Tuple[str, float]]:
"""Dual prediction system that blends strategic and regular predictions."""
# Get regular predictions
regular_preds = self._predict_regular(text, k)
# Get strategic predictions
strategic_preds = self.predict_strategic(text, k)
# Blend predictions based on configuration
blended_scores = {}
# Weight for blending (configurable)
regular_weight = self.config.strategic_blend_regular_weight
strategic_weight = self.config.strategic_blend_strategic_weight
# Combine regular predictions
for label, score in regular_preds:
blended_scores[label] = score * regular_weight
# Combine strategic predictions
for label, score in strategic_preds:
blended_scores[label] = blended_scores.get(label, 0) + score * strategic_weight
# Sort and normalize
blended_predictions = sorted(
blended_scores.items(),
key=lambda x: x[1],
reverse=True
)
# Normalize scores
total = sum(score for _, score in blended_predictions)
if total > 0:
blended_predictions = [
(label, score / total) for label, score in blended_predictions
]
# Log dual prediction for debugging
logger.debug(f"Dual prediction - Regular: {regular_preds[:3]}, Strategic: {strategic_preds[:3]}, Blended: {blended_predictions[:3]}")
return blended_predictions[:k]
def _save_pretrained(
self,
save_directory: Union[str, Path],
config: Optional[Dict[str, Any]] = None,
**kwargs
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""Save the model to a directory.
Args:
save_directory: Directory to save the model to
config: Optional additional configuration
**kwargs: Additional arguments passed to save_pretrained
Returns:
Tuple of (dict of filenames, dict of objects to save)
"""
save_directory = Path(save_directory)
os.makedirs(save_directory, exist_ok=True)
# Save configuration and metadata
config_dict = {
'model_name': self.model.config._name_or_path,
'embedding_dim': self.embedding_dim,
'label_to_id': self.label_to_id,
'id_to_label': {str(k): v for k, v in self.id_to_label.items()},
'train_steps': self.train_steps,
'training_history': self.training_history, # Save cumulative training counts
'config': self.config.to_dict()
}
# Save examples in a separate file to keep config clean
saved_examples = {}
for label, examples in self.memory.examples.items():
saved_examples[label] = [
ex.to_dict() for ex in
self.select_representative_examples(
examples, k=self.config.num_representative_examples)
]
# Save model tensors
tensor_dict = {}
# Save prototypes
for label, proto in self.memory.prototypes.items():
tensor_dict[f'prototype_{label}'] = proto
# Save adaptive head if it exists
if self.adaptive_head is not None:
for name, param in self.adaptive_head.state_dict().items():
tensor_dict[f'adaptive_head_{name}'] = param
# Save files
config_file = save_directory / "config.json"
examples_file = save_directory / "examples.json"
tensors_file = save_directory / "model.safetensors"
with open(config_file, "w", encoding="utf-8") as f:
json.dump(config_dict, f, indent=2, sort_keys=True)
with open(examples_file, "w", encoding="utf-8") as f:
json.dump(saved_examples, f, indent=2, sort_keys=True)
save_file(tensor_dict, tensors_file)
# Generate model card if it doesn't exist
model_card_path = save_directory / "README.md"
if not model_card_path.exists():
model_card_content = self._generate_model_card()
with open(model_card_path, "w", encoding="utf-8") as f:
f.write(model_card_content)
# Return files that were created
saved_files = {
"config": config_file.name,
"examples": examples_file.name,
"model": tensors_file.name,
"model_card": model_card_path.name,
}
return saved_files, {}
@classmethod
def _from_pretrained(
cls,
model_id: str,
revision: Optional[str] = None,
cache_dir: Optional[str] = None,
force_download: bool = False,
proxies: Optional[Dict] = None,
resume_download: bool = False,
local_files_only: bool = False,
token: Optional[Union[str, bool]] = None,
**kwargs
) -> "AdaptiveClassifier":
"""Load a model from the HuggingFace Hub or local directory.
Args:
model_id: HuggingFace Hub model ID or path to local directory
revision: Revision of the model on the Hub
cache_dir: Cache directory for downloaded models
force_download: Force download of models
proxies: Proxies to use for downloading
resume_download: Resume downloading if interrupted
local_files_only: Use local files only, don't download
token: Authentication token for Hub
**kwargs: Additional arguments passed to from_pretrained
Returns:
Loaded AdaptiveClassifier instance
"""
# Check if model_id is a local directory
model_path = Path(model_id)
try:
if model_path.is_dir() and (model_path / "config.json").exists():
# Local directory with required files
pass
else:
# Download files from HuggingFace Hub
config_file = hf_hub_download(
repo_id=model_id,
filename="config.json",
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
proxies=proxies,
resume_download=resume_download,
token=token,
local_files_only=local_files_only,
)
model_path = Path(os.path.dirname(config_file))
# Download examples file
hf_hub_download(
repo_id=model_id,
filename="examples.json",
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
proxies=proxies,
resume_download=resume_download,
token=token,
local_files_only=local_files_only,
)
# Download model file
hf_hub_download(
repo_id=model_id,
filename="model.safetensors",
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
proxies=proxies,
resume_download=resume_download,
token=token,
local_files_only=local_files_only,
)
except Exception as e:
raise ValueError(f"Error loading model from {model_id}: {e}")
# Load configuration
with open(model_path / "config.json", "r", encoding="utf-8") as f:
config_dict = json.load(f)
# Load examples
with open(model_path / "examples.json", "r", encoding="utf-8") as f:
saved_examples = json.load(f)
# Initialize classifier
device = kwargs.get("device", None)
classifier = cls(
config_dict['model_name'],
device=device,
config=config_dict.get('config', None)
)
# Restore label mappings
classifier.label_to_id = config_dict['label_to_id']
classifier.id_to_label = {
int(k): v for k, v in config_dict['id_to_label'].items()
}
classifier.train_steps = config_dict['train_steps']
# Restore training history with backward compatibility
classifier.training_history = config_dict.get('training_history', {})
# Load tensors
tensors = load_file(model_path / "model.safetensors")
# Restore saved examples
for label, examples_data in saved_examples.items():
classifier.memory.examples[label] = [
Example.from_dict(ex_data) for ex_data in examples_data
]
# Restore prototypes
for label in classifier.label_to_id.keys():
prototype_key = f'prototype_{label}'
if prototype_key in tensors:
prototype = tensors[prototype_key]
classifier.memory.prototypes[label] = prototype
# Rebuild memory system
classifier.memory._restore_from_save()
# Restore adaptive head if it exists
adaptive_head_params = {
k.replace('adaptive_head_', ''): v
for k, v in tensors.items()
if k.startswith('adaptive_head_')
}
if adaptive_head_params:
classifier._initialize_adaptive_head()
classifier.adaptive_head.load_state_dict(adaptive_head_params)
# Backward compatibility: estimate training history if not present
if not classifier.training_history:
for label, examples in saved_examples.items():
# Estimate based on saved examples (default saves 5, typical training uses 100+)
# Using 20x multiplier as reasonable estimate
classifier.training_history[label] = len(examples) * 20
return classifier
def _generate_model_card(self) -> str:
"""Generate a model card for the classifier.
Returns:
Model card content as string
"""
stats = self.get_memory_stats()
model_card = f"""---
language: multilingual
tags:
- adaptive-classifier
- text-classification
- continuous-learning
license: apache-2.0
---
# Adaptive Classifier
This model is an instance of an [adaptive-classifier](https://github.com/codelion/adaptive-classifier) that allows for continuous learning and dynamic class addition.
You can install it with `pip install adaptive-classifier`.
## Model Details
- Base Model: {self.model.config._name_or_path}
- Number of Classes: {stats['num_classes']}
- Total Examples: {stats['total_examples']}
- Embedding Dimension: {self.embedding_dim}
## Class Distribution
```
{self._format_class_distribution(stats)}
```
## Usage
```python
from adaptive_classifier import AdaptiveClassifier
# Load the model
classifier = AdaptiveClassifier.from_pretrained("adaptive-classifier/model-name")
# Make predictions
text = "Your text here"
predictions = classifier.predict(text)
print(predictions) # List of (label, confidence) tuples
# Add new examples
texts = ["Example 1", "Example 2"]
labels = ["class1", "class2"]
classifier.add_examples(texts, labels)
```
## Training Details
- Training Steps: {self.train_steps}
- Examples per Class: See distribution above
- Prototype Memory: Active
- Neural Adaptation: {"Active" if self.adaptive_head is not None else "Inactive"}
## Limitations
This model:
- Requires at least {self.config.min_examples_per_class} examples per class
- Has a maximum of {self.config.max_examples_per_class} examples per class
- Updates prototypes every {self.config.prototype_update_frequency} examples
## Citation
```bibtex
@software{{adaptive_classifier,
title = {{Adaptive Classifier: Dynamic Text Classification with Continuous Learning}},
author = {{Sharma, Asankhaya}},
year = {{2025}},
publisher = {{GitHub}},
url = {{https://github.com/codelion/adaptive-classifier}}
}}
```
"""
return model_card
def _format_class_distribution(self, stats: Dict[str, Any]) -> str:
"""Format class distribution for model card.
Args:
stats: Statistics from get_memory_stats()
Returns:
Formatted string of class distribution
"""
if 'examples_per_class' not in stats:
return "No examples stored"
lines = []
total = sum(stats['examples_per_class'].values())
for label, count in sorted(stats['examples_per_class'].items()):
percentage = (count / total) * 100 if total > 0 else 0
lines.append(f"{label}: {count} examples ({percentage:.1f}%)")
return "\n".join(lines)
# Keep existing save/load methods for backwards compatibility
def save(self, save_dir: str):
"""Legacy save method for backwards compatibility."""
return self._save_pretrained(save_dir)
@classmethod
def load(cls, save_dir: str, device: Optional[str] = None) -> 'AdaptiveClassifier':
"""Legacy load method for backwards compatibility."""
kwargs = {}
if device is not None:
kwargs['device'] = device
return cls._from_pretrained(save_dir, **kwargs)
def to(self, device: str) -> 'AdaptiveClassifier':
"""Move the model to specified device.
Args:
device: Device to move to ("cuda" or "cpu")
Returns:
Self for chaining
"""
self.device = device
self.model = self.model.to(device)
if self.adaptive_head is not None:
self.adaptive_head = self.adaptive_head.to(device)
return self
def get_memory_stats(self) -> Dict[str, Any]:
"""Get memory statistics.
Returns:
Dictionary of memory statistics
"""
return self.memory.get_stats()
def _initialize_adaptive_head(self):
"""Initialize or reinitialize the adaptive head with improved configuration."""
num_classes = len(self.label_to_id)
hidden_dims = [self.embedding_dim, self.embedding_dim // 2]
self.adaptive_head = AdaptiveHead(
self.embedding_dim,
num_classes,
hidden_dims=hidden_dims
).to(self.device)
def _get_embeddings(self, texts: List[str]) -> List[torch.Tensor]:
"""Get embeddings for input texts."""
# Temporarily set model to eval mode
was_training = self.model.training
self.model.eval()
# Get embeddings
with torch.no_grad():
inputs = self.tokenizer(
texts,
max_length=self.config.max_length,
truncation=True,
padding=True,
return_tensors="pt"
).to(self.device)
outputs = self.model(**inputs)
embeddings = outputs.last_hidden_state[:, 0, :]
# Normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
# Restore original training mode
if was_training:
self.model.train()
# Return embeddings as list
return [emb.cpu() for emb in embeddings]
def get_example_statistics(self) -> Dict[str, Any]:
"""Get statistics about stored examples and model state."""
stats = {
'total_examples': sum(len(exs) for exs in self.memory.examples.values()),
'examples_per_class': {
label: len(exs) for label, exs in self.memory.examples.items()
},
'num_classes': len(self.label_to_id),
'train_steps': self.train_steps,
'memory_usage': {
'prototypes': sum(p.nelement() * p.element_size()
for p in self.memory.prototypes.values()),
'examples': sum(sum(ex.embedding.nelement() * ex.embedding.element_size()
for ex in exs)
for exs in self.memory.examples.values())
}
}
if self.adaptive_head is not None:
stats['model_params'] = sum(p.nelement() for p in
self.adaptive_head.parameters())
return stats
def predict_batch(
self,
texts: List[str],
k: int = 5,
batch_size: int = 32
) -> List[List[Tuple[str, float]]]:
"""Predict labels for a batch of texts with improved batching."""
if not texts:
raise ValueError("Empty input batch")
all_predictions = []
# Process in batches
for i in range(0, len(texts), batch_size):
batch_texts = texts[i:i + batch_size]
# Get embeddings for batch
batch_embeddings = self._get_embeddings(batch_texts)
# Get predictions for each embedding
batch_predictions = []
for embedding in batch_embeddings:
# Get prototype predictions
proto_preds = self.memory.get_nearest_prototypes(
embedding,
k=k
)
# Get neural predictions if available
if self.adaptive_head is not None:
self.adaptive_head.eval()
with torch.no_grad():
# Add batch dimension and move to device
input_embedding = embedding.unsqueeze(0).to(self.device)
logits = self.adaptive_head(input_embedding)
# Squeeze batch dimension
logits = logits.squeeze(0)
probs = F.softmax(logits, dim=0)
values, indices = torch.topk(
probs,
min(k, len(self.id_to_label))
)
head_preds = [
(self.id_to_label[idx.item()], val.item())
for val, idx in zip(values, indices)
]
else:
head_preds = []
# Combine predictions
combined_scores = {}
proto_weight = 0.7 # More weight to prototypes
head_weight = 0.3 # Less weight to neural network
for label, score in proto_preds:
combined_scores[label] = score * proto_weight
for label, score in head_preds:
combined_scores[label] = (
combined_scores.get(label, 0) + score * head_weight
)
# Sort and normalize predictions
predictions = sorted(
combined_scores.items(),
key=lambda x: x[1],
reverse=True
)
# Normalize scores
total = sum(score for _, score in predictions)
if total > 0:
predictions = [(label, score/total)
for label, score in predictions]
batch_predictions.append(predictions[:k])
all_predictions.extend(batch_predictions)
return all_predictions
def clear_memory(self, labels: Optional[List[str]] = None):
"""Clear memory for specified labels or all if none specified."""
if labels is None:
self.memory.clear()
else:
for label in labels:
if label in self.memory.examples:
del self.memory.examples[label]
if label in self.memory.prototypes:
del self.memory.prototypes[label]
self.memory._rebuild_index()
def merge_classifiers(self, other: 'AdaptiveClassifier') -> 'AdaptiveClassifier':
"""Merge another classifier into this one."""
# Verify compatibility
if self.embedding_dim != other.embedding_dim:
raise ValueError("Classifiers have different embedding dimensions")