Skip to content

Commit 32ddddb

Browse files
committed
feat: expose random seed, small typing ix
1 parent 8f46692 commit 32ddddb

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

model2vec/train/classifier.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from model2vec.train.base import FinetunableStaticModel, TextDataset
2424

2525
logger = logging.getLogger(__name__)
26-
_RANDOM_SEED = 42
26+
_DEFAULT_RANDOM_SEED = 42
2727

2828
LabelType = TypeVar("LabelType", list[str], list[list[str]])
2929

@@ -158,6 +158,7 @@ def fit( # noqa: C901 # Complexity is bad.
158158
X_val: list[str] | None = None,
159159
y_val: LabelType | None = None,
160160
class_weight: torch.Tensor | None = None,
161+
seed: int = _DEFAULT_RANDOM_SEED,
161162
) -> StaticModelForClassification:
162163
"""
163164
Fit a model.
@@ -187,14 +188,14 @@ def fit( # noqa: C901 # Complexity is bad.
187188
:param y_val: The labels to be used for validation.
188189
:param class_weight: The weight of the classes. If None, all classes are weighted equally. Must
189190
have the same length as the number of classes.
191+
:param seed: The random seed to use. Defaults to 42.
190192
:return: The fitted model.
191193
:raises ValueError: If either X_val or y_val are provided, but not both.
192194
"""
193-
pl.seed_everything(_RANDOM_SEED)
195+
pl.seed_everything(seed)
194196
logger.info("Re-initializing model.")
195197

196198
# Determine whether the task is multilabel based on the type of y.
197-
198199
self._initialize(y)
199200

200201
if (X_val is not None) != (y_val is not None):
@@ -380,7 +381,7 @@ def to_pipeline(self) -> StaticModelPipeline:
380381
"""Convert the model to an sklearn pipeline."""
381382
static_model = self.to_static_model()
382383

383-
random_state = np.random.RandomState(_RANDOM_SEED)
384+
random_state = np.random.RandomState(_DEFAULT_RANDOM_SEED)
384385
n_items = len(self.classes)
385386
X = random_state.randn(n_items, static_model.dim)
386387
y = self.classes

0 commit comments

Comments
 (0)