wefe.ECT

class wefe.ECT[source]

Embedding Coherence Test [1].

The metric was originally proposed in [1] and implemented in [2]. Values closer to 1 are better as they represent less bias.

The general steps of the test, as defined in [1], are as follows:

  1. Embed all given target and attribute words with the given embedding model.

  2. Calculate mean vectors for the two sets of target word vectors.

  3. Measure the cosine similarity of the mean target vectors to all of the given attribute words.

  4. Calculate the Spearman r correlation between the resulting two lists of similarities.

  5. Return the correlation value as score of the metric (in the range of -1 to 1); higher is better.

References

[1]: Dev, S., & Phillips, J. (2019, April). Attenuating Bias in Word vectors.
__init__(*args, **kwargs)
run_query(query: wefe.query.Query, model: wefe.word_embedding_model.WordEmbeddingModel, lost_vocabulary_threshold: float = 0.2, preprocessors: List[Dict[str, Union[str, bool, Callable]]] = [{}], strategy: str = 'first', normalize: bool = False, warn_not_found_words: bool = False, *args: Any, **kwargs: Any) Dict[str, Any][source]

Run ECT with the given query with the given parameters.

Parameters
queryQuery

A Query object that contains the target and attribute word sets to be tested.

modelWordEmbeddingModel

A word embedding model.

preprocessorsList[Dict[str, Union[str, bool, Callable]]]

A list with preprocessor options.

A preprocessor is a dictionary that specifies what processing(s) are performed on each word before it is looked up in the model vocabulary. For example, the preprocessor {'lowecase': True, 'strip_accents': True} allows you to lowercase and remove the accent from each word before searching for them in the model vocabulary. Note that an empty dictionary {} indicates that no preprocessing is done.

The possible options for a preprocessor are:

  • lowercase: bool. Indicates that the words are transformed to lowercase.

  • uppercase: bool. Indicates that the words are transformed to uppercase.

  • titlecase: bool. Indicates that the words are transformed to titlecase.

  • strip_accents: bool, {'ascii', 'unicode'}: Specifies that the accents of the words are eliminated. The stripping type can be specified. True uses ‘unicode’ by default.

  • preprocessor: Callable. It receives a function that operates on each word. In the case of specifying a function, it overrides the default preprocessor (i.e., the previous options stop working).

A list of preprocessor options allows you to search for several variants of the words into the model. For example, the preprocessors [{}, {"lowercase": True, "strip_accents": True}] {} allows first to search for the original words in the vocabulary of the model. In case some of them are not found, {"lowercase": True, "strip_accents": True} is executed on these words and then they are searched in the model vocabulary.

strategystr, optional

The strategy indicates how it will use the preprocessed words: ‘first’ will include only the first transformed word found. all’ will include all transformed words found, by default “first”.

normalizebool, optional

True indicates that embeddings will be normalized, by default False

warn_not_found_wordsbool, optional

Specifies if the function will warn (in the logger) the words that were not found in the model’s vocabulary, by default False.

Returns
Dict[str, Any]

A dictionary with the query name and the result of the query.

Examples

>>> from wefe.metrics import ECT
>>> from wefe.query import Query
>>> from wefe.utils import load_test_model
>>>
>>> # define the query
>>> query = Query(
...     target_sets=[
...         ["female", "woman", "girl", "sister", "she", "her", "hers",
...          "daughter"],
...         ["male", "man", "boy", "brother", "he", "him", "his", "son"],
...     ],
...     attribute_sets=[
...         [
...             "home", "parents", "children", "family", "cousins", "marriage",
...             "wedding", "relatives",
...         ],
...     ],
...     target_sets_names=["Female terms", "Male Terms"],
...     attribute_sets_names=["Family"],
... )
>>> # load the model (in this case, the test model included in wefe)
>>> model = load_test_model()
>>>
>>> # instance the metric and run the query
>>> ECT().run_query(query, model) 
{'query_name': 'Female terms and Male Terms wrt Family',
'result': 0.6190476190476191,
'ect': 0.6190476190476191}
>>> # if you want the embeddings to be normalized before calculating the metrics
>>> # use the normalize parameter as True before executing the query.
>>> ECT().run_query(query, model, normalize=True) 
{'query_name': 'Female terms and Male Terms wrt Family',
'result': 0.7619047619047621,
'ect': 0.7619047619047621}