adriangb / scikeras

Scikit-Learn API wrapper for Keras.
https://www.adriangb.com/scikeras/
MIT License
239 stars 47 forks source link

SciKeras 0.13.0 / keras 3.2.1 - ModuleNotFoundError: No module named 'keras.src' #321

Closed jbeno closed 4 months ago

jbeno commented 5 months ago

I'm using: scikeras 0.13.0, keras 3.2.1. While importing scikeras, I saw this error. It seems that editing _savings_utils.py to not reference ".src" allowed it to load:

Causes import error: from keras.src.saving.saving_lib import load_model, save_model

No import error (but did not test functionality): from keras.saving.saving_lib import load_model, save_model

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 37
     35 # Import TensorFlow and Keras
     36 from keras.callbacks import EarlyStopping
---> 37 from scikeras.wrappers import KerasClassifier
     38 from keras.utils import to_categorical
     39 from keras.models import Sequential

File /opt/anaconda3/envs/datawaza/lib/python3.10/site-packages/scikeras/__init__.py:11
      7 __version__ = importlib_metadata.version("scikeras")  # type: ignore
      9 import keras as _keras
---> 11 from scikeras import _saving_utils
     13 _keras.Model.__reduce__ = _saving_utils.pack_keras_model
     14 _keras.Model.__deepcopy__ = _saving_utils.deepcopy_model

File /opt/anaconda3/envs/datawaza/lib/python3.10/site-packages/scikeras/_saving_utils.py:7
      5 import keras.saving
      6 import numpy as np
----> 7 from keras.src.saving.saving_lib import load_model, save_model
     10 def unpack_keras_model(
     11     packed_keras_model: np.ndarray,
     12 ):
     13     """Reconstruct a model from the result of __reduce__"""

ModuleNotFoundError: No module named 'keras.src'
adriangb commented 5 months ago

Thanks for proposing a fix. Would you like to make a PR with it?

jbeno commented 4 months ago

The scikeras tests only pass with the keras.src reference. When I removed .src, I got this:

_____________________________________________________________________________ ERROR collecting tests/test_api.py ______________________________________________________________________________
ImportError while importing test module '/Users/jim/git/scikeras/tests/test_api.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/anaconda3/envs/scikeras/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_api.py:28: in <module>
    from scikeras.wrappers import KerasClassifier, KerasRegressor
scikeras/__init__.py:11: in <module>
    from scikeras import _saving_utils
scikeras/_saving_utils.py:7: in <module>
    from keras.saving.saving_lib import load_model, save_model
E   ModuleNotFoundError: No module named 'keras.saving.saving_lib'

This made me think the problem might be in my other environment. I reinstalled keras without cache, and the import worked okay. So looks like no change to scikeras required, and this can be closed.