wefe.debias.multiclass_hard_debias.MulticlassHardDebias

class wefe.debias.multiclass_hard_debias.MulticlassHardDebias(pca_args: Dict[str, Any] = {'n_components': 10}, verbose: bool = False, criterion_name: Optional[str] = None)[source]

Generalized version of Hard Debias that enables multiclass debiasing.

Generalized refers to the fact that this method extends Hard Debias in order to support more than two types of social target sets within the definitional set. For example, for the case of religion bias, it supports a debias using words associated with Christianity, Islam and Judaism.

References

[1]: Manzini, T., Chong, L. Y., Black, A. W., & Tsvetkov, Y. (2019, June).
Black is to Criminal as Caucasian is to Police: Detecting and Removing Multiclass
Bias in Word Embeddings.
In Proceedings of the 2019 Conference of the North American Chapter of the
Association for Computational Linguistics: Human Language Technologies,
Volume 1 (Long and Short Papers) (pp. 615-621).

Examples

Note

For more information on the use of mitigation methods, visit Bias Mitigation (Debias) in the User Guide.

The following example shows how to run an ethnicity debias based on Black, Asian and Caucasian groups.

>>> from wefe.datasets import fetch_debias_multiclass, load_weat
>>> from wefe.debias.multiclass_hard_debias import MulticlassHardDebias
>>> from wefe.utils import load_test_model
>>>
>>> model = load_test_model()  # load a reduced version of word2vec
>>>
>>> # obtain the sets of words that will be used in the debias process.
>>> multiclass_debias_wordsets = fetch_debias_multiclass()
>>> weat_wordsets = load_weat()
>>>
>>> ethnicity_definitional_sets = (
...     multiclass_debias_wordsets["ethnicity_definitional_sets"]
... )
>>> ethnicity_equalize_sets = list(
...     multiclass_debias_wordsets["ethnicity_analogy_templates"].values()
... )
>>>
>>> # instance the debias object that will perform the mitigation
>>> mhd = MulticlassHardDebias(verbose=False, criterion_name="ethnicity")
>>> # fits the transformation parameters (bias direction, etc...)
>>> mhd.fit(
...     model=model,
...     definitional_sets=ethnicity_definitional_sets,
...     equalize_sets=ethnicity_equalize_sets,
... )
>>>
>>> # perform the transformation (debiasing) on the embedding model
>>> ethnicity_debiased_model = mhd.transform(model, copy=True)
__init__(pca_args: Dict[str, Any] = {'n_components': 10}, verbose: bool = False, criterion_name: Optional[str] = None) None[source]

Initialize a Multiclass Hard Debias instance.

Parameters:
pca_argsDict[str, Any], optional

Arguments for the PCA that is calculated internally in the identification of the bias subspace, by default {“n_components”: 10}

verbosebool, optional

True will print informative messages about the debiasing process, by default False.

criterion_nameOptional[str], optional

The name of the criterion for which the debias is being executed, e.g. ‘Gender’. This will indicate the name of the model returning transform, by default None

fit(model: WordEmbeddingModel, definitional_sets: List[List[str]], equalize_sets: List[List[str]]) BaseDebias[source]

Compute the bias direction and obtains the equalize embedding pairs.

Parameters:
modelWordEmbeddingModel

The word embedding model to debias.

definitional_setsList[List[str]]

A sequence of string pairs that will be used to define the bias direction. For example, for the case of gender debias, this list could be [[‘woman’, ‘man’], [‘girl’, ‘boy’], [‘she’, ‘he’], [‘mother’, ‘father’], …]. Multiclass hard debias also accepts lists of sets of more than two words, such as religion where sets of words representing Christianity, Islam and Judaism can be used. See the example for more information.

equalize_pairsOptional[List[List[str]]], optional

A list with pairs of strings, which will be equalized. In the case of passing None, the equalization will be done over the word pairs passed in definitional_sets, by default None.

Returns:
BaseDebias

The debias method fitted.

transform(model: WordEmbeddingModel, target: Optional[List[str]] = None, ignore: Optional[List[str]] = None, copy: bool = True) WordEmbeddingModel[source]

Execute Multiclass Hard Debias over the provided model.

Parameters:
modelWordEmbeddingModel

The word embedding model to debias.

targetOptional[List[str]], optional

If a set of words is specified in target, the debias method will be performed only on the word embeddings of this set. If None is provided, the debias will be performed on all words (except those specified in ignore). Note that some words that are not in target may be modified due to the equalization process. by default None.

ignoreOptional[List[str]], optional

If target is None and a set of words is specified in ignore, the debias method will perform the debias in all words except those specified in this set. Note that some words that are in ignore may be modified due to the equalization process. by default None.

copybool, optional

If True, the debias will be performed on a copy of the model. If False, the debias will be applied on the same model delivered, causing its vectors to mutate. WARNING: Setting copy with True requires RAM at least 2x of the size of the model, otherwise the execution of the debias may raise to MemoryError, by default True.

Returns:
WordEmbeddingModel

The debiased embedding model.