Giter VIP home page Giter VIP logo

dccuchile / wefe Goto Github PK

View Code? Open in Web Editor NEW
170.0 170.0 15.0 42.63 MB

WEFE: The Word Embeddings Fairness Evaluation Framework. WEFE is a framework that standardizes the bias measurement and mitigation in Word Embeddings models. Please feel welcome to open an issue in case you have any questions or a pull request if you want to contribute to the project!

Home Page: https://wefe.readthedocs.io/

License: MIT License

Python 100.00%
bias-detection bias-reduction fairness-ai fairness-ml library nlp nlp-library python3 word-embedding-evaluation word-embedding-fairness word-embeddings

wefe's People

Contributors

dependabot[bot] avatar felipebravom avatar h4c5 avatar harshvr15 avatar kr-ramesh avatar mspl13 avatar mzambrano1 avatar pbadillatorrealba avatar szabgab avatar yupei-du avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wefe's Issues

Questions RNSB metric

Hi,
I would ask you some questions about WEFE in order to calculate bias with RNSB.

I am using two target sets and two attribute sets and running queries in this form: Query([target1_array, target2_array],[positive_attribute_array, negative_attribute_array], [ntarget1, ntarget2], [nattribute1, nattribute2]). For attributes I have 26 or 50 items in total. I am working with 3 different word embedding models.
My questions are:

  1. Why am I obtaining the same results with different runnings?
  2. Why am I obtaining very different results with respect to the model used with the same words? For example, I have in most cases RNSB = 0.20 from one model and RNSB = 0.60 / 0.70 for the others. It seems strange.

I read the source code but I don't understand. Maybe I have a problem with my data.

Thank you very much.

Best regards,
Elisabetta Triolo.

How to reproduce table 1 as in the paper?

Hello WEFE developers,

I am trying to reproduce your paper results. It looks to me the best reference is your WEFE-ranking notebook example. Obviously, the notebook can't run directly, with runtime errors as in this issue. I can fix the syntax problem, but detailed experiment settings are unknown.

Other errors can show up like:

Exception: After dividing the datset using stratified train_test_split, the attribute 0 has 0 training examples.
...
Exception: Error during executing the query: Christianity terms and Islam terms wrt Conservative and Terrorism on the model: word2vec-google-news-300

Can you provide a complete notebook simply for reproducing your paper table 1?

Thanks a lot!

Missing WEAT words

There are two versions of unpleasant_5 words in the original WEAT paper. One of them is used in WEAT 1, and the other is used in WEAT 3. (For example, the former includes "prison," while the latter does not.) However, wefe/datasets/data/WEAT.json has only the former.

