Skip to content

Commit

Permalink
Merge pull request #119 from antoinedemathelin/master
Browse files Browse the repository at this point in the history
fix: Fix compatibility issue with latest Tensorflow version
  • Loading branch information
antoinedemathelin authored Dec 12, 2023
2 parents 077349e + 053f81c commit 68bb557
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ jobs:
build:
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: ['3.8', '3.9', '3.10', '3.11']
os: [ubuntu-latest, windows-latest, macos-latest]
exclude:
- os: windows-latest
python-version: 3.9
- os: ubuntu-latest
python-version: 3.6
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ docs_build/
docs/html/
docs/doctrees/
adapt/datasets.py
datasets/
datasets/
debug.ipynb
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ADAPT

[![PyPI version](https://badge.fury.io/py/adapt.svg)](https://pypi.org/project/adapt)
[![Build Status](https://github.com/adapt-python/adapt/workflows/build/badge.svg)](https://github.com/adapt-python/adapt/actions)
[![Build Status](https://github.com/adapt-python/adapt/actions/workflows/run-test.yml/badge.svg)](https://github.com/adapt-python/adapt/actions)
[![Python Version](https://img.shields.io/badge/python-3.6%20|%203.7%20|%203.8|%203.9-blue)](https://img.shields.io/badge/python-3.6%20|%203.7%20|%203.8|%203.9-blue)
[![Codecov Status](https://codecov.io/gh/adapt-python/adapt/branch/master/graph/badge.svg?token=IWQXMYGY2Q)](https://codecov.io/gh/adapt-python/adapt)

Expand Down Expand Up @@ -72,11 +72,12 @@ The following dependencies are required and will be installed with the library:
- `tensorflow` (>= 2.0)
- `scikit-learn`
- `cvxopt`
- `scikeras`

If for some reason, these packages failed to install, you can do it manually with:

```
pip install numpy scipy tensorflow scikit-learn cvxopt
pip install numpy scipy tensorflow scikit-learn cvxopt scikeras
```

Finally import the module in your python scripts with:
Expand All @@ -87,6 +88,15 @@ import adapt

A simple example of usage is given in the [Quick-Start](#Quick-Start) below.

### Stable environments [Updated Dec 2023]

ADAPT sometimes encounters incompatibility issue after a new Tensorflow release. In this case, you can use the following environment, which has passed all tests. ADAPT should work well on it:
- OS: `ubuntu-22.04, windows-2022, macos-12`
- Python versions: `3.8 to 3.11`

```
pip install numpy==1.26.2 scipy==1.11.4 tensorflow==2.15.0 scikit-learn==1.3.2 cvxopt==1.3.2 scikeras==0.12.0
```

## ADAPT Guideline

Expand Down
7 changes: 5 additions & 2 deletions adapt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
from sklearn.metrics.pairwise import KERNEL_PARAMS
from sklearn.exceptions import NotFittedError
from tensorflow.keras import Model
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
try:
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
except:
from scikeras.wrappers import KerasClassifier, KerasRegressor
try:
from tensorflow.keras.optimizers.legacy import RMSprop
except:
Expand Down Expand Up @@ -1623,7 +1626,7 @@ def _get_length_dataset(self, dataset, domain="src"):


def _check_for_batch(self, dataset):
if dataset.__class__.__name__ == "BatchDataset":
if "BatchDataset" in dataset.__class__.__name__:
return True
if hasattr(dataset, "_input_dataset"):
return self._check_for_batch(dataset._input_dataset)
Expand Down
2 changes: 1 addition & 1 deletion adapt/instance_based/_iwc.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def fit_weights(self, Xs, Xt, warm_start=False, **kwargs):
else:
y_pred = self.classifier_.predict(Xs).ravel()

self.weights_ = 1. / (y_pred + EPS) - 1.
self.weights_ = 1. / np.clip(y_pred, EPS, 1.) - 1.

return self.weights_

Expand Down
8 changes: 4 additions & 4 deletions adapt/instance_based/_tradaboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def fit(self, X, y, Xt=None, yt=None,
self : returns an instance of self
"""
set_random_seed(self.random_state)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

Xs, ys = check_arrays(X, y, accept_sparse=True)
Xt, yt = self._get_target_data(Xt, yt)
Expand Down Expand Up @@ -757,9 +757,9 @@ class TwoStageTrAdaBoostR2(TrAdaBoostR2):
--------
>>> from sklearn.linear_model import Ridge
>>> from adapt.utils import make_regression_da
>>> from adapt.instance_based import TrAdaBoostR2
>>> from adapt.instance_based import TwoStageTrAdaBoostR2
>>> Xs, ys, Xt, yt = make_regression_da()
>>> model = TrAdaBoostR2(Ridge(), n_estimators=10, Xt=Xt[:10], yt=yt[:10], random_state=0)
>>> model = TwoStageTrAdaBoostR2(Ridge(), n_estimators=10, Xt=Xt[:10], yt=yt[:10], random_state=0)
>>> model.fit(Xs, ys)
Iteration 0 - Cross-validation score: 0.2956 (0.0905)
Iteration 1 - Cross-validation score: 0.2956 (0.0905)
Expand Down Expand Up @@ -814,7 +814,7 @@ def fit(self, X, y, Xt=None, yt=None,
sample_weight_tgt=None,
**fit_params):
set_random_seed(self.random_state)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

Xs, ys = check_arrays(X, y, accept_sparse=True)
Xt, yt = self._get_target_data(Xt, yt)
Expand Down
5 changes: 4 additions & 1 deletion adapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from sklearn.utils import check_array
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin, clone
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
try:
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
except:
from scikeras.wrappers import KerasClassifier, KerasRegressor
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras import Sequential, Model
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
numpy
scipy
tensorflow < 2.12
tensorflow
scikit-learn
cvxopt <= 1.3.0
cvxopt
scikeras
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
author_email='[email protected]',
license='BSD-2',
packages=find_packages(exclude=["tests"]),
install_requires=["numpy>=1.16", "scipy>=1.0", "tensorflow<2.12", "scikit-learn>=0.2", "cvxopt<=1.3.0"],
install_requires=["numpy", "scipy", "tensorflow", "scikit-learn", "cvxopt", "scikeras"],
zip_safe=False,
long_description=long_description,
long_description_content_type='text/markdown'
Expand Down
11 changes: 9 additions & 2 deletions src_docs/_templates/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,35 @@
'add_weight',
'apply',
'build',
'build_from_config',
'call',
'compile_from_config',
'compute_mask',
'compute_output_shape',
'compute_output_signature',
'count_params',
'evaluate',
'evaluate_generator',
'export',
'finalize_state',
'fit_generator',
'from_config',
'get_build_config',
'get_compile_config',
'get_config',
'get_input_at',
'get_input_mask_at',
'get_input_shape_at',
'get_layer',
'get_losses_for',
'get_metrics_result',
'get_output_at',
'get_output_mask_at',
'get_output_shape_at',
'get_updates_for',
'get_weights',
'load_weights',
'get_weight_paths',
'load_own_variables',
'make_predict_function',
'make_test_function',
'make_train_function',
Expand All @@ -53,7 +60,7 @@
'reset_states',
'save',
'save_spec',
'save_weights',
'save_own_variables',
'set_weights',
'summary',
'test_on_batch',
Expand Down

0 comments on commit 68bb557

Please sign in to comment.