{"id":126,"date":"2020-08-05T14:40:01","date_gmt":"2020-08-05T14:40:01","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/08\/05\/word-embedding-fairness-evaluation\/"},"modified":"2020-08-05T14:40:01","modified_gmt":"2020-08-05T14:40:01","slug":"word-embedding-fairness-evaluation","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/08\/05\/word-embedding-fairness-evaluation\/","title":{"rendered":"Word Embedding Fairness Evaluation"},"content":{"rendered":"<div id=\"post-\">\n<p><strong>By <a href=\"https:\/\/github.com\/pabloBad\" target=\"_blank\" rel=\"noopener noreferrer\">Pablo Badilla<\/a>\u00a0and <a href=\"https:\/\/felipebravom.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Felipe Bravo-Marquez<\/a>.<\/strong><\/p>\n<p>\u00a0<\/p>\n<h3>About WEFE<\/h3>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.kdnuggets.com\/2019\/02\/word-embeddings-nlp-applications.html\" target=\"_blank\" rel=\"noopener noreferrer\">Word embeddings<\/a>\u00a0are dense vector representations of words trained from document corpora. They have become a core component of natural language processing (NLP) downstream systems because of their ability to efficiently capture semantic and syntactic relationships between words. A widely reported shortcoming of word embeddings is that they are prone to inherit stereotypical social biases exhibited in the corpora on which they are trained.<\/p>\n<p>The problem of how to quantify the mentioned biases is currently an active area of research, and several different\u00a0<em>fairness metrics<\/em>\u00a0have been proposed in the literature in the past few years.<\/p>\n<p>Although all metrics have a similar objective, the relationship between them is by no means clear. Two issues that prevent a clean comparison is that they operate with different inputs (pairs of words, sets of words, multiple sets of words, and so on) and that their outputs are incompatible with each other (reals, positive numbers,\u00a0\u00a0range, etc.). This leads to a lack of consistency between them, which causes several problems when trying to compare and validate their results.<\/p>\n<p>We propose the\u00a0<a href=\"https:\/\/felipebravom.com\/publications\/ijcai2020.pdf\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Word Embedding Fairness Evaluation<\/strong>\u00a0(WEFE)<\/a>\u00a0as a framework for measuring fairness in word embeddings, and we released its implementation as an\u00a0<a href=\"https:\/\/wefe.readthedocs.io\" target=\"_blank\" rel=\"noopener noreferrer\">open-source library<\/a>.<\/p>\n<p>\u00a0<\/p>\n<h3>Framework<\/h3>\n<p>\u00a0<\/p>\n<p>We propose an abstract view of a\u00a0<em>fairness metric<\/em>\u00a0as a function that receives\u00a0<em>queries<\/em>\u00a0as input, with each query formed by a\u00a0<em>target<\/em>\u00a0and\u00a0<em>attribute<\/em>\u00a0words. The\u00a0<em>target words<\/em>\u00a0describe the social groups in which fairness is intended to be measured (e.g., women, white people, Muslims), and the\u00a0<em>attribute words<\/em>\u00a0describe traits or attitudes by which a bias towards one of the social groups may be exhibited (e.g., pleasant vs. unpleasant terms). For more details on the framework, you can read our recently accepted paper IJCAI paper [1].<\/p>\n<p>WEFE implements the following metrics:<\/p>\n<p>\u00a0<\/p>\n<h3>Standard usage pattern of WEFE<\/h3>\n<p>\u00a0<\/p>\n<p>The standard process for measuring bias using WEFE is shown in the following diagram:<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/wefe.readthedocs.io\/en\/latest\/_images\/diagram_1.png\" width=\"90%\"><\/p>\n<p>\u00a0<\/p>\n<h3>Installation<\/h3>\n<p>\u00a0<\/p>\n<p>There are two different ways to install WEFE:<\/p>\n<p>or<\/p>\n<div>\n<pre>conda install -c pbadilla wefe\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h3>Running a Query<\/h3>\n<p>\u00a0<\/p>\n<p>In the following code, we measure the\u00a0<em>gender bias<\/em>\u00a0of\u00a0<em>word2vec<\/em>\u00a0using:<\/p>\n<p>Given a word embedding <img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7Bw%7D\" alt=\"w\">, WEAT defines first the measure\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7Bd(w,A_1,A_2)=%5Cbig(%5Coperatorname%7Bmean%7D_%7Bx%20%5Cin%20A_1%7D%5Ccos(w,%20x)%5Cbig)-%5Cbig(%5Coperatorname%7Bmean%7D_%7Bx%20%5Cin%20A_2%7D%5Ccos(w,%20x)%5Cbig)%7D\" alt=\"d(w,A_1,A_2) = mean_{x in A_1}cos(w,x) - mean_{x in A_2}cos(w,x)\">\u00a0where\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7B%5Ccos(w,x)%7D\" alt=\"cos(w, x)\">\u00a0is the cosine similarity of the word embedding vectors.<\/p>\n<p>Then for a query\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BQ=(%7BT_1,T_2%7D,%7BA_1,A_2%7D)%7D\" alt=\"Q=({T_1,T_2},{A_1,A_2})\">\u00a0the WEAT metric is defined over the embeddings of the query word sets as:<\/p>\n<p><img class=\"aligncenter\" src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BF_%7B%5Ctext%7BWEAT%7D%7D(%5Cmathbf%7BM%7D,Q)%20=%20%5Csum_%7Bw%20%5Cin%20T_1%7D%20d(w,A_1,A_2)%20-%20%5Csum_%7Bw%20%5Cin%20T_2%7D%20d(w,A_1,A_2)%20%7D\" alt=\"F_WEAT(M, Q) = sum{w in T_1} d(w, A_1, A_2) - sum(w in T_2) d(w, A_1, A_2)\"><\/p>\n<p>The idea is that the more positive the value given by\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BF_%5Ctext%7BWEAT%7D%7D\" alt=\"F_WEAT\">, the more the target\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BT_1%7D\" alt=\"T_1\">\u00a0will be related to attribute\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BA_1%7D\" alt=\"A_1\">\u00a0and target\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BT_2%7D\" alt=\"T_2\">\u00a0to attribute\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BA_2%7D\" alt=\"A_2\">. On the other hand, the more negative the value, the more target <img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BT_1%7D\" alt=\"T_1\">\u00a0will be related to attribute\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BA_2%7D\" alt=\"A_2\">\u00a0and target\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BT_2%7D\" alt=\"T_2\">\u00a0to attribute\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7BA_1%7D\" alt=\"A_1\">. Commonly these values are between\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7B%5Cpm0.5\" alt=\"+-0.5\">\u00a0and\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7B%5Cpm2.0%7D\" alt=\"+-2\">. The ideal score is\u00a0<img src=\"https:\/\/latex.codecogs.com\/gif.latex?%5Cmathbf%7B0%7D\" alt=\"+-0.5\">.<\/p>\n<p>1. We first load a word embedding model using the\u00a0<a href=\"https:\/\/radimrehurek.com\/gensim\/\">gensim<\/a>\u00a0API.<\/p>\n<div>\n<pre>from wefe import WordEmbeddingModel, Query, WEAT\r\nimport gensim.downloader as api\r\n\r\nword2vec_model = WordEmbeddingModel(api.load('word2vec-google-news-300'),\r\n                                    'word2vec-google-news-300')\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>2. Then, we create the Query object using the target words (<em>Male names<\/em>\u00a0and\u00a0<em>Female names<\/em>) and two attribute words sets (<em>Career<\/em>\u00a0and\u00a0<em>Family<\/em>\u00a0terms).<\/p>\n<div>\n<pre># target sets (sets of popular names in the US)\r\nmale_names = ['John', 'Paul', 'Mike', 'Kevin', 'Steve', 'Greg', 'Jeff', 'Bill']\r\nfemale_names = ['Amy', 'Joan', 'Lisa', 'Sarah', 'Diana', 'Kate', 'Ann', 'Donna']\r\n\r\n#attribute sets\r\ncareer = ['executive', 'management', 'professional', 'corporation',\r\n         'salary', 'office', 'business', 'career']\r\nfamily = ['home', 'parents', 'children', 'family', 'cousins', 'marriage',\r\n         'wedding', 'relatives']\r\n\r\ngender_occupation_query = Query([male_names, female_names],\r\n                                [career, family],\r\n                                ['Male names', 'Female names'],\r\n                                ['Career', 'Family'])\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>3. Finally, we run the Query using WEAT as the metric.<\/p>\n<div>\n<pre>weat = WEAT()\r\nresults = weat.run_query(gender_occupation_query, word2vec_model)\r\n\r\nprint(results\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<div>\n<pre>{'query_name': 'Male names and Female names wrt Career and Family',\r\n 'result': 1.251}\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>As we can see, the execution returns a\u00a0<em>dict<\/em>\u00a0with the name of the executed query and its score. The score being positive and higher than one indicates that\u00a0<em>word2vec<\/em>\u00a0exhibits a moderately strong relationship between men&#8217;s names and careers and women&#8217;s names and family.<\/p>\n<p><strong>Running multiple Queries<\/strong><\/p>\n<p>In WEFE, we can easily test multiple queries in one single call:<\/p>\n<p>1. Create the queries:<\/p>\n<div>\n<pre>from wefe.utils import run_queries\r\nfrom wefe.datasets import load_weat\r\n\r\n# Load the sets used in the weat case study\r\nweat_wordsets = load_weat()\r\n\r\ngender_math_arts_query = Query(\r\n    [male_names, female_names],\r\n    [weat_wordsets['math'], weat_wordsets['arts']],\r\n    ['Male names', 'Female names'],\r\n    ['Math', 'Arts'])\r\n\r\ngender_science_arts_query = Query(\r\n    [male_names, female_names],\r\n    [weat_wordsets['science'], weat_wordsets['arts_2']],\r\n    ['Male names', 'Female names'],\r\n    ['Science', 'Arts'])\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>2. Add the queries to an array:<\/p>\n<div>\n<pre>queries = [\r\n    gender_occupation_query,\r\n    gender_math_arts_query,\r\n    gender_science_arts_query,\r\n]\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>3. Run the queries using WEAT:<\/p>\n<div>\n<pre>weat_results = run_queries(WEAT, queries, [word2vec_model])\r\nweat_results\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>Note that these results are returned as DataFrame objects.<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>model_name<\/strong><\/td>\n<td><strong>Male names and Female names wrt Career and Family<\/strong><\/td>\n<td><strong>Male names and Female names wrt Math and Arts<\/strong><\/td>\n<td><strong>Male names and Female names wrt Science and Arts<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>word2vec-google-news-300<\/td>\n<td>1.25161<\/td>\n<td>0.631749<\/td>\n<td>0.535707<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p>We can see that in all cases, male names are positively associated with career, science and math words, whereas female names are more associated with family and art terms.<\/p>\n<p>While the above results give us an idea of the gender bias that\u00a0word2vec\u00a0exhibits, we would also like to know how these biases occur in other Word Embeddings models.<\/p>\n<p>We run the same queries on two other embedding models: &#8220;glove-wiki&#8221; and &#8220;glove-twitter&#8221;.<\/p>\n<p>1. Load glove models and execute the queries again:<\/p>\n<div>\n<pre># load the models.\r\nglove_wiki = WordEmbeddingModel(api.load('glove-wiki-gigaword-300'),\r\n                                'glove-wiki-gigaword-300')\r\nglove_twitter = WordEmbeddingModel(api.load('glove-twitter-200'),\r\n                                'glove-twitter-200')\r\n\r\n# add the models to an array.\r\nmodels = [word2vec_model, glove_wiki, glove_twitter]\r\n\r\n# run the queries.\r\nresults = run_queries(WEAT, queries, models)\r\nresults\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Model Name<\/strong><\/td>\n<td><strong>Male names and Female names wrt Career and Family<\/strong><\/td>\n<td><strong>Male names and Female names wrt Math and Arts<\/strong><\/td>\n<td><strong>Male names and Female names wrt Science and Arts<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>word2vec-google-news-300<\/td>\n<td>1.25161<\/td>\n<td>0.631749<\/td>\n<td>0.535707<\/td>\n<\/tr>\n<tr>\n<td>glove-wiki-gigaword-300<\/td>\n<td>1.31949<\/td>\n<td>0.536996<\/td>\n<td>0.549819<\/td>\n<\/tr>\n<tr>\n<td>glove-twitter-200<\/td>\n<td>0.537437<\/td>\n<td>0.445879<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p>2. We can also plot the results:<\/p>\n<div>\n<pre>from wefe.utils import plot_queries_results\r\nplot_queries_results(results)\r\n<\/pre>\n<\/div>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/raw.githubusercontent.com\/dccuchile\/wefe\/master\/examples\/scores_weat_simple.PNG\" width=\"90%\"><\/p>\n<p>\u00a0<\/p>\n<h3>Aggregating Results<\/h3>\n<p>\u00a0<\/p>\n<p>The execution of\u00a0run_queries\u00a0in the previous step gave us various result scores. However, these do not tell us much about the overall fairness of the embedding models.<\/p>\n<p>We would like to have some mechanism to aggregate these results into a single score.<\/p>\n<p>To do this, when using\u00a0run_queries, you can set the\u00a0add_results\u00a0parameter to\u00a0True. This will activate the option to add the results by averaging the absolute values of the results and putting them in the last column of the result table.<\/p>\n<p>It is also possible to ask the function\u00a0run_queries\u00a0to return only the aggregated results by setting the\u00a0return_only_aggregation\u00a0parameter to\u00a0True.<\/p>\n<div>\n<pre>weat_results = run_queries(WEAT,\r\n                        queries,\r\n                        models,\r\n                        aggregate_results=True,\r\n                        return_only_aggregation=True,\r\n                        queries_set_name='Gender bias')\r\nweat_results\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>model_name<\/strong><\/td>\n<td><strong>WEAT: Gender bias average of abs values score<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>word2vec-google-news-300<\/td>\n<td>0.806355<\/td>\n<\/tr>\n<tr>\n<td>glove-wiki-gigaword-300<\/td>\n<td>0.802102<\/td>\n<\/tr>\n<tr>\n<td>glove-twitter-200<\/td>\n<td>0.438586<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p>The idea of this type of aggregation is to quantify the amount of bias of the embedding model according to various queries. In this case, we can see that glove-twitter has a lower amount of gender bias than the other models.<\/p>\n<p>\u00a0<\/p>\n<h3>Rank Word Embeddings<\/h3>\n<p>\u00a0<\/p>\n<p>Finally, we would like to rank these embedding models according to the overall amount of bias they contain. This can be done using the\u00a0create_ranking\u00a0function, which calculates a fairness ranking from one or more query result.<\/p>\n<div>\n<pre>from wefe.utils import create_ranking\r\n\r\nranking = create_ranking([weat_results])\r\nranking\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>model_name<\/strong><\/td>\n<td><strong>WEAT: Gender bias average of abs values score<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>word2vec-google-news-300<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>glove-wiki-gigaword-300<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>glove-twitter-200<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p>You can see this tutorial code in this\u00a0<a href=\"https:\/\/github.com\/dccuchile\/wefe\/blob\/master\/examples\/KDNuggetsTutorial.ipynb\" target=\"_blank\" rel=\"noopener noreferrer\">notebook<\/a>\u00a0and the complete reference documentation including a user guide, examples and replication of previous studies at the following\u00a0<a href=\"https:\/\/wefe.readthedocs.io\/en\/latest\/\" target=\"_blank\" rel=\"noopener noreferrer\">link<\/a>.<\/p>\n<p>If you like the project, you are more than welcome to &#8220;star&#8221; it on\u00a0<a href=\"https:\/\/github.com\/dccuchile\/wefe\" target=\"_blank\" rel=\"noopener noreferrer\">Github<\/a>.<\/p>\n<p>[1] P. Badilla, F. Bravo-Marquez, and J. P\u00e9rez\u00a0<a href=\"https:\/\/www.ijcai20.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">WEFE: The Word Embeddings Fairness Evaluation Framework<\/a>\u00a0In\u00a0<em>Proceedings of the 29th International Joint Conference on Artificial Intelligence and the 17th Pacific Rim International Conference on Artificial Intelligence (IJCAI-PRICAI 2020)<\/em>, Yokohama, Japan.<\/p>\n<p>\u00a0<\/p>\n<p><b>Related:<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.kdnuggets.com\/2020\/08\/word-embedding-fairness-evaluation.html<\/p>\n","protected":false},"author":0,"featured_media":127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/127"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}