I fixed the problem by renaming unpleasant_5 to unpleasant_5a and adding unpleasant_5b (This is the fix branch. https://github.com/kato8966/wefe/tree/fix-weat-words), but this breaks backward compatibility.

Should I proceed and make a PR, or should I try a different approach?

Problem with the library typing

I tried to test the wefe library through colabolatory and it does not work due to compatibility issues with the library typing (product colab works with python 3.7). I got this problem:

ImportError: cannot import name 'Literal' from 'typing' (/usr/lib/python3.7/typing.py)

Perhaps a solution to this problem is to add the following lines in the code:

try:
    from typing import Literal
except ImportError:
    from typing_extensions import Literal

[Question] One-sided p-value for WEAT?

The WEAT is a very useful metric. I was wondering if there was a way to use WEFE to to obtain the one-sided p-value described on page 9 in the following paper on WEAT?

Caliskan, A., Bryson, J. J., & Narayanan, A. (2017). Semantics derived automatically from language corpora contain human-like biases. Science, 356(6334), 183-186.

Thanks,
sbs

RNSB Error

I'm getting the following error.

What may be the cause?

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-22-1b733a31758e> in <module>
     37 
     38     # RNSB
---> 39     result_rnsb = rnsb.run_query(query, wefemodel)
     40     print(result_rnsb)

~/.local/lib/python3.9/site-packages/wefe/metrics/RNSB.py in run_query(self, query, word_embedding, estimator, estimator_params, num_iterations, random_state, print_model_evaluation, lost_vocabulary_threshold, preprocessor_args, secondary_preprocessor_args, warn_not_found_words, *args, **kwargs)
    335 
    336             # train the logit with the train data.
--> 337             trained_classifier, score = self._train_classifier(
    338                 attribute_embeddings_dict=attribute_embeddings,
    339                 random_state=random_state,

~/.local/lib/python3.9/site-packages/wefe/metrics/RNSB.py in _train_classifier(self, attribute_embeddings_dict, estimator, estimator_params, random_state, print_model_evaluation)
     96         # Check the number of train and test examples.
     97         if num_train_positive_examples == 1:
---> 98             raise Exception(
     99                 'After dividing the datset using stratified train_test_split, '
    100                 'the attribute 0 has 0 training examples.')

Exception: After dividing the datset using stratified train_test_split, the attribute 0 has 0 training examples.

RNSB Deprecation Warning

/home/raffaele/.local/lib/python3.9/site-packages/wefe/metrics/RNSB.py:150: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  probabilities = np.array([
/home/raffaele/.local/lib/python3.9/site-packages/wefe/metrics/RNSB.py:156: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  negative_probabilities = np.array(

Availability for embeddings created with transformer models

Hi, I'm using transformer models to create my embeddings but would like to calculate the bias. Is there a way to just feed in the embeddings instead of the embedding models or to introduce a transformer model as embedding model?
Would there be a difference in the calculation of the WEAT score?
Many thanks

Issue with RIPA metric

Hi,

I tried to compute RIPA with a single base pair but I get only nan :

emb_model = WordEmbeddingModel(model)

query = Query(
    target_sets=[["he"], ["she"]],
    attribute_sets=[["doctor", "nurse"]],
    target_sets_names=["male", "female"],
    attribute_sets_names=["profession"],
)

metric = metrics.RIPA()
metric.run_query(gender_query, emb_model, lost_vocabulary_threshold=1.0)

#{'query_name': 'male and female wrt profession',
# 'result': nan,
# 'ripa': nan,
# 'word_values': {'doctor': {'mean': nan, 'std': nan},
#  'nurse': {'mean': nan, 'std': nan}}}

I think this comes from wefe/metrics/RIPA.py line 115 :

# calculating the ripa score for each attribute word with each target pair
for word in range(len(attribute_embeddings_0)):
    ripa_scores[attributes[word]] = []
    for index in range(target_length - 1): # <- Should it be range(target_length) instead ?
        bvec = self._b_vec(
            target_embeddings_0[index], target_embeddings_1[index]
        )
        score = self._ripa_calc(attribute_embeddings_0[word], bvec)
        ripa_scores[attributes[word]].append(score)

Is there a reason why the last element is not used ?

WEFE documentation is inconsistent with the literature

The WEFE documentation says that a target set:

A target word set (denoted by ) corresponds to a set of words intended to denote a particular social group
e.g., gender, social class, age, and ethnicity

while and attribute set:

An attribute word set (denoted by ) is a set of words representing some attitude, characteristic, trait, occupational field, etc.

But in this paper the two concepts are reversed, with target meaning mathematics and arts (occupational fields) and attribute meaning male and female (a particular social group).

Is it a problem of the paper alone?

Are those terms used consistently in the literature?

What happens to the WEAT if I exchange attribute and target sets?

(Is the WEFE documentation right?)

Information about the pre-loaded wordsets (Dataloaders)

Hi Pablo,
Greetings!

1). I was trying to get results on multi-lingual fastText embeddings through the query using WEAT_wordsets() and RND_wordsets(). I am not sure whether I can use the same set of query for multi-lingual embeddings. However, I am getting some result for this.

WEAT_wordsets = load_weat()
RND_wordsets = fetch_eds()

glove_embedding = WordEmbeddings('de')
glove_keyed_vectors = glove_embedding.precomputed_word_embeddings
glove_100 = WordEmbeddingModel(glove_keyed_vectors, 'de')

result = weat.run_query(gender_3, glove_100, lost_vocabulary_threshold=0.3, warn_not_found_words=True, calculate_p_value=True, p_value_iterations=10000)
print(result)

RESULT:
{'query_name': 'Male terms and Female terms wrt Science and Arts', 'result': 0.50751936, 'weat': 0.50751936, 'effect_size': 0.9096829, 'p_value': 0.0034996500349965005}

But I am not sure whether it is correct? Can you please guide whether these wordset queries can be used for multi-lingual embeddings and if not how can I proceed?

2). Can you please share any links where I can find information about the pre loaded wordsets ( load_weat() and fetch_eds() )?

For now, I just know:
fetch_eds(): Fetches the word sets used in the experiments of the work Word Embeddings *Quantify 100 Years Of Gender And Ethnic Stereotypes.
load_weat(): Loads the word sets used in the paper Semantics Derived Automatically From Language Corpora Contain Human-Like Biases.

I couldn't find any links on what data are they extracted from or what sets does it have and further information.

Furthermore,

For example: In this query:
gender_1 = Query(
[RND_wordsets["male_terms"], RND_wordsets["female_terms"]],
[WEAT_wordsets["career"], WEAT_wordsets["family"]],
["Male terms", "Female terms"],
["Career", "Family"],

(Correct me if I'm wrong) You are taking male_terms and female _terms as target set and career and family as attribute sets from these dataloaders. What are the other sets (like male_terms, adjectives_intelligence, female_terms etc.) which are there in this dataloaders? You have described a few in your queries, are there only those or are there few others which can be considered?

Hope to hear from you soon.

Thanks,
Harsh

Metrics not discussed in the paper

Hello,

The WEFE paper presented at IJCAI discuss WEAT, RND, and RNSB, but it does not discuss MAC and ECT.

I wondered whether there were an updated version that discussed those?

Without opening a new issue, the reference for MAC should actually be

Black is to Criminal as Caucasian is to Police: Detecting and Removing Multiclass Bias in Word Embeddings

Instead in the documentation is written as:

Black is to criminals caucasian is to police: Detecting and removing multiclass bias in word embeddings

(criminals is plural and "as" is missing: it0s a minor thing but the reference manager doesn't find it written in this way :))

ImportError: cannot import name 'Literal' from 'typing' in Python 3.7

Hello!

I have an error running WEFE in Python version < 3.8 due to the fact metrics/WEAT.py calls Literal from typing:

"/home/appuser/venv/lib/python3.7/site-packages/wefe/metrics/WEAT.py", line 2, in
from typing import Any, Dict, List, Literal, Set, Tuple, Union
ImportError: cannot import name 'Literal' from 'typing'

This is a known issue for typing, see here.

I would suggest a quick adjustment to WEAT.py, line 2:

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal

Please let me know if feasible.

Thanks,
Tia

word_embedding not found under wefe

ImportError: cannot import name 'word_embedding' from 'wefe' (/Users/xx/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/wefe/init.py)

please help :)

ECT score

Hi,

I'm using the WEFE package to measure bias in different word embeddings. However, When I use ECT the results are not clear to me. AS mentioned in the original paper "Attenuating Bias in Word vectors." they explain that the higher the ECT score the better its s meaning that the higher the score the less biased a word embedding is. I'm wondering if you are taking that in consideration in the Ranking process?

Thanks,
Fatma

Error on import in Google Colab (v0.3.2) with PL

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-ad60f770c107> in <module>()
----> 1 from wefe import Query, WEAT
      2 import gensim.downloader as api
      3 
      4 word2vec_model = model

13 frames
/usr/local/lib/python3.7/dist-packages/distributed/config.py in <module>()
     18 
     19 with open(fn) as f:
---> 20     defaults = yaml.load(f)
     21 
     22 dask.config.update_defaults(defaults)

TypeError: load() missing 1 required positional argument: 'Loader'

WEAT effect size: Different values

I get slightly different values when using WEFE's weat.run_query() than when doing the calculations manually as below:

def getCosine(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

list_avg_diffs= []

avg_t1_a1= 0
avg_t1_a2= 0
avg_t1_diff= 0

for t in t1: #t1= ['brother', 'father', 'uncle', 'grandfather', 'son']
    x= w2v[t]
    for a in a1: #a1= ['science', 'technology', 'physics', 'chemistry', 'Einstein', 'NASA', 'experiment', 'astronomy']
        y= w2v[a]
        avg_t1_a1= avg_t1_a1 + getCosine(x,y)
    avg_t1_a1= avg_t1_a1/len(a1)
    
    for a in a2: #a2= ['poetry', 'art', 'Shakespeare', 'dance', 'literature', 'novel', 'symphony', 'drama']
        y= w2v[a]
        avg_t1_a2= avg_t1_a2 + getCosine(x,y)
    avg_t1_a2= avg_t1_a2/len(a2)

    avg_t1_diff= avg_t1_diff + (avg_t1_a1 - avg_t1_a2)
    list_avg_diffs.append(avg_t1_a1 - avg_t1_a2)

avg_t1_diff= avg_t1_diff/len(t1)

avg_t2_a1= 0
avg_t2_a2= 0
avg_t2_diff= 0

for t in t2: #t2= ['sister', 'mother', 'aunt', 'grandmother', 'daughter']
    x= w2v[t]
    for a in a1: #a1= ['science', 'technology', 'physics', 'chemistry', 'Einstein', 'NASA', 'experiment', 'astronomy']
        y= w2v[a]
        avg_t2_a1= avg_t2_a1 + getCosine(x,y)
    avg_t2_a1= avg_t2_a1/len(a1)
    
    for a in a2: #a2= ['poetry', 'art', 'Shakespeare', 'dance', 'literature', 'novel', 'symphony', 'drama']
        y= w2v[a]
        avg_t2_a2= avg_t2_a2 + getCosine(x,y)
    avg_t2_a2= avg_t2_a2/len(a2)

    avg_t2_diff= avg_t2_diff + (avg_t2_a1 - avg_t2_a2)
    list_avg_diffs.append(avg_t2_a1 - avg_t2_a2)

avg_t2_diff= avg_t2_diff/len(t2)

diff_of_diff= avg_t1_diff - avg_t2_diff
sd= np.std(list_avg_diffs) #use ddof=1 in case you need same sd as in R
weat_effect_size= diff_of_diff/sd

The weat_effect_size above is: 1.6132258039368363. However, that obtained using weat.run_query() is: 1.674.

Not sure why this difference of 0.06 in effect size. Request help.

Import error: cannot import name 'BaseKeyedVectors' from 'gensim.models.keyedvectors'

Thanks for making this very useful python package.

I tried to use it. But get an error with the following line:
from wefe.query import Query


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-02319299b065> in <module>
----> 1 from wefe.query import Query
      2 from wefe.word_embedding_model import WordEmbeddingModel
      3 from wefe.metrics.WEAT import WEAT
      4 #import gensim.downloader as api

/anaconda/envs/py37_default/lib/python3.7/site-packages/wefe/__init__.py in <module>
----> 1 from .word_embedding_model import WordEmbeddingModel
      2 from .query import Query
      3 from .metrics.base_metric import BaseMetric
      4 from .metrics.WEAT import WEAT
      5 from .metrics.RND import RND

/anaconda/envs/py37_default/lib/python3.7/site-packages/wefe/word_embedding_model.py in <module>
----> 1 from gensim.models.keyedvectors import BaseKeyedVectors
      2 
      3 
      4 class WordEmbeddingModel:
      5     """A container for Word Embedding pre-trained models.

ImportError: cannot import name 'BaseKeyedVectors' from 'gensim.models.keyedvectors' (/anaconda/envs/py37_default/lib/python3.7/site-packages/gensim/models/keyedvectors.py)


WEAT p-value is nan

Hello,

the WEAT p-value is always nan.

This happened for two different models I have tried.

Is it normal?

WEAT returns nothing

I use this code to compute a WEAT:

wefemodel = WordEmbeddingModel(wv, model_name)
query = Query(target_sets, attribute_sets, target_sets_names, attribute_sets_names)
result_weat = weat.run_query(query, wefemodel, 
                                     calculate_p_value = True,
                                     return_effect_size = True)

But sometimes the returning result_weat does not include a p_value key:

KeyError: 'p_value'

I think it depends on the model.

For some of my models the returning dictionary do not include this key.

Is it possible it depends on the model?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.