File size: 7,768 Bytes
e17f3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e057d08
 
 
e17f3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e057d08
 
 
 
 
 
 
 
 
 
 
 
e17f3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e057d08
e17f3ba
e057d08
 
 
 
 
fc6a2fc
e057d08
fc6a2fc
e057d08
 
e17f3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
TabPFN Wrapper
==============

Sklearn-compatible wrapper for TabPFN (Tabular Pre-trained Transformers).

TabPFN is a pretrained model for tabular classification using
in-context learning (no training required).

Author: UW MSIM Team
Date: November 2025
"""

import time
import logging
import os
from typing import Optional, Union
import numpy as np
import pandas as pd

# Automatically accept the TabPFN license to prevent browser/socket crashes on Windows
os.environ["TABPFN_LICENSE"] = "accept"
os.environ["TABPFN_ACCEPT_LICENSE"] = "1"

# ── Patch for old TabPFN compatibility with newer torch ──────────────────────
try:
    import torch.nn.modules.transformer
    if not hasattr(torch.nn.modules.transformer, 'Optional'):
        import typing
        torch.nn.modules.transformer.Optional = typing.Optional
        torch.nn.modules.transformer.Any = typing.Any
        torch.nn.modules.transformer.Tuple = typing.Tuple
        torch.nn.modules.transformer.List = typing.List
except (ImportError, AttributeError):
    pass

# ── Patch for old TabPFN compatibility with newer sklearn ────────────────────
try:
    import sklearn.utils.validation
    def _patch_validation(func):
        from functools import wraps
        @wraps(func)
        def wrapper(*args, **kwargs):
            if 'force_all_finite' in kwargs:
                kwargs['ensure_all_finite'] = kwargs.pop('force_all_finite')
            return func(*args, **kwargs)
        return wrapper
    sklearn.utils.validation.check_X_y = _patch_validation(sklearn.utils.validation.check_X_y)
    sklearn.utils.validation.check_array = _patch_validation(sklearn.utils.validation.check_array)
except (ImportError, AttributeError):
    pass

from .base_wrapper import BaseModelWrapper

logger = logging.getLogger(__name__)


class TabPFNWrapper(BaseModelWrapper):
    """
    TabPFN (Tabular Prior-Fitted Networks) wrapper.

    TabPFN uses pretrained transformers for zero-shot tabular prediction.
    Works best on datasets with <1000 samples and <100 features.

    Parameters
    ----------
    task_type : str, default='classification'
        Task type (only 'classification' supported by TabPFN)
    n_ensemble : int, default=1
        Number of ensemble members
    device : str, default='auto'
        Device: 'cpu', 'cuda', or 'auto'
    random_state : int, default=42
        Random seed
    """

    def __init__(
        self,
        task_type: str = 'classification',
        n_ensemble: int = 1,
        device: str = 'auto',
        random_state: int = 42
    ):
        super().__init__(task_type=task_type, random_state=random_state)

        if task_type != 'classification':
            raise ValueError("TabPFN only supports classification tasks")

        self.n_ensemble = n_ensemble
        self.device = device

    def fit(self, X: Union[pd.DataFrame, np.ndarray], y: Union[pd.Series, np.ndarray]) -> 'TabPFNWrapper':
        """
        Fit TabPFN (stores training data for in-context learning).

        Parameters
        ----------
        X : pd.DataFrame or np.ndarray, shape (n_samples, n_features)
            Training features (max 1000 samples, 100 features)
        y : pd.Series or np.ndarray, shape (n_samples,)
            Training target

        Returns
        -------
        self : TabPFNWrapper
            Fitted model
        """
        self._validate_input(X, y)

        # Check TabPFN constraints
        if X.shape[0] > 1024:
            logger.warning(f"TabPFN strictly requires <= 1024 samples to avoid Memory OOM. Subsampling {X.shape[0]} to 1024 samples.")
            sample_idx = np.random.RandomState(self.random_state).choice(
                len(X), 1024, replace=False
            )
            if isinstance(X, pd.DataFrame):
                X = X.iloc[sample_idx]
            else:
                X = X[sample_idx]
            
            if isinstance(y, pd.Series):
                y = y.iloc[sample_idx]
            else:
                y = y[sample_idx]

        if X.shape[1] > 100:
            logger.warning(f"TabPFN strictly requires <= 100 features. Truncating {X.shape[1]} to 100 features.")
            if isinstance(X, pd.DataFrame):
                X = X.iloc[:, :100]
            else:
                X = X[:, :100]
            self.truncated_features_ = True
        else:
            self.truncated_features_ = False

        logger.info(f"Fitting TabPFN on {X.shape[0]} samples...")
        start_time = time.time()

        try:
            from tabpfn import TabPFNClassifier

            import torch
            import tabpfn
            
            actual_device = 'cuda' if (self.device == 'auto' and torch.cuda.is_available()) else ('cpu' if self.device == 'auto' else self.device)
            
            if hasattr(tabpfn, '__version__') and tabpfn.__version__.startswith('0.1'):
                self.model = TabPFNClassifier(device=actual_device, N_ensemble_configurations=self.n_ensemble)
            else:
                self.model = TabPFNClassifier(device=actual_device)

            # Fit model
            self.model.fit(X, y)

            self.is_fitted = True
            self.fit_time = time.time() - start_time

            logger.info(f"TabPFN fitted in {self.fit_time:.2f} seconds")

        except ImportError:
            logger.error("TabPFN not installed")
            raise ImportError("Install TabPFN with: pip install tabpfn")
        except Exception as e:
            logger.error(f"Error fitting TabPFN: {e}")
            raise

        return self

    def predict(self, X: Union[pd.DataFrame, np.ndarray]) -> np.ndarray:
        """
        Make predictions with TabPFN.

        Parameters
        ----------
        X : pd.DataFrame or np.ndarray, shape (n_samples, n_features)
            Test features

        Returns
        -------
        predictions : np.ndarray, shape (n_samples,)
            Predicted class labels
        """
        if not self.is_fitted:
            raise ValueError("Model not fitted. Call fit() first.")

        self._validate_input(X)

        if getattr(self, 'truncated_features_', False) and X.shape[1] > 100:
            if isinstance(X, pd.DataFrame):
                X = X.iloc[:, :100]
            else:
                X = X[:, :100]

        logger.info(f"Predicting on {X.shape[0]} samples with TabPFN...")
        start_time = time.time()

        try:
            predictions = self.model.predict(X)
            self.predict_time = time.time() - start_time

            logger.info(f"Predictions complete in {self.predict_time:.2f} seconds")

            return predictions

        except Exception as e:
            logger.error(f"Error during prediction: {e}")
            raise

    def _predict_proba_impl(self, X: Union[pd.DataFrame, np.ndarray]) -> np.ndarray:
        """
        Predict class probabilities with TabPFN.

        Parameters
        ----------
        X : pd.DataFrame or np.ndarray, shape (n_samples, n_features)
            Test features

        Returns
        -------
        probabilities : np.ndarray, shape (n_samples, n_classes)
            Class probabilities
        """
        if getattr(self, 'truncated_features_', False) and X.shape[1] > 100:
            if isinstance(X, pd.DataFrame):
                X = X.iloc[:, :100]
            else:
                X = X[:, :100]

        return self.model.predict_proba(X)

    def get_params(self, deep: bool = True) -> dict:
        """Get parameters for this estimator."""
        params = super().get_params(deep)
        params.update({
            'n_ensemble': self.n_ensemble,
            'device': self.device
        })
        return params