TabPFN icon indicating copy to clipboard operation
TabPFN copied to clipboard

TabPFNRegressor fails on constant input data

Open noahho opened this issue 9 months ago • 1 comments

Describe the bug

When fitting on constant regression target, errors

Steps/Code to Reproduce

import numpy as np
from tabpfn import TabPFNRegressor

# Set random seed for reproducibility
np.random.seed(42)

# Create a synthetic dataset with constant targets
n_samples = 100
n_features = 10
constant_value = 5.0

# Generate random features
X = np.random.normal(size=(n_samples, n_features))

# Create constant target values
y = np.ones(n_samples) * constant_value

# Split data into training and testing sets
n_train = int(0.8 * n_samples)
X_train, X_test = X[:n_train], X[n_train:]
y_train, y_test = y[:n_train], y[n_train:]

print(f"Training with {n_train} samples, testing with {n_samples - n_train} samples")
print(f"Target values are constant: {constant_value}")

# Initialize and train the tabPFN regressor
model = TabPFNRegressor(device='cpu')

# Train the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Print a few predictions to see the results
print(f"First 5 predictions: {y_pred[:5]}")
print(f"Expected value: {constant_value}")

Expected Results

No error is thrown, predict constant value

Actual Results


AssertionError Traceback (most recent call last) in <cell line: 0>() 28 29 # Train the model ---> 30 model.fit(X_train, y_train) 31 32 # Make predictions

3 frames /content/tabpfn/src/tabpfn/model/bar_distribution.py in init(self, borders, ignore_nan_targets) 38 full_width = self.bucket_widths.sum() 39 ---> 40 assert (1 - (full_width / (self.borders[-1] - self.borders[0]))).abs() < 1e-2, ( 41 f"diff: {full_width - (self.borders[-1] - self.borders[0])} with" 42 f" {full_width} {self.borders[-1]} {self.borders[0]}"

AssertionError: diff: 0.0 with 0.0 5.0 5.0

Versions

PyTorch version: 2.6.0+cu124
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.31.6
Libc version: glibc-2.35

Python version: 3.11.11 (main, Dec  4 2024, 08:55:07) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.1.85+-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.5.82
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-SXM4-40GB
Nvidia driver version: 550.54.15
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.2.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.2.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                         x86_64
CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               12
On-line CPU(s) list:                  0-11
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Xeon(R) CPU @ 2.20GHz
CPU family:                           6
Model:                                85
Thread(s) per core:                   2
Core(s) per socket:                   6
Socket(s):                            1
Stepping:                             7
BogoMIPS:                             4400.29
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat avx512_vnni md_clear arch_capabilities
Hypervisor vendor:                    KVM
Virtualization type:                  full
L1d cache:                            192 KiB (6 instances)
L1i cache:                            192 KiB (6 instances)
L2 cache:                             6 MiB (6 instances)
L3 cache:                             38.5 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-11
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Vulnerable
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Vulnerable
Vulnerability Spectre v1:             Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
Vulnerability Spectre v2:             Vulnerable; IBPB: disabled; STIBP: disabled; PBRSB-eIBRS: Vulnerable; BHI: Vulnerable (Syscall hardening enabled)
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Vulnerable

Dependency Versions:
--------------------
tabpfn: 2.0.6
torch: 2.6.0+cu124
numpy: 1.26.4
scipy: 1.14.1
pandas: 2.2.2
scikit-learn: 1.5.2
typing_extensions: 4.12.2
einops: 0.8.1
huggingface-hub: 0.28.1

noahho avatar Mar 18 '25 19:03 noahho

The root cause that I have pinpointed is that the model computes target value range by utilizing max (y) - min (y) and for constant targets, this leads to a range of zero. I would like to propose two possible approaches:

  1. Add Tiny Noise to Targets This solution preserves TabPFN's learning capability while maintaining practically constant predictions. It's particularly valuable for solar forecasting applications where true constant targets are rare but minor variations exist in real-world scenarios.

  2. Skip Fitting for Constant Targets
    This approach provides explicit edge case handling and avoids unnecessary computation. It's especially useful when we expect truly constant adjustments (e.g., during equipment failures or maintenance periods).

The first solution offers more flexibility for real-world conditions, while the second provides computational efficiency for true constants.

I want your opinion on the following:

  • Which approach better aligns with our production needs?
  • Should we implement both with a configuration flag?
  • Are there other edge cases we should consider in solar forecasting?

Sample Implementation

# Solution 1: Tiny noise addition
y = y_constant + np.random.normal(0, 1e-6, len(y_constant))

# Solution 2: Skip fitting
if np.ptp(y) == 0:  # If all values are identical
    return np.full_like(X_test[:, 0], y[0])

Would love to hear thoughts from other contributors on these approaches!

yashasvisharma20 avatar Mar 29 '25 08:03 yashasvisharma20