adapt icon indicating copy to clipboard operation
adapt copied to clipboard

ModuleNotFoundError: No module named 'tensorflow.keras.wrappers'

Open adhamenaya opened this issue 9 months ago • 16 comments

I am using tensorflow 2.13.0 and keras 2.13.1. I got this error? could you please advise what is the version of compitable TensorFlow

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[4], line 1
----> 1 import adapt

File ~/miniforge3/envs/tensorflow/lib/python3.10/site-packages/adapt/__init__.py:5
      1 """
      2 ADAPT: Awesome Domain Adaptation Package Toolbox
      3 """
----> 5 from adapt import feature_based
      6 from adapt import instance_based
      7 from adapt import parameter_based

File ~/miniforge3/envs/tensorflow/lib/python3.10/site-packages/adapt/feature_based/__init__.py:5
      1 """
      2 Feature-Based Methods Module
      3 """
----> 5 from ._fa import FA
      6 from ._coral import CORAL
      7 from ._dann import DANN

File ~/miniforge3/envs/tensorflow/lib/python3.10/site-packages/adapt/feature_based/_fa.py:11
      8 from sklearn.utils import check_array
      9 from sklearn.exceptions import NotFittedError
---> 11 from adapt.base import BaseAdaptEstimator, make_insert_doc
     12 from adapt.utils import check_arrays
     15 @make_insert_doc(supervised=True)
     16 class FA(BaseAdaptEstimator):

File ~/miniforge3/envs/tensorflow/lib/python3.10/site-packages/adapt/base.py:16
     14 from sklearn.exceptions import NotFittedError
     15 from tensorflow.keras import Model
---> 16 from tensorflow.keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
     18 from adapt.utils import (check_estimator,
     19                          check_network,
     20                          check_arrays,
   (...)
     25                          get_default_task,
     26                          get_default_discriminator)
     27 from adapt.metrics import normalized_linear_discrepancy

ModuleNotFoundError: No module named 'tensorflow.keras.wrappers'

adhamenaya avatar Nov 09 '23 23:11 adhamenaya

Another Approach you can try pip install tensorflow==2.12.0

divakarkumarp avatar Nov 26 '23 16:11 divakarkumarp

I have the same problem. On MacOS, "pip install tensorflow==2.12.0" does not work either

olivermueller avatar Dec 11 '23 21:12 olivermueller

Hello everyone, Thank you for reporting this issue. Indeed, it seems that there is a problem with tensorflow for MacOS. The adapt package encounters multiple issues with the tensorflow version above 2.12. We think that the problem can be resolved by correcting these bugs. We will let you know, when it is done. Best,

antoinedemathelin avatar Dec 12 '23 08:12 antoinedemathelin

Hi everyone, We made changes to fix the compatibility issue with the last Tensorflow version (2.15). A new ADAPT version has been deployed on Pypi (0.4.4). Please try it and let us know if you still encounter this issue?

PS: you made need to upgrade your Tensorflow version to 2.15, but ADAPT should work with previous versions too.

antoinedemathelin avatar Dec 13 '23 13:12 antoinedemathelin

Beginner here, how do I apply this fix, I'm encountering the same issue. Thanks in advance!

juliog23 avatar Jan 10 '24 05:01 juliog23

Hi @juliog23, Do you know which version of Adapt you are using ? The issue should be resolved with Adapt 0.4.4

Please try (in your environment):

pip install --upgrade adapt

If you are using a jupyter notebook, you can run the following command in a coding cell:

!pip install --upgrade adapt

antoinedemathelin avatar Jan 10 '24 09:01 antoinedemathelin

Thank you for the quick response. I just got around to running the code you supplied and it installed successfully but still getting the same error. When I look in the directory there is no module named “wrappers”. My current file path is “../site-packages/tensorflow/python/keras On Jan 10, 2024, at 1:21 AM, antoinedemathelin @.***> wrote: Hi @juliog23, Do you know which version of Adapt you are using ? The issue should be resolved with Adapt 0.4.4 Please try (in your environment): pip install --upgrade adapt If you are using a jupyter notebook, you can run the following command in a coding cell: !pip install --upgrade adapt

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

juliog23 avatar Jan 11 '24 01:01 juliog23

Hi @juliog23,

Can you please share the whole error message you get ?

Do you work in a conda environment ? If yes, can you please share the versions of your packages by copy-pasting the prompt of:

conda list

Maybe you can resolve the issue by installing scikeras with:

pip install scikeras

antoinedemathelin avatar Jan 11 '24 15:01 antoinedemathelin

Hello, here is the full error message:2024-01-11 10:36:54.385678: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.WARNING:tensorflow:From C:\Users\julio\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead. Traceback (most recent call last):  File "C:\Users\julio\OneDrive\NBA Games\NBA 23-24 Season\polyfit_deep_learning_rev2.py", line 12, in     from tensorflow.keras.wrappers.scikit_learn import KerasRegressorModuleNotFoundError: No module named 'tensorflow.keras.wrappers'

Thank you! On Thursday, January 11, 2024 at 07:09:26 AM PST, antoinedemathelin @.***> wrote:

Hi @juliog23,

Can you please share the whole error message you get ?

Do you work in a conda environment ? If yes, can you please share the versions of your packages by copy-pasting the prompt of: conda list

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

juliog23 avatar Jan 11 '24 18:01 juliog23

Hi @juliog23,

I think the error can be resolved by installing scikeras, can you please try:

pip install scikeras

antoinedemathelin avatar Jan 12 '24 08:01 antoinedemathelin

If the error is still not resolved, you can watch the installed package versions with:

pip list

or

conda list

To verify that you have the last version of adapt installed.

PS : don't forget to put "!" before the commands, if you are executing them in a jupyter cell (!pip list)

antoinedemathelin avatar Jan 12 '24 09:01 antoinedemathelin

I already had scikeras installed but I uninstalled it and then reinstalled it just in case. The scikeras path in my computer is: ".........\Programs\Python\Python311\Lib\site-packages\scikeras"

The code from my understanding is erroring with "ModuleNotFoundError: No module named 'tensorflow.keras.wrappers'" (error from my previous message). But when I look in my file path in tensorflow: "....\site-packages\tensorflow\python\keras" the keras folder doesn't have a module called "wrappers". Or does it know to look for this module elsewhere based on how the error code is being spit out. This is the beginning of the code: import pandas as pdimport scipy.sparseimport osfrom tensorflow import kerasfrom sklearn.model_selection import train_test_split, cross_val_score, KFoldfrom sklearn.preprocessing import StandardScaler, OneHotEncoderfrom sklearn.compose import ColumnTransformerfrom sklearn.metrics import mean_squared_errorfrom tensorflow.keras.models import Sequential, load_modelfrom tensorflow.keras.layers import Dense, Dropoutfrom tensorflow.keras.wrappers.scikit_learn import KerasRegressorimport joblibfrom imblearn.over_sampling import SMOTE

**I also have another keras folder in ".....\site-packages\keras" not sure if this matters.

