|
| 1 | +"""TransformerScorer class for transformer-based classification.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import numpy.typing as npt |
| 8 | +import torch |
| 9 | +from datasets import Dataset |
| 10 | +from transformers import ( |
| 11 | + AutoModelForSequenceClassification, |
| 12 | + AutoTokenizer, |
| 13 | + DataCollatorWithPadding, |
| 14 | + Trainer, |
| 15 | + TrainingArguments, |
| 16 | +) |
| 17 | + |
| 18 | +from autointent import Context |
| 19 | +from autointent.configs import EmbedderConfig |
| 20 | +from autointent.custom_types import ListOfLabels |
| 21 | +from autointent.modules.base import BaseScorer |
| 22 | + |
| 23 | + |
| 24 | +class TransformerScorer(BaseScorer): |
| 25 | + name = "transformer" |
| 26 | + supports_multiclass = True |
| 27 | + supports_multilabel = True |
| 28 | + _multilabel: bool |
| 29 | + _model: Any |
| 30 | + _tokenizer: Any |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + model_config: EmbedderConfig | str | dict[str, Any] | None = None, |
| 35 | + num_train_epochs: int = 3, |
| 36 | + batch_size: int = 8, |
| 37 | + learning_rate: float = 5e-5, |
| 38 | + seed: int = 0, |
| 39 | + ) -> None: |
| 40 | + self.model_config = EmbedderConfig.from_search_config(model_config) |
| 41 | + self.num_train_epochs = num_train_epochs |
| 42 | + self.batch_size = batch_size |
| 43 | + self.learning_rate = learning_rate |
| 44 | + self.seed = seed |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def from_context( |
| 48 | + cls, |
| 49 | + context: Context, |
| 50 | + model_config: EmbedderConfig | str | None = None, |
| 51 | + ) -> "TransformerScorer": |
| 52 | + if model_config is None: |
| 53 | + model_config = context.resolve_embedder() |
| 54 | + return cls(model_config=model_config) |
| 55 | + |
| 56 | + def get_embedder_config(self) -> dict[str, Any]: |
| 57 | + return self.model_config.model_dump() |
| 58 | + |
| 59 | + def fit( |
| 60 | + self, |
| 61 | + utterances: list[str], |
| 62 | + labels: ListOfLabels, |
| 63 | + ) -> None: |
| 64 | + if hasattr(self, "_model"): |
| 65 | + self.clear_cache() |
| 66 | + |
| 67 | + self._validate_task(labels) |
| 68 | + |
| 69 | + if self._multilabel: |
| 70 | + labels_array = np.array(labels) if not isinstance(labels, np.ndarray) else labels |
| 71 | + num_labels = labels_array.shape[1] |
| 72 | + else: |
| 73 | + num_labels = len(set(labels)) |
| 74 | + |
| 75 | + model_name = self.model_config.model_name |
| 76 | + self._tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 77 | + self._model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels) |
| 78 | + |
| 79 | + def tokenize_function(examples: dict[str, Any]) -> dict[str, Any]: |
| 80 | + return self._tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128) |
| 81 | + |
| 82 | + dataset = Dataset.from_dict({"text": utterances, "labels": labels}) |
| 83 | + tokenized_dataset = dataset.map(tokenize_function, batched=True) |
| 84 | + |
| 85 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 86 | + training_args = TrainingArguments( |
| 87 | + output_dir=tmp_dir, |
| 88 | + num_train_epochs=self.num_train_epochs, |
| 89 | + per_device_train_batch_size=self.batch_size, |
| 90 | + learning_rate=self.learning_rate, |
| 91 | + seed=self.seed, |
| 92 | + save_strategy="no", |
| 93 | + logging_strategy="no", |
| 94 | + report_to="none", |
| 95 | + ) |
| 96 | + |
| 97 | + trainer = Trainer( |
| 98 | + model=self._model, |
| 99 | + args=training_args, |
| 100 | + train_dataset=tokenized_dataset, |
| 101 | + tokenizer=self._tokenizer, |
| 102 | + data_collator=DataCollatorWithPadding(tokenizer=self._tokenizer), |
| 103 | + ) |
| 104 | + |
| 105 | + trainer.train() |
| 106 | + |
| 107 | + self._model.eval() |
| 108 | + |
| 109 | + def predict(self, utterances: list[str]) -> npt.NDArray[Any]: |
| 110 | + if not hasattr(self, "_model") or not hasattr(self, "_tokenizer"): |
| 111 | + msg = "Model is not trained. Call fit() first." |
| 112 | + raise RuntimeError(msg) |
| 113 | + |
| 114 | + inputs = self._tokenizer(utterances, padding=True, truncation=True, max_length=128, return_tensors="pt") |
| 115 | + |
| 116 | + with torch.no_grad(): |
| 117 | + outputs = self._model(**inputs) |
| 118 | + logits = outputs.logits |
| 119 | + |
| 120 | + if self._multilabel: |
| 121 | + return torch.sigmoid(logits).numpy() |
| 122 | + return torch.softmax(logits, dim=1).numpy() |
| 123 | + |
| 124 | + |
| 125 | + def clear_cache(self) -> None: |
| 126 | + if hasattr(self, "_model"): |
| 127 | + del self._model |
| 128 | + if hasattr(self, "_tokenizer"): |
| 129 | + del self._tokenizer |
0 commit comments