dreamquark-ai / tabnet

PyTorch implementation of TabNet paper : https://arxiv.org/pdf/1908.07442.pdf
https://dreamquark-ai.github.io/tabnet/
MIT License
2.55k stars 470 forks source link

RuntimeError: Could not infer dtype of numpy.float32 when fitting TabModel #466

Closed 5020hunter closed 1 year ago

5020hunter commented 1 year ago

I have copied the regression_example.ipynb file onto my machine. When I try to fit the TabModel, I receive a RuntimeError: Could not infer dtype of numpy.float32

I have tried changing all dtypes of the training, validation, and testing data to type numpy.int32, numpy.float32, numpy.int64, and numpy.float64

OS: Windows 11 python version: 3.10.0 torch version: 2.0.0

The Error Message: ----> 1 clf.fit( 2 X_train=X_train, y_train=y_train, 3 eval_set=[(X_train, y_train), (X_valid, y_valid)], 4 eval_name=['train', 'valid'], 5 eval_metric=['rmsle', 'mae', 'rmse', 'mse'], 6 max_epochs=max_epochs, 7 patience=50, 8 batch_size=1024, virtual_batch_size=128, 9 num_workers=0, 10 drop_last=False, 11 augmentations=aug, #aug 12 )

File c:\Users\5020h\AppData\Local\Programs\Python\Python310\lib\site-packages\pytorch_tabnet\abstract_model.py:241, in TabModel.fit(self, X_train, y_train, eval_set, eval_name, eval_metric, loss_fn, weights, max_epochs, patience, batch_size, virtual_batch_size, num_workers, drop_last, callbacks, pin_memory, from_unsupervised, warm_start, augmentations) 236 for epoch_idx in range(self.max_epochs): 237 238 # Call method on_epoch_begin for all callbacks 239 self._callback_container.on_epoch_begin(epoch_idx) --> 241 self._train_epoch(train_dataloader) 243 # Apply predict epoch to all eval sets 244 for eval_name, valid_dataloader in zip(eval_names, valid_dataloaders): ... 169 if np_str_obj_array_pattern.search(elem.dtype.str) is not None: 170 raise TypeError(default_collate_err_msg_format.format(elem.dtype)) --> 172 return collate([torch.as_tensor(b) for b in batch], collate_fn_map=collate_fn_map)

RuntimeError: Could not infer dtype of numpy.float32

Optimox commented 1 year ago

Any chance you could try that with torch < 2.0 ?

5020hunter commented 1 year ago

Tried with torch == 1.13, got the same error.

Optimox commented 1 year ago

Could you share a minimal reproducible code that genrates the error ? A code which generates data similar to your problem which generates this error ?

5020hunter commented 1 year ago

`from pytorch_tabnet.tab_model import TabNetRegressor

import torch from sklearn.preprocessing import LabelEncoder from sklearn.metrics import mean_squared_error

import pandas as pd import numpy as np np.random.seed(0)

import os import wget from pathlib import Path

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" dataset_name = 'census-income' out = Path(os.getcwd()+'/data/'+dataset_name+'.csv')

out.parent.mkdir(parents=True, exist_ok=True) if out.exists(): print("File already exists.") else: print("Downloading file...") wget.download(url, out.as_posix())

train = pd.read_csv(out) target = ' <=50K' if "Set" not in train.columns: train["Set"] = np.random.choice(["train", "valid", "test"], p =[.8, .1, .1], size=(train.shape[0],))

train_indices = train[train.Set=="train"].index valid_indices = train[train.Set=="valid"].index test_indices = train[train.Set=="test"].index

categorical_columns = [] categorical_dims = {} for col in train.columns[train.dtypes == object]: print(col, train[col].nunique()) l_enc = LabelEncoder() train[col] = train[col].fillna("VV_likely") train[col] = l_enc.fit_transform(train[col].values) categorical_columns.append(col) categorical_dims[col] = len(lenc.classes)

for col in train.columns[train.dtypes == np.number]: train.fillna(train.loc[train_indices, col].mean(), inplace=True)

unused_feat = ['Set']

features = [ col for col in train.columns if col not in unused_feat+[target]]

cat_idxs = [ i for i, f in enumerate(features) if f in categorical_columns]

cat_dims = [ categorical_dims[f] for i, f in enumerate(features) if f in categorical_columns]

define your embedding sizes : here just a random choice

cat_emb_dim = [5, 4, 3, 6, 2, 2, 1, 10]

clf = TabNetRegressor(cat_dims=cat_dims, cat_emb_dim=cat_emb_dim, cat_idxs=cat_idxs)

X_train = train[features].values[train_indices] y_train = train[target].values[train_indices].reshape(-1, 1)

X_valid = train[features].values[valid_indices] y_valid = train[target].values[valid_indices].reshape(-1, 1)

X_test = train[features].values[test_indices] y_test = train[target].values[test_indices].reshape(-1, 1)

max_epochs = 100 if not os.getenv("CI", False) else 2

from pytorch_tabnet.augmentations import RegressionSMOTE aug = RegressionSMOTE(p=0.2)

clf.fit( X_train=X_train, y_train=y_train, eval_set=[(X_train, y_train), (X_valid, y_valid)], eval_name=['train', 'valid'], eval_metric=['rmsle', 'mae', 'rmse', 'mse'], max_epochs=max_epochs, patience=50, batch_size=1024, virtual_batch_size=128, num_workers=0, drop_last=False, augmentations=aug, #aug )`

Optimox commented 1 year ago

This is the original code, which is tested by CI. You must have some broken set of dependencies IMO.