Skip to content

Commit

Permalink
Merge pull request Trusted-AI#230 from Trusted-AI/tf2
Browse files Browse the repository at this point in the history
Allow for TensorFlow 2
  • Loading branch information
nrkarthikeyan committed Mar 4, 2021
2 parents 19c7ffc + 38cdd47 commit 0416730
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.6, 3.7]
python-version: [3.6, 3.7, 3.8]

env:
UCI_DB: "https://archive.ics.uci.edu/ml/machine-learning-databases"
PROPUBLICA_GH: "https://raw.githubusercontent.com/propublica/compas-analysis/bafff5da3f2e45eca6c2d5055faad269defd135a"
Expand All @@ -37,15 +37,15 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install numpy==1.19.5
pip install -e '.[all]'
pip install flake8
pip list
- name: Download datasets
run: |
wget ${UCI_DB}/adult/adult.data -P aif360/data/raw/adult/
Expand All @@ -54,29 +54,29 @@ jobs:
wget ${UCI_DB}/statlog/german/german.data -P aif360/data/raw/german/
wget ${UCI_DB}/statlog/german/german.doc -P aif360/data/raw/german/
wget ${PROPUBLICA_GH}/compas-scores-two-years.csv -P aif360/data/raw/compas/
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: pytest tests

build-r:
runs-on: ubuntu-16.04

strategy:
fail-fast: false
matrix:
python-version: [3.6, 3.7]
python-version: [3.6, 3.7, 3.8]

steps:
- name: Check out repo
uses: actions/checkout@v2

- name: Set up R
uses: r-lib/actions/setup-r@v1

Expand All @@ -88,11 +88,11 @@ jobs:
- name: Install R dependencies
run: install.packages(c("reticulate", "rstudioapi", "testthat"))
shell: Rscript {0}

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install '.[all]'
- name: Install R package
run: R CMD INSTALL aif360/aif360-r
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ Supported Python Configurations:

| OS | Python version |
| ------- | -------------- |
| macOS | 3.6, 3.7 |
| Ubuntu | 3.6, 3.7 |
| Windows | 3.6, 3.7 |
| macOS | 3.6, 3.7, 3.8 |
| Ubuntu | 3.6, 3.7, 3.8 |
| Windows | 3.6, 3.7, 3.8 |

### (Optional) Create a virtual environment

Expand Down Expand Up @@ -176,10 +176,10 @@ issue here and try the solutions.

#### TensorFlow

See the [Install TensorFlow with pip](https://www.tensorflow.org/install/pip?lang=python3#older-versions-of-tensorflow)
See the [Install TensorFlow with pip](https://www.tensorflow.org/install/pip)
page for detailed instructions.

Note: we require `'tensorflow >= 1.13.1, < 2'`.
Note: we require `'tensorflow >= 1.13.1'`.

Once tensorflow is installed, try re-running:

Expand Down
13 changes: 9 additions & 4 deletions aif360/algorithms/inprocessing/adversarial_debiasing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

try:
import tensorflow as tf
import tensorflow.compat.v1 as tf
except ImportError as error:
from logging import warning
warning("{}: AdversarialDebiasing will be unavailable. To install, run:\n"
Expand Down Expand Up @@ -82,14 +82,14 @@ def _classifier_model(self, features, features_dim, keep_prob):
"""
with tf.variable_scope("classifier_model"):
W1 = tf.get_variable('W1', [features_dim, self.classifier_num_hidden_units],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed1))
initializer=tf.initializers.glorot_uniform(seed=self.seed1))
b1 = tf.Variable(tf.zeros(shape=[self.classifier_num_hidden_units]), name='b1')

h1 = tf.nn.relu(tf.matmul(features, W1) + b1)
h1 = tf.nn.dropout(h1, keep_prob=keep_prob, seed=self.seed2)

W2 = tf.get_variable('W2', [self.classifier_num_hidden_units, 1],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed3))
initializer=tf.initializers.glorot_uniform(seed=self.seed3))
b2 = tf.Variable(tf.zeros(shape=[1]), name='b2')

pred_logit = tf.matmul(h1, W2) + b2
Expand All @@ -105,7 +105,7 @@ def _adversary_model(self, pred_logits, true_labels):
s = tf.sigmoid((1 + tf.abs(c)) * pred_logits)

W2 = tf.get_variable('W2', [3, 1],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed4))
initializer=tf.initializers.glorot_uniform(seed=self.seed4))
b2 = tf.Variable(tf.zeros(shape=[1]), name='b2')

pred_protected_attribute_logit = tf.matmul(tf.concat([s, s * true_labels, s * (1.0 - true_labels)], axis=1), W2) + b2
Expand All @@ -123,6 +123,11 @@ def fit(self, dataset):
Returns:
AdversarialDebiasing: Returns self.
"""
if tf.executing_eagerly():
raise RuntimeError("AdversarialDebiasing does not work in eager "
"execution mode. To fix, add `tf.disable_eager_execution()`"
" to the top of the calling script.")

if self.seed is not None:
np.random.seed(self.seed)
ii32 = np.iinfo(np.int32)
Expand Down
7 changes: 6 additions & 1 deletion aif360/sklearn/inprocessing/adversarial_debiasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import check_random_state
from sklearn.utils.validation import check_is_fitted
import tensorflow as tf
import tensorflow.compat.v1 as tf

from aif360.sklearn.utils import check_inputs, check_groups

Expand Down Expand Up @@ -91,6 +91,11 @@ def fit(self, X, y):
Returns:
self
"""
if tf.executing_eagerly():
raise RuntimeError("AdversarialDebiasing does not work in eager "
"execution mode. To fix, add `tf.disable_eager_execution()`"
" to the top of the calling script.")

X, y, _ = check_inputs(X, y)
rng = check_random_state(self.random_state)
ii32 = np.iinfo(np.int32)
Expand Down
3 changes: 2 additions & 1 deletion examples/demo_adversarial_debiasing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"from IPython.display import Markdown, display\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import tensorflow as tf"
"import tensorflow.compat.v1 as tf\n",
"tf.disable_eager_execution()"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion examples/sklearn/demo_new_features.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import tensorflow as tf\n",
"import tensorflow.compat.v1 as tf\n",
"tf.disable_eager_execution()\n",
"tf.logging.set_verbosity(tf.logging.ERROR)\n",
"\n",
"from sklearn.compose import make_column_transformer\n",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

extras = {
'OptimPreproc': ['cvxpy>=1.0'],
'AdversarialDebiasing': ['tensorflow>=1.13.1,<2'],
'AdversarialDebiasing': ['tensorflow>=1.13.1'],
'DisparateImpactRemover': ['BlackBoxAuditing'],
'LIME': ['lime'],
'ART': ['adversarial-robustness-toolbox>=1.0.0'],
Expand Down
3 changes: 2 additions & 1 deletion tests/sklearn/test_adversarial_debiasing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

from aif360.datasets import AdultDataset
from aif360.sklearn.datasets import fetch_adult
Expand Down

0 comments on commit 0416730

Please sign in to comment.