jrzaurin / pytorch-widedeep

A flexible package for multimodal-deep-learning to combine tabular data with text and images using Wide and Deep models in Pytorch
Apache License 2.0
1.3k stars 190 forks source link

How to install the previous version, the current 1.5 version has been working problems? #203

Closed taylover12138 closed 8 months ago

taylover12138 commented 8 months ago

How to install the previous version, the current 1.5 version has been working problems?

jrzaurin commented 8 months ago

Hey @taylover12138

Thanks for using the library!

pip install pytorch-widedeep==1.4.0

Will install the previous version

However, if you give me more details on the functoning problems that the current version has I will try to fix them 🙂

taylover12138 commented 8 months ago

I ran the example on your GitHub and it gave me this error: 屏幕截图 2024-03-08 145438 ![Uploading 屏幕截图 2024-03-08 145557.png…]()

taylover12138 commented 8 months ago

Hey @taylover12138

Thanks for using the library!

pip install pytorch-widedeep==1.4.0

Will install the previous version

However, if you give me more details on the functoning problems that the current version has I will try to fix them 🙂

Now the program gets an error: C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\datasets_base.py:49: DeprecationWarning: path is deprecated. Use files() instead. Refer to https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy for migration advice. with resources.path( C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\preprocessing\tab_preprocessor.py:360: UserWarning: Continuous columns will not be normalised warnings.warn("Continuous columns will not be normalised") 0%| | 0/153 [00:00<?, ?it/s]Traceback (most recent call last): File "", line 1, in File "C:\Users\tgz.conda\envs\tabnet\Lib\multiprocessing\spawn.py", line 122, in spawn_main exitcode = _main(fd, parent_sentinel) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\tgz.conda\envs\tabnet\Lib\multiprocessing\spawn.py", line 131, in _main prepare(preparation_data) File "C:\Users\tgz.conda\envs\tabnet\Lib\multiprocessing\spawn.py", line 246, in prepare _fixup_main_from_path(data['init_main_from_path']) File "C:\Users\tgz.conda\envs\tabnet\Lib\multiprocessing\spawn.py", line 297, in _fixup_main_from_path main_content = runpy.run_path(main_path, ^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 291, in run_path File "", line 98, in _run_module_code File "", line 88, in _run_code File "E:\projects\class\tabnet\src\tabdemo.py", line 6, in from pytorch_widedeep import Trainer File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep__init.py", line 9, in from pytorch_widedeep.utils import ( File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\utils__init.py", line 1, in from pytorch_widedeep.utils.text_utils import ( File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\utils\text_utils.py", line 7, in from pytorch_widedeep.utils.fastai_transforms import ( File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\utils\fastai_transforms.py", line 21, in from pytorch_widedeep.wdtypes import ( File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\pytorch_widedeep\wdtypes.py", line 23, in from torchvision.transforms import ( File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision__init.py", line 6, in from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision\models__init.py", line 2, in from .convnext import * File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision\models\convnext.py", line 8, in from ..ops.misc import Conv2dNormActivation, Permute File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision\ops__init__.py", line 23, in from .poolers import MultiScaleRoIAlign File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision\ops\poolers.py", line 10, in from .roi_align import roi_align File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torchvision\ops\roi_align.py", line 4, in import torch._dynamo File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torch_dynamo\init__.py", line 2, in from . import allowed_functions, convert_frame, eval_frame, resume_execution File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torch_dynamo\allowed_functions.py", line 30, in from .utils import hashable, is_safe_constant, NP_SUPPORTED_MODULES File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torch_dynamo\utils.py", line 89, in import torch.fx.experimental.symbolic_shapes File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torch\fx\experimental\symbolic_shapes.py", line 38, in from torch.utils._sympy.functions import FloorDiv, Mod, IsNonOverlappingAndDenseIndicator File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\torch\utils_sympy\functions.py", line 1, in import sympy File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\init__.py", line 74, in from .polys import (Poly, PurePoly, poly_from_expr, parallel_poly_from_expr, File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\init__.py", line 68, in from .polytools import (Poly, PurePoly, poly_from_expr, File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\polytools.py", line 26, in from sympy.polys.constructor import construct_domain File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\constructor.py", line 7, in from sympy.polys.domains import ZZ, QQ, ZZ_I, QQ_I, EX File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\domains\init__.py", line 15, in from .algebraicfield import AlgebraicField File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\domains\algebraicfield.py", line 10, in from sympy.polys.polyclasses import ANP File "C:\Users\tgz.conda\envs\tabnet\Lib\site-packages\sympy\polys\polyclasses.py", line 120, in from sympy.polys.factortools import ( File "", line 1176, in _find_and_load File "", line 1147, in _find_and_load_unlocked File "", line 690, in _load_unlocked File "", line 936, in exec_module File "", line 1032, in get_code File "", line 1131, in get_data MemoryError

taylover12138 commented 8 months ago

I'm still using the same version: 1.5.0

taylover12138 commented 8 months ago

When I changed to version 1.2.0, the program gave me an error: 0%| | 0/153 [00:48<?, ?it/s] Traceback (most recent call last): File "E:\projects\class\tabnet\src\tabdemo.py", line 81, in main() File "E:\projects\class\tabnet\src\tabdemo.py", line 71, in main batch_size=256, File "C:\Users\tgz.conda\envs\tab\lib\site-packages\pytorch_widedeep\utils\general_utils.py", line 61, in call return wrapped(*args, **kwargs) File "C:\Users\tgz.conda\envs\tab\lib\site-packages\pytorch_widedeep\training\trainer.py", line 486, in fit for batch_idx, (data, targett, lds_weightt) in zip(t, train_loader): File "C:\Users\tgz.conda\envs\tab\lib\site-packages\torch\utils\data\dataloader.py", line 435, in iter return self._get_iterator() File "C:\Users\tgz.conda\envs\tab\lib\site-packages\torch\utils\data\dataloader.py", line 381, in _get_iterator return _MultiProcessingDataLoaderIter(self) File "C:\Users\tgz.conda\envs\tab\lib\site-packages\torch\utils\data\dataloader.py", line 1034, in init w.start() File "C:\Users\tgz.conda\envs\tab\lib\multiprocessing\process.py", line 112, in start self._popen = self._Popen(self) File "C:\Users\tgz.conda\envs\tab\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Users\tgz.conda\envs\tab\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "C:\Users\tgz.conda\envs\tab\lib\multiprocessing\popen_spawn_win32.py", line 89, in init reduction.dump(process_obj, to_child) File "C:\Users\tgz.conda\envs\tab\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) BrokenPipeError: [Errno 32] Broken pipe

jrzaurin commented 8 months ago

@5uperpalo you have a windows machine and maybe you can help?

This is that conda/spawn mutiprocessing issue.

@taylover12138 in the meantime, do you have access to a linux machine?

taylover12138 commented 8 months ago

@5uperpalo you have a windows machine and maybe you can help?

This is that conda/spawn mutiprocessing issue.

@taylover12138 in the meantime, do you have access to a linux machine?

I don't have Linux, I have Windows

taylover12138 commented 8 months ago

I wanted to change 'num_workers', but I couldn't find the hyperparameter

taylover12138 commented 8 months ago

sir,i can use it in the linux machine,but still not in windows

5uperpalo commented 8 months ago

@taylover12138 could you please provide a code snippet(or example script from the repository) that you are trying to run? I will try to run/troubleshoot it in Win10 OS , but tbh I never run any code under Win, I always go for WSL due to torch, torch_vision, multiprocessing etc. related issues when using Windows

taylover12138 commented 8 months ago

@taylover12138 could you please provide a code snippet(or example script from the repository) that you are trying to run? I will try to run/troubleshoot it in Win10 OS , but tbh I never run any code under Win, I always go for WSL due to torch, torch_vision, multiprocessing etc. related issues when using Windows I ran the example code you provided: import numpy as np import torch from sklearn.model_selection import train_test_split

from pytorch_widedeep import Trainer from pytorch_widedeep.preprocessing import WidePreprocessor, TabPreprocessor from pytorch_widedeep.models import Wide, TabMlp, WideDeep from pytorch_widedeep.metrics import Accuracy from pytorch_widedeep.datasets import load_adult

df = load_adult(as_frame=True) df["income_label"] = (df["income"].apply(lambda x: ">50K" in x)).astype(int) df.drop("income", axis=1, inplace=True) df_train, df_test = train_test_split(df, test_size=0.2, stratify=df.income_label)

Define the 'column set up'

wide_cols = [ "education", "relationship", "workclass", "occupation", "native-country", "gender", ] crossed_cols = [("education", "occupation"), ("native-country", "occupation")]

cat_embed_cols = [ "workclass", "education", "marital-status", "occupation", "relationship", "race", "gender", "capital-gain", "capital-loss", "native-country", ] continuous_cols = ["age", "hours-per-week"] target = "income_label" target = df_train[target].values

prepare the data

wide_preprocessor = WidePreprocessor(wide_cols=wide_cols, crossed_cols=crossed_cols) X_wide = wide_preprocessor.fit_transform(df_train)

tab_preprocessor = TabPreprocessor( cat_embed_cols=cat_embed_cols, continuous_cols=continuous_cols # type: ignore[arg-type] ) X_tab = tab_preprocessor.fit_transform(df_train)

build the model

wide = Wide(input_dim=np.unique(X_wide).shape[0], pred_dim=1) tab_mlp = TabMlp( column_idx=tab_preprocessor.column_idx, cat_embed_input=tab_preprocessor.cat_embed_input, continuous_cols=continuous_cols, ) model = WideDeep(wide=wide, deeptabular=tab_mlp)

train and validate

trainer = Trainer(model, objective="binary", metrics=[Accuracy]) trainer.fit( X_wide=X_wide, X_tab=X_tab, target=target, n_epochs=5, batch_size=256, )

predict on test

X_wide_te = wide_preprocessor.transform(df_test) X_tab_te = tab_preprocessor.transform(df_test) preds = trainer.predict(X_wide=X_wide_te, X_tab=X_tab_te)

Save and load

Option 1: this will also save training history and lr history if the

LRHistory callback is used

trainer.save(path="model_weights", save_state_dict=True)

Option 2: save as any other torch model

torch.save(model.state_dict(), "model_weights/wd_model.pt")

From here in advance, Option 1 or 2 are the same. I assume the user has

prepared the data and defined the new model components:

1. Build the model

model_new = WideDeep(wide=wide, deeptabular=tab_mlp) model_new.load_state_dict(torch.load("model_weights/wd_model.pt"))

2. Instantiate the trainer

trainer_new = Trainer(model_new, objective="binary")

3. Either start the fit or directly predict

preds = trainer_new.predict(X_wide=X_wide, X_tab=X_tab)

And,My package's version is as follows: Package Version


annotated-types 0.6.0 blis 0.7.11 catalogue 2.0.10 certifi 2024.2.2 charset-normalizer 3.3.2 click 8.1.7 cloudpathlib 0.16.0 colorama 0.4.6 confection 0.1.4 cramjam 2.8.2 cymem 2.0.8 einops 0.7.0 fastparquet 2024.2.0 filelock 3.9.0 fsspec 2023.4.0 gensim 4.3.2 idna 3.6 imutils 0.5.4 Jinja2 3.1.2 joblib 1.3.2 langcodes 3.3.0 lightning-utilities 0.10.1 MarkupSafe 2.1.3 mpmath 1.3.0 murmurhash 1.0.10 networkx 3.2.1 numpy 1.26.3 opencv-contrib-python 4.9.0.80 packaging 23.2 pandas 2.2.1 pillow 10.2.0 pip 23.3.1 preshed 3.0.9 pyarrow 15.0.1 pydantic 2.6.3 pydantic_core 2.16.3 python-dateutil 2.9.0.post0 pytorch-widedeep 1.5.0 pytz 2024.1 requests 2.31.0 scikit-learn 1.4.1.post1 scipy 1.12.0 setuptools 68.2.2 six 1.16.0 smart-open 6.4.0 spacy 3.7.4 spacy-legacy 3.0.12 spacy-loggers 1.0.5 srsly 2.4.8 sympy 1.12 thinc 8.2.3 threadpoolctl 3.3.0 torch 2.2.1+cu121 torchaudio 2.2.1+cu121 torchmetrics 1.3.1 torchvision 0.17.1+cu121 tqdm 4.66.2 typer 0.9.0 typing_extensions 4.8.0 tzdata 2024.1 urllib3 2.2.1 wasabi 1.1.2 weasel 0.3.4 wheel 0.41.2 wrapt 1.16.0

5uperpalo commented 8 months ago

@taylover12138

what I did

if name == 'main' df = load_adult(as_frame=True) ... preds = trainer_new.predict(X_wide=X_wide, X_tab=X_tab, batch_size=256)


* now it is working

Please confirm if this fixes your problem?

# Explanation
* follow the link in [this comment](https://stackoverflow.com/a/18205086) - "For an explanation of why (on Windows) the if __name__ == '__main__'  part is necessary, see Programming guidelines."

# Note
* please for your own sake avoid multiprocessing and possibly python coding under Windows and rather use WSL and venv as opposed to Win10 + Anaconda
jrzaurin commented 8 months ago

@5uperpalo thank you for stepping in 😉

taylover12138 commented 8 months ago

@taylover12138

what I did

  • I created "fresh" python 3.8 env in my Conda on WIndows 10; I never use python/vs code/conda under Windows so I have some 2-3years old Anaconda installation, but good enough for troubleshooting
  • I did pip install pytorch_widedeep under this env
  • I ran the code from README.MD of the repo
  • I received same error as you did
  • I added if __name__ == '__main__' before the start of the code and batch_size to the final line to avoid the error in the predictions, i.e.:
from pytorch_widedeep.datasets import load_adult

if __name__ == '__main__'
  df = load_adult(as_frame=True)
  ...
  preds = trainer_new.predict(X_wide=X_wide, X_tab=X_tab, batch_size=256)
  • now it is working

Please confirm if this fixes your problem?

Explanation

  • follow the link in this comment - "For an explanation of why (on Windows) the if name == 'main' part is necessary, see Programming guidelines."

Note

  • please for your own sake avoid multiprocessing and possibly python coding under Windows and rather use WSL and venv as opposed to Win10 + Anaconda

@jrzaurin and @5uperpalo Thank you so much sir !i fixed it!