Here is the pip list (I'm running all this on cmd as well): Package                      Version---------------------------- ---------absl-py                       2.0.0adapt                         0.4.4alembic                      1.13.1altair                         5.1.1amply                        0.1.5astunparse                 1.6.3attrs                         23.1.0blinker                      1.6.2cachetools                 5.3.1certifi                        2023.7.22charset-normalizer     3.2.0click                          8.1.7colorama                   0.4.6colorlog                     6.8.0contourpy                  1.1.0copulas                      0.9.0cvxopt                       1.3.2cycler                        0.11.0docutils                     0.20.1et-xmlfile                   1.1.0flatbuffers                  23.5.26fonttools                    4.40.0fuzzywuzzy                0.18.0gast                          0.5.4gitdb                         4.0.10GitPython                  3.1.37google-auth               2.26.1google-auth-oauthlib   1.0.0google-pasta              0.2.0greenlet                     3.0.3grpcio                        1.60.0h2o                           3.44.0.3h5py                         3.10.0idna                          3.4imbalanced-learn        0.11.0importlib-metadata     6.8.0Jinja2                       3.1.2joblib                       1.3.2json5                        0.9.14jsonschema                   4.19.1jsonschema-specifications    2023.7.1keras                        2.15.0kiwisolver                  1.4.4Levenshtein               0.21.0libclang                     16.0.6llvmlite                     0.41.1Mako                         1.3.0Markdown                  3.5.1markdown-it-py          3.0.0MarkupSafe                2.1.3matplotlib                   3.7.2mdurl                        0.1.2ml-dtypes                    0.2.0numba                        0.58.1numpy                        1.24.3oauthlib                     3.2.2openpyxl                     3.1.2opt-einsum                   3.3.0optuna                       3.5.0package-name                 0.1packaging                    23.1pandas                       2.0.2patsy                        0.5.4Pillow                       9.5.0pip                          23.3.2ply                          3.11protobuf                     4.23.4PuLP                         2.4pyarrow                      13.0.0pyasn1                       0.5.1pyasn1-modules               0.3.0pydeck                       0.8.1b0pydfs-lineup-optimizer       3.6.1Pygments                     2.16.1Pyomo                        6.6.1pyparsing                    3.0.9python-dateutil              2.8.2python-Levenshtein           0.21.0pytz                         2023.3PyYAML                       6.0.1rapidfuzz                    3.0.0referencing                  0.30.2requests                     2.31.0requests-oauthlib            1.3.1rich                         13.5.3rpds-py                      0.10.3rsa                          4.9scikeras                     0.12.0scikit-learn                 1.3.2scipy                        1.11.1seaborn                      0.12.2setuptools                   65.5.0six                          1.16.0smmap                        5.0.1SQLAlchemy                   2.0.25statsmodels                  0.14.0streamlit                    1.27.0tabulate                     0.9.0tenacity                     8.2.3tensorboard                  2.15.1tensorboard-data-server      0.7.2tensorflow                   2.15.0tensorflow-estimator         2.15.0tensorflow-intel             2.15.0tensorflow-io-gcs-filesystem 0.31.0termcolor                    2.4.0threadpoolctl                3.2.0timedelta                    2020.12.3toml                         0.10.2toolz                        0.12.0tornado                      6.3.3tqdm                         4.65.0typing_extensions            4.8.0tzdata                       2023.3tzlocal                      5.0.1urllib3                      2.0.5validators                   0.22.0watchdog                     3.0.0Werkzeug                     3.0.1wheel                        0.42.0wrapt                        1.14.1zipp                         3.17.0

On Friday, January 12, 2024 at 01:14:23 AM PST, antoinedemathelin ***@***.***> wrote:  

If the error is still not resolved, you can watch the installed package versions with: pip list

or conda list

To verify that you have the last version of adapt installed.

PS : don't forget to put "!" before the commands, if you are executing them in a jupyter cell (!pip list)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

juliog23 avatar Jan 12 '24 20:01 juliog23

Hi @juliog23, The module keras.wrappershas been removed in tensorflow 2.15, so its normal you don't find it.

You have the last version of adapt (0.4.4) so the bug doesn't come from the adapt library.

Your issue comes from this import : from tensorflow.keras.wrappers.scikit_learn import KerasRegressor You should replace this line with: from scikeras.wrappers import KerasRegressor

Best,

antoinedemathelin avatar Jan 13 '24 14:01 antoinedemathelin

This last suggestion may have fixed it, no more error on that. Will work through some other modifications I need to do to run the code in its entirety. Thank you for your guidance and patience. On Jan 13, 2024, at 6:10 AM, antoinedemathelin @.***> wrote: Hi @juliog23, The module keras.wrappershas been removed in tensorflow 2.15, so its normal you don't find it. You have the last version of adapt (0.4.4) so the bug doesn't come from the adapt library. Your issue comes from this import : from tensorflow.keras.wrappers.scikit_learn import KerasRegressor You should replace this line with: from scikeras.wrappers import KerasRegressor Best,

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

juliog23 avatar Jan 16 '24 02:01 juliog23

the error for the module not found is resolved I think. However, when using GridSearchCV, the fit function is not working properly.

The code is very simple:

`import numpy as np import pandas as pd from sklearn.model_selection import GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.datasets import make_regression import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from scikeras.wrappers import KerasRegressor

Generate synthetic regression data

X, y = make_regression(n_samples=1000, n_features=5, noise=0.1, random_state=42)

Scale the features

scaler = StandardScaler() X_scaled = scaler.fit_transform(X)

Define the ANN model function

def create_model(units=64, activation='relu', learning_rate=0.001): model = Sequential([ Dense(units, activation=activation, input_shape=(X_scaled.shape[1],)), Dense(units // 2, activation=activation), Dense(1) # Output layer with 1 unit for regression ]) model.compile(loss="mean_squared_error", optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate)) return model

Create KerasRegressor object

keras_model = KerasRegressor(build_fn=create_model, verbose=0)

Define the hyperparameter grid

param_grid = { "units": [64, 128], "activation": ["relu", "tanh"], "learning_rate": [0.001, 0.01] }

Create the GridSearchCV object

grid_search = GridSearchCV(estimator=keras_model, param_grid=param_grid, cv=3, scoring="neg_mean_squared_error")

Fit the model with GridSearchCV

grid_search.fit(X_scaled, y)

Get the best model and parameters

best_model = grid_search.best_estimator_ best_params = grid_search.best_params_

Print the best parameters

print("Best Hyperparameters:", best_params)

Use the best model for prediction

y_pred = best_model.predict(X_scaled)

Evaluate the model (replace with your desired evaluation metric)

from sklearn.metrics import mean_squared_error mse = mean_squared_error(y, y_pred) print("Mean Squared Error:", mse) ` however, the following error is showing:


ValueError Traceback (most recent call last) Cell In[3], line 42 39 grid_search = GridSearchCV(estimator=keras_model, param_grid=param_grid, cv=3, scoring="neg_mean_squared_error") 41 # Fit the model with GridSearchCV ---> 42 grid_search.fit(X_scaled, y) 44 # Get the best model and parameters 45 best_model = grid_search.best_estimator_

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\base.py:1151, in _fit_context..decorator..wrapper(estimator, *args, **kwargs) 1144 estimator._validate_params() 1146 with config_context( 1147 skip_parameter_validation=( 1148 prefer_skip_nested_validation or global_skip_validation 1149 ) 1150 ): -> 1151 return fit_method(estimator, *args, **kwargs)

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\model_selection_search.py:898, in BaseSearchCV.fit(self, X, y, groups, **fit_params) 892 results = self._format_results( 893 all_candidate_params, n_splits, all_out, all_more_results 894 ) 896 return results --> 898 self._run_search(evaluate_candidates) 900 # multimetric is determined here because in the case of a callable 901 # self.scoring the return type is only known after calling 902 first_test_score = all_out[0]["test_scores"]

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\model_selection_search.py:1419, in GridSearchCV._run_search(self, evaluate_candidates) 1417 def _run_search(self, evaluate_candidates): 1418 """Search all candidates in param_grid""" -> 1419 evaluate_candidates(ParameterGrid(self.param_grid))

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\model_selection_search.py:845, in BaseSearchCV.fit..evaluate_candidates(candidate_params, cv, more_results) 837 if self.verbose > 0: 838 print( 839 "Fitting {0} folds for each of {1} candidates," 840 " totalling {2} fits".format( 841 n_splits, n_candidates, n_candidates * n_splits 842 ) 843 ) --> 845 out = parallel( 846 delayed(_fit_and_score)( 847 clone(base_estimator), 848 X, 849 y, 850 train=train, 851 test=test, 852 parameters=parameters, 853 split_progress=(split_idx, n_splits), 854 candidate_progress=(cand_idx, n_candidates), 855 **fit_and_score_kwargs, 856 ) 857 for (cand_idx, parameters), (split_idx, (train, test)) in product( 858 enumerate(candidate_params), enumerate(cv.split(X, y, groups)) 859 ) 860 ) 862 if len(out) < 1: 863 raise ValueError( 864 "No fits were performed. " 865 "Was the CV iterator empty? " 866 "Were there no candidates?" 867 )

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\parallel.py:65, in Parallel.call(self, iterable) 60 config = get_config() 61 iterable_with_config = ( 62 (_with_config(delayed_func, config), args, kwargs) 63 for delayed_func, args, kwargs in iterable 64 ) ---> 65 return super().call(iterable_with_config)

File C:\ProgramData\anaconda3\Lib\site-packages\joblib\parallel.py:1085, in Parallel.call(self, iterable) 1076 try: 1077 # Only set self._iterating to True if at least a batch 1078 # was dispatched. In particular this covers the edge (...) 1082 # was very quick and its callback already dispatched all the 1083 # remaining jobs. 1084 self._iterating = False -> 1085 if self.dispatch_one_batch(iterator): 1086 self._iterating = self._original_iterator is not None 1088 while self.dispatch_one_batch(iterator):

File C:\ProgramData\anaconda3\Lib\site-packages\joblib\parallel.py:901, in Parallel.dispatch_one_batch(self, iterator) 899 return False 900 else: --> 901 self._dispatch(tasks) 902 return True

File C:\ProgramData\anaconda3\Lib\site-packages\joblib\parallel.py:819, in Parallel._dispatch(self, batch) 817 with self._lock: 818 job_idx = len(self._jobs) --> 819 job = self._backend.apply_async(batch, callback=cb) 820 # A job can complete so quickly than its callback is 821 # called before we get here, causing self._jobs to 822 # grow. To ensure correct results ordering, .insert is 823 # used (rather than .append) in the following line 824 self._jobs.insert(job_idx, job)

File C:\ProgramData\anaconda3\Lib\site-packages\joblib_parallel_backends.py:208, in SequentialBackend.apply_async(self, func, callback) 206 def apply_async(self, func, callback=None): 207 """Schedule a func to be run""" --> 208 result = ImmediateResult(func) 209 if callback: 210 callback(result)

File C:\ProgramData\anaconda3\Lib\site-packages\joblib_parallel_backends.py:597, in ImmediateResult.init(self, batch) 594 def init(self, batch): 595 # Don't delay the application, to avoid keeping the input 596 # arguments in memory --> 597 self.results = batch()

File C:\ProgramData\anaconda3\Lib\site-packages\joblib\parallel.py:288, in BatchedCalls.call(self) 284 def call(self): 285 # Set the default nested backend to self._backend but do not set the 286 # change the default number of processes to -1 287 with parallel_backend(self._backend, n_jobs=self._n_jobs): --> 288 return [func(*args, **kwargs) 289 for func, args, kwargs in self.items]

File C:\ProgramData\anaconda3\Lib\site-packages\joblib\parallel.py:288, in (.0) 284 def call(self): 285 # Set the default nested backend to self._backend but do not set the 286 # change the default number of processes to -1 287 with parallel_backend(self._backend, n_jobs=self._n_jobs): --> 288 return [func(*args, **kwargs) 289 for func, args, kwargs in self.items]

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\utils\parallel.py:127, in _FuncWrapper.call(self, *args, **kwargs) 125 config = {} 126 with config_context(**config): --> 127 return self.function(*args, **kwargs)

File C:\ProgramData\anaconda3\Lib\site-packages\sklearn\model_selection_validation.py:720, in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score) 717 for k, v in parameters.items(): 718 cloned_parameters[k] = clone(v, safe=False) --> 720 estimator = estimator.set_params(**cloned_parameters) 722 start_time = time.time() 724 X_train, y_train = _safe_split(estimator, X, y, train)

