|
23 | 23 | from model2vec.train.base import FinetunableStaticModel, TextDataset |
24 | 24 |
|
25 | 25 | logger = logging.getLogger(__name__) |
26 | | -_RANDOM_SEED = 42 |
| 26 | +_DEFAULT_RANDOM_SEED = 42 |
27 | 27 |
|
28 | 28 | LabelType = TypeVar("LabelType", list[str], list[list[str]]) |
29 | 29 |
|
@@ -158,6 +158,7 @@ def fit( # noqa: C901 # Complexity is bad. |
158 | 158 | X_val: list[str] | None = None, |
159 | 159 | y_val: LabelType | None = None, |
160 | 160 | class_weight: torch.Tensor | None = None, |
| 161 | + seed: int = _DEFAULT_RANDOM_SEED, |
161 | 162 | ) -> StaticModelForClassification: |
162 | 163 | """ |
163 | 164 | Fit a model. |
@@ -187,14 +188,14 @@ def fit( # noqa: C901 # Complexity is bad. |
187 | 188 | :param y_val: The labels to be used for validation. |
188 | 189 | :param class_weight: The weight of the classes. If None, all classes are weighted equally. Must |
189 | 190 | have the same length as the number of classes. |
| 191 | + :param seed: The random seed to use. Defaults to 42. |
190 | 192 | :return: The fitted model. |
191 | 193 | :raises ValueError: If either X_val or y_val are provided, but not both. |
192 | 194 | """ |
193 | | - pl.seed_everything(_RANDOM_SEED) |
| 195 | + pl.seed_everything(seed) |
194 | 196 | logger.info("Re-initializing model.") |
195 | 197 |
|
196 | 198 | # Determine whether the task is multilabel based on the type of y. |
197 | | - |
198 | 199 | self._initialize(y) |
199 | 200 |
|
200 | 201 | if (X_val is not None) != (y_val is not None): |
@@ -380,7 +381,7 @@ def to_pipeline(self) -> StaticModelPipeline: |
380 | 381 | """Convert the model to an sklearn pipeline.""" |
381 | 382 | static_model = self.to_static_model() |
382 | 383 |
|
383 | | - random_state = np.random.RandomState(_RANDOM_SEED) |
| 384 | + random_state = np.random.RandomState(_DEFAULT_RANDOM_SEED) |
384 | 385 | n_items = len(self.classes) |
385 | 386 | X = random_state.randn(n_items, static_model.dim) |
386 | 387 | y = self.classes |
|
0 commit comments