Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup feature selection function #168

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/tabpfn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from importlib.metadata import version

from tabpfn.classifier import TabPFNClassifier
from tabpfn.regressor import TabPFNRegressor
from importlib.metadata import version

try:
__version__ = version(__name__)
Expand Down
35 changes: 17 additions & 18 deletions src/tabpfn/model/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,23 @@ def select_features(x: torch.Tensor, sel: torch.Tensor) -> torch.Tensor:
Returns:
The tensor with selected features.
"""
new_x = x.clone()
for B in range(x.shape[1]):
if x.shape[1] > 1:
new_x[:, B, :] = torch.cat(
[
x[:, B, sel[B]],
torch.zeros(
x.shape[0],
x.shape[-1] - sel[B].sum(),
device=x.device,
dtype=x.dtype,
),
],
-1,
)
else:
# If B == 1, we don't need to append zeros, as the number of features can change
new_x = x[:, :, sel[B]]
B, total_features = sel.shape
batch_size = x.shape[0]

# If B == 1, we don't need to append zeros, as the number of features don't need to be fixed.
if B == 1:
return x[:, :, sel[0]]

new_x = torch.zeros((batch_size, B, total_features), device=x.device, dtype=x.dtype)

# For each block, compute the number of selected features.
sel_counts = sel.sum(dim=-1) # shape: (B,)

for b in range(B):
s = int(sel_counts[b])
if s > 0:
new_x[:, b, :s] = x[:, b, sel[b]]

return new_x


Expand Down
Loading