File ~\AppData\Roaming\Python\Python311\site-packages\scikeras\wrappers.py:1165, in BaseWrapper.set_params(self, params) 1161 super().set_params({param: value}) 1162 except ValueError: 1163 # Give a SciKeras specific user message to aid 1164 # in moving from the Keras wrappers -> 1165 raise ValueError( 1166 f"Invalid parameter {param} for estimator {self.name}." 1167 "\nThis issue can likely be resolved by setting this parameter" 1168 f" in the {self.name} constructor:" 1169 f"\n{self.__name__}({param}={value})" 1170 "\nCheck the list of available parameters with" 1171 " estimator.get_params().keys()" 1172 ) from None 1173 return self

ValueError: Invalid parameter activation for estimator KerasRegressor. This issue can likely be resolved by setting this parameter in the KerasRegressor constructor: KerasRegressor(activation=relu) Check the list of available parameters with estimator.get_params().keys()

Can anyone tell me what the problem is?

Griffunus avatar Mar 21 '24 01:03 Griffunus

Hi @Griffunus, This error is not an Adapt error, it is raised by scikeras. But here is a way to fix it:

Just change this line:

keras_model = KerasRegressor(build_fn=create_model, verbose=0)

For this one:

keras_model = KerasRegressor(build_fn=create_model, verbose=0, activation="relu", learning_rate=0.1, units=24)

The idea is that you need to give random parameters for activation, learning_rate and units, such that KerasRegressor knows that they are "valid" parameters.

Best,

antoinedemathelin avatar Mar 21 '24 08:03 antoinedemathelin