fairlearn.adversarial.AdversarialFairnessClassifier#
- class fairlearn.adversarial.AdversarialFairnessClassifier(*, backend='auto', predictor_model=None, adversary_model=None, predictor_optimizer='Adam', adversary_optimizer='Adam', constraints='demographic_parity', learning_rate=0.001, alpha=1.0, epochs=1, batch_size=32, shuffle=False, progress_updates=None, skip_validation=False, callbacks=None, cuda=None, warm_start=False, random_state=None)[source]#
Train PyTorch or TensorFlow classifiers while mitigating unfairness.
This estimator implements the supervised learning method proposed in “Mitigating Unwanted Biases with Adversarial Learning”. [1] The training algorithm takes as input two neural network models, a predictor model and an adversarial model, defined either as a PyTorch module or TensorFlow2 model. The API follows conventions of sklearn estimators.
The predictor model takes the features
Xas input and seeks to predicty. For binary classification, the predictor model should return a single real-valued score, which is transformed into a probability of the positive class via the logistic function (aka sigmoid), similarly to logistic regression. For multi-class classification, the predictor model should return a vector of real-valued scores, which are transformed into class probabilities via the softmax function, similarly to multinomial logistic regression. The training loss is the negative log likelihood (aka log loss, logistic loss, cross-entropy loss).The adversarial model for demographic parity takes scores produced by the predictor model as input, and seeks to predict
sensitive_features. Depending on the type of the provided sensitive features, the model should produce a scalar or vector output. Three types of sensitive features are supported: (1) a single binary feature; (2) a single discrete feature; (3) one or multiple real-valued features. For a single binary sensitive feature and a single discrete feature, the network outputs are transformed by the logistic function and the softmax function, respectively, and the loss is the negative log likelihood. For one or multiple real-valued features, the network output is left as is, and the loss is a square loss.The adversarial model for equalized odds additionally takes
yas input. For multi-class classification,yis transformed using one-hot encoding.- Parameters:
- backend
str, BackendEngine, default = ‘auto’ The backend to use. Must be one of
'torch','tensorflow', or'auto'which indicates PyTorch, TensorFlow, or to automatically infer the backend from thepredictor_model. You can also pass in a BackendEngine class.- predictor_model
list, torch.nn.Module, keras.Model The predictor model to train. Instead of a neural network model, it is possible to pass a list \([k_1, k_2, \dots]\), where each \(k_i\) either indicates the number of nodes (if \(k_i\) is an integer) or an activation function (if \(k_i\) is a string) or a layer or activation function instance directly (if \(k_i\) is a callable). The default parameter is
[], which indicates a neural network without any hidden layers. However, the number of nodes in the input and output layer are automatically inferred from data, and the final activation function (such as softmax for categorical predictors) are inferred from data. Ifbackendis specified, you cannot pass a model that uses a different backend.- adversary_model
list, torch.nn.Module, keras.Model The adversary model to train. Defined similarly as
predictor_model. Must be the same type as thepredictor_model.- predictor_optimizer
str, torch.optim, keras.optimizers,callable(), default = ‘Adam’ The optimizer class to use. If a string is passed instead, this must be either ‘SGD’ or ‘Adam’. A corresponding SGD or Adam optimizer is initialized with the given predictor model and learning rate. If an instance of a subclass of torch.optim.Optimizer or keras.optimizers.Optimizer is passed, this is used directly. If a callable
fnis passed, we call this callable and pass our model, and set the result of this call as the optimizer, so:predictor_optimizer=fn(predictor_model).- adversary_optimizer
str, torch.optim, keras.optimizers,callable(), default = ‘Adam’ The optimizer class to use. Defined similarly as
predictor_optimizer.- constraints
str, default = ‘demographic_parity’ The fairness constraint. Must be either ‘demographic_parity’ or ‘equalized_odds’.
- learning_rate
float, default = 0.001 A small number greater than zero to set as a learning rate.
- alpha
float, default = 1.0 A small number \(\alpha\) as specified in the paper. It is the factor that balances the training towards predicting
y(choose \(\alpha\) closer to zero) or enforcing fairness constraint (choose larger \(\alpha\)).- epochs
int, default = 1 Number of epochs to train for.
- batch_size
int, default = 32 Batch size. For no batching, set this to -1.
- shufflebool, default =
False When true, shuffle the data before every epoch (including the first).
- progress_updatesnumber, optional, default =
None If a number \(t\) is provided, we print an update about the training loop after processing a batch and \(t\) seconds have passed since the previous update.
- skip_validationbool, default =
False Skip the validation of the data. Useful because validate_input is a costly operation, and we may instead pass all data to validate_input at an earlier stage. Note that not only checking
Xis skipped, but also no transform is applied toyandsensitive_features.- callbacks
callable() Callback function, called after every batch. For instance useable when wanting to validate. A list of callback functions can also be provided. Each callback function is called as:
callback( self, step=self.step_, X=X, y=y, z=sensitive_features, pos_label=1 )
which is passed the
selfobject, the step number, the inputsX, the targetsy, the sensitive featuresz, and the positive label. The callback may return a Boolean value. If the returned value is True, the optimization algorithm terminates. This can be used to implement early stopping.- cuda
str, default =None A string to indicate which device to use when training. For instance, set
cuda='cuda:0'to train on the first GPU. Only for PyTorch backend.- warm_startbool, default =
False Normally, when set to False, a call to
fit()triggers reinitialization, which discards the models and initializes them again. Setting to True triggers reuse of these models. Note: if pre-initialized models are passed, the models (and their parameters) are never discarded.- random_state
int,RandomState, default =None Controls the randomized aspects of this algorithm, such as shuffling. Useful to get reproducible output across multiple function calls.
- backend
- fit(X, y, *, sensitive_features=None)[source]#
Fit the model based on the given training data and sensitive features.
Currently, for discrete y and sensitive_features ALL classes need to be passed in the first call to fit!
- Parameters:
- X
numpy.ndarray Two-dimensional numpy array containing training data
- y
array Array-like containing training targets
- sensitive_features
array Array-like containing the sensitive features of the training data.
- X
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- partial_fit(X, y, *, classes=None, sensitive_features=None)[source]#
Perform one training step on given samples and update model.
This method allows for incremental fitting on batches of data.
- Parameters:
- Xarray_like of shape (n_samples, n_features)
The training input samples.
- yarray_like of shape (n_samples,)
The target values.
- classesarray_like of shape (n_classes,), default=None
List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls.
- sensitive_featuresarray_like of shape (n_samples,), default=None
The sensitive features for each sample. If None, a vector of zeros will be used.
- Returns:
- selfobject
Returns self.
- predict(X)[source]#
Compute predictions for given test data.
Predictions are discrete for classifiers, making use of the predictor_function.
- Parameters:
- X
numpy.ndarray Two-dimensional numpy array containing test data
- X
- Returns:
- y_pred
array array-like containing the model’s predictions fed through the (discrete)
predictor_function
- y_pred
- score(X, y, sample_weight=None)[source]#
Return accuracy on provided data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
- Xarray_like of shape (n_samples, n_features)
Test samples.
- yarray_like of shape (n_samples,) or (n_samples, n_outputs)
True labels for X.
- sample_weightarray_like of shape (n_samples,), default=None
Sample weights.
- Returns:
- score
float Mean accuracy of
self.predict(X)w.r.t. y.
- score
- set_fit_request(*, sensitive_features: bool | None | str = '$UNCHANGED$') AdversarialFairnessClassifier[source]#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- set_params(**params)[source]#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **params
dict Estimator parameters.
- **params
- Returns:
- selfestimator instance
Estimator instance.
- set_partial_fit_request(*, classes: bool | None | str = '$UNCHANGED$', sensitive_features: bool | None | str = '$UNCHANGED$') AdversarialFairnessClassifier[source]#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') AdversarialFairnessClassifier[source]#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Gallery examples#
Basics & Model Specification of AdversarialFairness