|
| 1 | +"""Base predictor class.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from abc import ABC, abstractmethod |
| 6 | +from typing import Any, Dict, List, Optional |
| 7 | + |
| 8 | +import torch |
| 9 | +from jaxtyping import Float |
| 10 | +from sklearn.base import BaseEstimator |
| 11 | + |
| 12 | +from ..manifolds import ProductManifold |
| 13 | + |
| 14 | + |
| 15 | +class BasePredictor(BaseEstimator, ABC): |
| 16 | + """Base class for everything in `manify.predictors`. |
| 17 | +
|
| 18 | + This is an abstract class that defines a common interface for all mixed-curvature predictors. We assume only that a |
| 19 | + ProductManifold object is given. We try to follow the scikit-learn API's fit/predict_proba/predict paradigm as |
| 20 | + closely as possible, while accommodating the nuances of product manifold geometry and Pytorch/Geoopt. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + pm: ProductManifold object associated with the predictor. |
| 24 | + task: Task type, either "classification" or "regression". |
| 25 | + random_state: Random state for reproducibility. |
| 26 | + device: Device for tensor computations. If not provided, defaults to pm.device. |
| 27 | + loss_history_: History of loss values during training. |
| 28 | + is_fitted_: Boolean flag indicating if the predictor has been fitted. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + pm: ProductManifold, |
| 34 | + task: Literal["classification", "regression"], |
| 35 | + random_state: Optional[int] = None, |
| 36 | + device: Optional[str] = None, |
| 37 | + ) -> None: |
| 38 | + self.pm = pm |
| 39 | + self.task = task |
| 40 | + self.random_state = random_state |
| 41 | + self.device = pm.device if device is None else device |
| 42 | + self.loss_history_: Dict[str, List[float]] = {} |
| 43 | + self.is_fitted_: bool = False |
| 44 | + |
| 45 | + @abstractmethod |
| 46 | + def fit( |
| 47 | + self, X: Float[torch.Tensor, "n_points n_features"], y: Float[torch.Tensor, "n_points n_classes"] |
| 48 | + ) -> "BasePredictor": |
| 49 | + """Abstract method to fit a predictor. Requires features and labels. |
| 50 | +
|
| 51 | + Args: |
| 52 | + X: Features to fit. |
| 53 | + y: Labels for the features. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + self: Fitted predictor instance. |
| 57 | + """ |
| 58 | + pass |
| 59 | + |
| 60 | + @abstractmethod |
| 61 | + def predict_proba( |
| 62 | + self, X: Optional[Float[torch.Tensor, "n_points n_features"]] |
| 63 | + ) -> Float[torch.Tensor, "n_points n_classes"]: |
| 64 | + """Compute the predicted probabilities for the given features. |
| 65 | +
|
| 66 | + Args: |
| 67 | + X: New inputs for which to make predictions. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + X_proba: Predicted probabilities for the input features. |
| 71 | + """ |
| 72 | + pass |
| 73 | + |
| 74 | + def predict( |
| 75 | + self, X: Optional[Float[torch.Tensor, "n_points n_features"]] |
| 76 | + ) -> Float[torch.Tensor, "n_points n_classes"]: |
| 77 | + """Compute the predicted classes for the given features. |
| 78 | +
|
| 79 | + Args: |
| 80 | + X: New inputs for which to make predictions. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + X_proba: Predicted probabilities for the input features. |
| 84 | + """ |
| 85 | + if self.task == "regression": |
| 86 | + return self.predict_proba(X=X) |
| 87 | + return self.predict_proba(X=X).argmax(dim=-1) |
0 commit comments