wefe.debias.double_hard_debias.DoubleHardDebias
- class wefe.debias.double_hard_debias.DoubleHardDebias(pca_args: dict[str, Any] = {'n_components': 10}, verbose: bool = False, criterion_name: str | None = None, incremental_pca: bool = True, n_words: int = 1000, n_components: int = 4)[source]
Bases:
BaseDebiasDouble Hard Debias Method.
This method allow reducing the bias of an embedding model through geometric operations between embeddings. This method is binary because it only allows 2 classes of the same bias criterion, such as male or female.
The main idea of this method is:
1. Identify a bias subspace through the defining sets. In the case of gender, these could be e.g.
[['woman', 'man'], ['she', 'he'], ...]2. Find the dominant directions of the entire set of vectors by doing a Principal components analysis over it.
3. Get the target words by finding the most biased words, this is the words tha are closest to the representation of each bias group. In case of gender
'he'and'she'.3. Try removing each component resulting of PCA and remove also the bias direction to every vector in the target set and find which component reduces bias the most.
4. Remove the dominant direction that most reduces bias and remove also the bias direction of the vectors in the target set.
Examples
The following example shows how to execute Double Hard Debias method that reduces bias in a word embedding model:
>>> from wefe.debias.double_hard_debias import DoubleHardDebias >>> from wefe.utils import load_test_model >>> from wefe.datasets import fetch_debiaswe >>> >>> # load the model (in this case, the test model included in wefe) >>> model = load_test_model() >>> # load definitional pairs, in this case definitinal pairs included in wefe >>> debiaswe_wordsets = fetch_debiaswe() >>> definitional_pairs = debiaswe_wordsets["definitional_pairs"] >>> >>> # instance and fit the method including bias representation words, >>> # in case of gender definitional_pairs=[['he', 'she'], ...] >>> dhd = DoubleHardDebias().fit( ... model=model, ... definitional_pairs=definitional_pairs, ... bias_representation=['he','she']) >>> # execute the debias, if you don't want a set of words to be debiased >>> # include them in the ignore set. >>> gender_specific = debiaswe_wordsets["gender_specific"] >>> >>> debiased_model = dhd.transform( ... model=model, ignore=gender_specific ... ) Copy argument is True. Transform will attempt to create a copy of the original model. This may fail due to lack of memory. Model copy created successfully.
If you want the debiased to be performed over a specific set of words you can specify them in the target parameter
>>> debiased_model = dhd.transform( ... model=model, target = ['doctor','nurse','programmer','teacher'] ... ) Copy argument is True. Transform will attempt to create a copy of the original model. This may fail due to lack of memory. Model copy created successfully.
References
[1]: Wang, Tianlu, Xi Victoria Lin, Nazneen Fatema Rajani, Bryan McCann, Vicente Or-donez y Caiming Xiong.Double-Hard Debias: Tailoring Word Embeddings for GenderBias Mitigation.CoRR, abs/2005.00965, 2020.https://arxiv.org/abs/2005.00965.- __init__(pca_args: dict[str, Any] = {'n_components': 10}, verbose: bool = False, criterion_name: str | None = None, incremental_pca: bool = True, n_words: int = 1000, n_components: int = 4) None[source]
Initialize a Double Hard Debias instance.
- Parameters:
pca_args (Dict[str, Any], optional) – Arguments for the PCA that is calculated internally in the identification of the bias subspace, by default {“n_components”: 10}
verbose (bool, optional) – True will print informative messages about the debiasing process, by default False.
criterion_name (Optional[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
incremental_pca (bool, optional) – If True, incremental pca will be used over the entire set of vectors. If False, pca will be used over the entire set of vectors. WARNING: Running pca over the entire set of vectors may raise to MemoryError, by default True.
n_words (int, optional) – Number of target words to be used for each bias group. By default 1000
n_components (int, optional) – Numbers of components of PCA to be used to explore the one that reduces bias the most. Usually the best one is close to embedding dimension/100. By default 4.
- fit(model: WordEmbeddingModel, definitional_pairs: list[list[str]], bias_representation: list[str], **fit_params) BaseDebias[source]
Compute the bias direction and get the principal components of the vectors.
- Parameters:
model (WordEmbeddingModel) – The word embedding model to debias.
definitional_pairs (List[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’], …].
bias_representation (List[str]) – Two words that represents each bias group. In case of gender “he” and “she”.
- Returns:
The debias method fitted.
- Return type:
BaseDebias
- fit_transform(model: WordEmbeddingModel, target: list[str] | None = None, ignore: list[str] | None = None, copy: bool = True, **fit_params) WordEmbeddingModel
Convenience method to execute fit and transform in a single call.
- Parameters:
model (WordEmbeddingModel) – A word embedding model object.
target (Optional[List[str]], optional) – If a set of words is specified in target, the debias method will be applied only on the word embeddings of this set, by default None.
ignore (Optional[List[str]], optional) – If target is None and a set of words is specified in ignore, the debias method will debias all words except those specified in ignore, by default None.
copy (bool, 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 at least 2x RAM of the size of the model. Otherwise the execution of the debias may raise MemoryError, by default True.
verbose (bool, optional) – True will print informative messages about the debiasing process, by default True.
- Returns:
The debiased word embedding model.
- Return type:
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequestencapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)
Get parameters for this estimator.
- get_target_words(model: WordEmbeddingModel, target: list[str], ignore: list[str], n_words: int, bias_representation: list[str]) list[str][source]
Obtain target words to be debiased.
This is done by searching the “n_words” most biased words by obtaining the words closest to each word in the bias_representation set (in case of gender “he” and “she”).
- Parameters:
model (WordEmbeddingModel) – The word embedding model to debias.
ignore (List[str]) – Set of words to be ignored from the debias process.
n_words (int) – number of target words to use.
bias_representation (List[str]) – Two words that represents de bias groups.
- Returns:
List of target words for each bias group
- Return type:
List[str]
- set_fit_request(*, bias_representation: bool | None | str = '$UNCHANGED$', definitional_pairs: bool | None | str = '$UNCHANGED$', model: bool | None | str = '$UNCHANGED$') DoubleHardDebias
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.
- Parameters:
bias_representation (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
bias_representationparameter infit.definitional_pairs (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
definitional_pairsparameter infit.model (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
modelparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_params(**params)
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.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_transform_request(*, copy: bool | None | str = '$UNCHANGED$', ignore: bool | None | str = '$UNCHANGED$', model: bool | None | str = '$UNCHANGED$', target: bool | None | str = '$UNCHANGED$') DoubleHardDebias
Configure whether metadata should be requested to be passed to the
transformmethod.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 totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.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:
copy (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
copyparameter intransform.ignore (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
ignoreparameter intransform.model (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
modelparameter intransform.target (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
targetparameter intransform.
- Returns:
self – The updated object.
- Return type:
- short_name = 'DHD'
- transform(model: WordEmbeddingModel, target: list[str] | None = None, ignore: list[str] | None = [], copy: bool = True) WordEmbeddingModel[source]
Execute hard debias over the provided model.
- Parameters:
model (WordEmbeddingModel) – The word embedding model to debias.
ignore (List[str], optional) – If set of words is specified in ignore, the debias method will perform the debias in all target words except those specified in this set, by default [].
copy (bool, 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:
The debiased embedding model.
- Return type: