Skip to content

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

Open

Description

@adhamenaya

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'

Activity

  1. divakarkumarp commented on Nov 26, 2023

    @divakarkumarp

    Another Approach you can try
    pip install tensorflow==2.12.0

  2. olivermueller commented on Dec 11, 2023

    @olivermueller

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

  3. antoinedemathelin commented on Dec 12, 2023

    @antoinedemathelin
    Collaborator

    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,

  4. antoinedemathelin commented on Dec 13, 2023

    @antoinedemathelin
    Collaborator

    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.

  5. juliog23 commented on Jan 10, 2024

    @juliog23

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

  6. antoinedemathelin commented on Jan 10, 2024

    @antoinedemathelin
    Collaborator

    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
  7. juliog23 commented on Jan 11, 2024

    @juliog23
  8. antoinedemathelin commented on Jan 11, 2024

    @antoinedemathelin
    Collaborator

    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
    
  9. juliog23 commented on Jan 11, 2024

    @juliog23
  10. antoinedemathelin commented on Jan 12, 2024

    @antoinedemathelin
    Collaborator

    Hi @juliog23,

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

    pip install scikeras
    
  11. antoinedemathelin commented on Jan 12, 2024

    @antoinedemathelin
    Collaborator

    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)

  12. juliog23 commented on Jan 12, 2024

    @juliog23
  13. antoinedemathelin commented on Jan 13, 2024

    @antoinedemathelin
    Collaborator

    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,

  14. juliog23 commented on Jan 16, 2024

    @juliog23
  15. Griffunus commented on Mar 21, 2024

    @Griffunus

    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?

  16. antoinedemathelin commented on Mar 21, 2024

    @antoinedemathelin
    Collaborator

    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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions