Trusted-AI / AIF360

A comprehensive set of fairness metrics for datasets and machine learning models, explanations for these metrics, and algorithms to mitigate bias in datasets and models.
https://aif360.res.ibm.com/
Apache License 2.0
2.46k stars 840 forks source link

error when using (cloud)pickle to dump a fitted sklearn AdversarialDebiasing model #462

Open evilantal opened 1 year ago

evilantal commented 1 year ago

Also posted as a reply under a similar issue, but as this concerns the sklearn implementation (of AdversarialDebiasing) and cloudpickle combo specifically, I thought to also open a separate issue.

I tried this using the sklearn version. But when I try to pickle.dump() using cloudpickle, I get an error saying "cannot pickle 'Graph' object". I'm not even sure why a fitted AdversarialDebiasing model would be a 'Graph' object.

cloudpickle version = 1.5.0 aif360 version = 0.5.0

EDIT: Same error when using regular pickle

In the example below X is a DataFrame that includes 'protfeat' set as index and y is a numpy array of 1/0 labels.

from aif360.sklearn.inprocessing import AdversarialDebiasing
import tensorflow.compat.v1 as tf
from aif360.datasets import StandardDataset
import pandas as pd
from cloudpickle import pickle

tf.disable_eager_execution()
sess = tf.Session()

model = AdversarialDebiasing(
    prot_attr='protfeat',
    scope_name='debiased_model',
    num_epochs=50,
    batch_size=128,
    debias=True, 
    random_state=1337,
    classifier_num_hidden_units=200
)

model.fit(X, y)

with open(os.path.join(folder.get_path(), 'test_pickle.pkl'), 'w') as f:
     pickle.dump(model, f)