{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Operations on word vectors\n", "\n", "Welcome to your first assignment of this week! \n", "\n", "Because word embeddings are very computationally expensive to train, most ML practitioners will load a pre-trained set of embeddings. \n", "\n", "**After this assignment you will be able to:**\n", "\n", "- Load pre-trained word vectors, and measure similarity using cosine similarity\n", "- Use word embeddings to solve word analogy problems such as Man is to Woman as King is to ______. \n", "- Modify word embeddings to reduce their gender bias \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Updates\n", "\n", "#### If you were working on the notebook before this update...\n", "* The current notebook is version \"2a\".\n", "* You can find your original work saved in the notebook with the previous version name (\"v2\") \n", "* To view the file directory, go to the menu \"File->Open\", and this will open a new tab that shows the file directory.\n", "\n", "#### List of updates\n", "* cosine_similarity\n", " * Additional hints.\n", "* complete_analogy\n", " * Replaces the list of input words with a set, and sets it outside the for loop (to follow best practices in coding).\n", "* Spelling, grammar and wording corrections." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's get started! Run the following cell to load the packages you will need." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "from w2v_utils import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load the word vectors\n", "* For this assignment, we will use 50-dimensional GloVe vectors to represent words. \n", "* Run the following cell to load the `word_to_vec_map`. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "words, word_to_vec_map = read_glove_vecs('../../readonly/glove.6B.50d.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've loaded:\n", "- `words`: set of words in the vocabulary.\n", "- `word_to_vec_map`: dictionary mapping words to their GloVe vector representation.\n", "\n", "#### Embedding vectors versus one-hot vectors\n", "* Recall from the lesson videos that one-hot vectors do not do a good job of capturing the level of similarity between words (every one-hot vector has the same Euclidean distance from any other one-hot vector).\n", "* Embedding vectors such as GloVe vectors provide much more useful information about the meaning of individual words. \n", "* Lets now see how you can use GloVe vectors to measure the similarity between two words. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1 - Cosine similarity\n", "\n", "To measure the similarity between two words, we need a way to measure the degree of similarity between two embedding vectors for the two words. Given two vectors $u$ and $v$, cosine similarity is defined as follows: \n", "\n", "$$\\text{CosineSimilarity(u, v)} = \\frac {u \\cdot v} {||u||_2 ||v||_2} = cos(\\theta) \\tag{1}$$\n", "\n", "* $u \\cdot v$ is the dot product (or inner product) of two vectors\n", "* $||u||_2$ is the norm (or length) of the vector $u$\n", "* $\\theta$ is the angle between $u$ and $v$. \n", "* The cosine similarity depends on the angle between $u$ and $v$. \n", " * If $u$ and $v$ are very similar, their cosine similarity will be close to 1.\n", " * If they are dissimilar, the cosine similarity will take a smaller value. \n", "\n", "\n", "
**Figure 1**: The cosine of the angle between two vectors is a measure their similarity
\n", "\n", "**Exercise**: Implement the function `cosine_similarity()` to evaluate the similarity between word vectors.\n", "\n", "**Reminder**: The norm of $u$ is defined as $ ||u||_2 = \\sqrt{\\sum_{i=1}^{n} u_i^2}$\n", "\n", "#### Additional Hints\n", "* You may find `np.dot`, `np.sum`, or `np.sqrt` useful depending upon the implementation that you choose." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# GRADED FUNCTION: cosine_similarity\n", "\n", "def cosine_similarity(u, v):\n", " \"\"\"\n", " Cosine similarity reflects the degree of similarity between u and v\n", " \n", " Arguments:\n", " u -- a word vector of shape (n,) \n", " v -- a word vector of shape (n,)\n", "\n", " Returns:\n", " cosine_similarity -- the cosine similarity between u and v defined by the formula above.\n", " \"\"\"\n", " \n", " distance = 0.0\n", " \n", " ### START CODE HERE ###\n", " # Compute the dot product between u and v (≈1 line)\n", " dot = np.dot(u,v)\n", " # Compute the L2 norm of u (≈1 line)\n", " norm_u = np.sqrt(np.sum(u * u))\n", " \n", " # Compute the L2 norm of v (≈1 line)\n", " norm_v = np.sqrt(np.sum(v * v))\n", " # Compute the cosine similarity defined by formula (1) (≈1 line)\n", " cosine_similarity = dot / (norm_u * norm_v)\n", " ### END CODE HERE ###\n", " \n", " return cosine_similarity" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cosine_similarity(father, mother) = 0.890903844289\n", "cosine_similarity(ball, crocodile) = 0.274392462614\n", "cosine_similarity(france - paris, rome - italy) = -0.675147930817\n" ] } ], "source": [ "father = word_to_vec_map[\"father\"]\n", "mother = word_to_vec_map[\"mother\"]\n", "ball = word_to_vec_map[\"ball\"]\n", "crocodile = word_to_vec_map[\"crocodile\"]\n", "france = word_to_vec_map[\"france\"]\n", "italy = word_to_vec_map[\"italy\"]\n", "paris = word_to_vec_map[\"paris\"]\n", "rome = word_to_vec_map[\"rome\"]\n", "\n", "print(\"cosine_similarity(father, mother) = \", cosine_similarity(father, mother))\n", "print(\"cosine_similarity(ball, crocodile) = \",cosine_similarity(ball, crocodile))\n", "print(\"cosine_similarity(france - paris, rome - italy) = \",cosine_similarity(france - paris, rome - italy))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Expected Output**:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **cosine_similarity(father, mother)** =\n", " \n", " 0.890903844289\n", "
\n", " **cosine_similarity(ball, crocodile)** =\n", " \n", " 0.274392462614\n", "
\n", " **cosine_similarity(france - paris, rome - italy)** =\n", " \n", " -0.675147930817\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try different words!\n", "* After you get the correct expected output, please feel free to modify the inputs and measure the cosine similarity between other pairs of words! \n", "* Playing around with the cosine similarity of other inputs will give you a better sense of how word vectors behave." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2 - Word analogy task\n", "\n", "* In the word analogy task, we complete the sentence: \n", " \"*a* is to *b* as *c* is to **____**\". \n", "\n", "* An example is: \n", " '*man* is to *woman* as *king* is to *queen*' . \n", "\n", "* We are trying to find a word *d*, such that the associated word vectors $e_a, e_b, e_c, e_d$ are related in the following manner: \n", " $e_b - e_a \\approx e_d - e_c$\n", "* We will measure the similarity between $e_b - e_a$ and $e_d - e_c$ using cosine similarity. \n", "\n", "**Exercise**: Complete the code below to be able to perform word analogies!" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# GRADED FUNCTION: complete_analogy\n", "\n", "def complete_analogy(word_a, word_b, word_c, word_to_vec_map):\n", " \"\"\"\n", " Performs the word analogy task as explained above: a is to b as c is to ____. \n", " \n", " Arguments:\n", " word_a -- a word, string\n", " word_b -- a word, string\n", " word_c -- a word, string\n", " word_to_vec_map -- dictionary that maps words to their corresponding vectors. \n", " \n", " Returns:\n", " best_word -- the word such that v_b - v_a is close to v_best_word - v_c, as measured by cosine similarity\n", " \"\"\"\n", " \n", " # convert words to lowercase\n", " word_a, word_b, word_c = word_a.lower(), word_b.lower(), word_c.lower()\n", " \n", " ### START CODE HERE ###\n", " # Get the word embeddings e_a, e_b and e_c (≈1-3 lines)\n", " e_a, e_b, e_c = word_to_vec_map[word_a], word_to_vec_map[word_b], word_to_vec_map[word_c]\n", " ### END CODE HERE ###\n", " \n", " words = word_to_vec_map.keys()\n", " max_cosine_sim = -100 # Initialize max_cosine_sim to a large negative number\n", " best_word = None # Initialize best_word with None, it will help keep track of the word to output\n", "\n", " # to avoid best_word being one of the input words, skip the input words\n", " # place the input words in a set for faster searching than a list\n", " # We will re-use this set of input words inside the for-loop\n", " input_words_set = set([word_a, word_b, word_c])\n", " \n", " # loop over the whole word vector set\n", " for w in words: \n", " # to avoid best_word being one of the input words, skip the input words\n", " if w in input_words_set:\n", " continue\n", " \n", " ### START CODE HERE ###\n", " # Compute cosine similarity between the vector (e_b - e_a) and the vector ((w's vector representation) - e_c) (≈1 line)\n", " cosine_sim = cosine_similarity(e_b - e_a, word_to_vec_map[w] - e_c)\n", " \n", " # If the cosine_sim is more than the max_cosine_sim seen so far,\n", " # then: set the new max_cosine_sim to the current cosine_sim and the best_word to the current word (≈3 lines)\n", " if cosine_sim > max_cosine_sim:\n", " max_cosine_sim = cosine_sim\n", " best_word = w\n", " ### END CODE HERE ###\n", " \n", " return best_word" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the cell below to test your code, this may take 1-2 minutes." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "italy -> italian :: spain -> spanish\n", "india -> delhi :: japan -> tokyo\n", "man -> woman :: boy -> girl\n", "small -> smaller :: large -> larger\n" ] } ], "source": [ "triads_to_try = [('italy', 'italian', 'spain'), ('india', 'delhi', 'japan'), ('man', 'woman', 'boy'), ('small', 'smaller', 'large')]\n", "for triad in triads_to_try:\n", " print ('{} -> {} :: {} -> {}'.format( *triad, complete_analogy(*triad,word_to_vec_map)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Expected Output**:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **italy -> italian** ::\n", " \n", " spain -> spanish\n", "
\n", " **india -> delhi** ::\n", " \n", " japan -> tokyo\n", "
\n", " **man -> woman ** ::\n", " \n", " boy -> girl\n", "
\n", " **small -> smaller ** ::\n", " \n", " large -> larger\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Once you get the correct expected output, please feel free to modify the input cells above to test your own analogies. \n", "* Try to find some other analogy pairs that do work, but also find some where the algorithm doesn't give the right answer:\n", " * For example, you can try small->smaller as big->?." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Congratulations!\n", "\n", "You've come to the end of the graded portion of the assignment. Here are the main points you should remember:\n", "\n", "- Cosine similarity is a good way to compare the similarity between pairs of word vectors.\n", " - Note that L2 (Euclidean) distance also works.\n", "- For NLP applications, using a pre-trained set of word vectors is often a good way to get started.\n", "- Even though you have finished the graded portions, we recommend you take a look at the rest of this notebook to learn about debiasing word vectors.\n", "\n", "Congratulations on finishing the graded portions of this notebook! \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3 - Debiasing word vectors (OPTIONAL/UNGRADED) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following exercise, you will examine gender biases that can be reflected in a word embedding, and explore algorithms for reducing the bias. In addition to learning about the topic of debiasing, this exercise will also help hone your intuition about what word vectors are doing. This section involves a bit of linear algebra, though you can probably complete it even without being an expert in linear algebra, and we encourage you to give it a shot. This portion of the notebook is optional and is not graded. \n", "\n", "Lets first see how the GloVe word embeddings relate to gender. You will first compute a vector $g = e_{woman}-e_{man}$, where $e_{woman}$ represents the word vector corresponding to the word *woman*, and $e_{man}$ corresponds to the word vector corresponding to the word *man*. The resulting vector $g$ roughly encodes the concept of \"gender\". (You might get a more accurate representation if you compute $g_1 = e_{mother}-e_{father}$, $g_2 = e_{girl}-e_{boy}$, etc. and average over them. But just using $e_{woman}-e_{man}$ will give good enough results for now.) \n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.087144 0.2182 -0.40986 -0.03922 -0.1032 0.94165\n", " -0.06042 0.32988 0.46144 -0.35962 0.31102 -0.86824\n", " 0.96006 0.01073 0.24337 0.08193 -1.02722 -0.21122\n", " 0.695044 -0.00222 0.29106 0.5053 -0.099454 0.40445\n", " 0.30181 0.1355 -0.0606 -0.07131 -0.19245 -0.06115\n", " -0.3204 0.07165 -0.13337 -0.25068714 -0.14293 -0.224957\n", " -0.149 0.048882 0.12191 -0.27362 -0.165476 -0.20426\n", " 0.54376 -0.271425 -0.10245 -0.32108 0.2516 -0.33455\n", " -0.04371 0.01258 ]\n" ] } ], "source": [ "g = word_to_vec_map['woman'] - word_to_vec_map['man']\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, you will consider the cosine similarity of different words with $g$. Consider what a positive value of similarity means vs a negative cosine similarity. " ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "List of names and their similarities with constructed vector:\n", "john -0.23163356146\n", "marie 0.315597935396\n", "sophie 0.318687898594\n", "ronaldo -0.312447968503\n", "priya 0.17632041839\n", "rahul -0.169154710392\n", "danielle 0.243932992163\n", "reza -0.079304296722\n", "katy 0.283106865957\n", "yasmin 0.233138577679\n" ] } ], "source": [ "print ('List of names and their similarities with constructed vector:')\n", "\n", "# girls and boys name\n", "name_list = ['john', 'marie', 'sophie', 'ronaldo', 'priya', 'rahul', 'danielle', 'reza', 'katy', 'yasmin']\n", "\n", "for w in name_list:\n", " print (w, cosine_similarity(word_to_vec_map[w], g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, female first names tend to have a positive cosine similarity with our constructed vector $g$, while male first names tend to have a negative cosine similarity. This is not surprising, and the result seems acceptable. \n", "\n", "But let's try with some other words." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Other words and their similarities:\n", "lipstick 0.276919162564\n", "guns -0.18884855679\n", "science -0.0608290654093\n", "arts 0.00818931238588\n", "literature 0.0647250443346\n", "warrior -0.209201646411\n", "doctor 0.118952894109\n", "tree -0.0708939917548\n", "receptionist 0.330779417506\n", "technology -0.131937324476\n", "fashion 0.0356389462577\n", "teacher 0.179209234318\n", "engineer -0.0803928049452\n", "pilot 0.00107644989919\n", "computer -0.103303588739\n", "singer 0.185005181365\n" ] } ], "source": [ "print('Other words and their similarities:')\n", "word_list = ['lipstick', 'guns', 'science', 'arts', 'literature', 'warrior','doctor', 'tree', 'receptionist', \n", " 'technology', 'fashion', 'teacher', 'engineer', 'pilot', 'computer', 'singer']\n", "for w in word_list:\n", " print (w, cosine_similarity(word_to_vec_map[w], g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do you notice anything surprising? It is astonishing how these results reflect certain unhealthy gender stereotypes. For example, \"computer\" is closer to \"man\" while \"literature\" is closer to \"woman\". Ouch! \n", "\n", "We'll see below how to reduce the bias of these vectors, using an algorithm due to [Boliukbasi et al., 2016](https://arxiv.org/abs/1607.06520). Note that some word pairs such as \"actor\"/\"actress\" or \"grandmother\"/\"grandfather\" should remain gender specific, while other words such as \"receptionist\" or \"technology\" should be neutralized, i.e. not be gender-related. You will have to treat these two types of words differently when debiasing.\n", "\n", "### 3.1 - Neutralize bias for non-gender specific words \n", "\n", "The figure below should help you visualize what neutralizing does. If you're using a 50-dimensional word embedding, the 50 dimensional space can be split into two parts: The bias-direction $g$, and the remaining 49 dimensions, which we'll call $g_{\\perp}$. In linear algebra, we say that the 49 dimensional $g_{\\perp}$ is perpendicular (or \"orthogonal\") to $g$, meaning it is at 90 degrees to $g$. The neutralization step takes a vector such as $e_{receptionist}$ and zeros out the component in the direction of $g$, giving us $e_{receptionist}^{debiased}$. \n", "\n", "Even though $g_{\\perp}$ is 49 dimensional, given the limitations of what we can draw on a 2D screen, we illustrate it using a 1 dimensional axis below. \n", "\n", "\n", "
**Figure 2**: The word vector for \"receptionist\" represented before and after applying the neutralize operation.
\n", "\n", "**Exercise**: Implement `neutralize()` to remove the bias of words such as \"receptionist\" or \"scientist\". Given an input embedding $e$, you can use the following formulas to compute $e^{debiased}$: \n", "\n", "$$e^{bias\\_component} = \\frac{e \\cdot g}{||g||_2^2} * g\\tag{2}$$\n", "$$e^{debiased} = e - e^{bias\\_component}\\tag{3}$$\n", "\n", "If you are an expert in linear algebra, you may recognize $e^{bias\\_component}$ as the projection of $e$ onto the direction $g$. If you're not an expert in linear algebra, don't worry about this.\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def neutralize(word, g, word_to_vec_map):\n", " \"\"\"\n", " Removes the bias of \"word\" by projecting it on the space orthogonal to the bias axis. \n", " This function ensures that gender neutral words are zero in the gender subspace.\n", " \n", " Arguments:\n", " word -- string indicating the word to debias\n", " g -- numpy-array of shape (50,), corresponding to the bias axis (such as gender)\n", " word_to_vec_map -- dictionary mapping words to their corresponding vectors.\n", " \n", " Returns:\n", " e_debiased -- neutralized word vector representation of the input \"word\"\n", " \"\"\"\n", " \n", " ### START CODE HERE ###\n", " # Select word vector representation of \"word\". Use word_to_vec_map. (≈ 1 line)\n", " e = word_to_vec_map[word]\n", " \n", " # Compute e_biascomponent using the formula give above. (≈ 1 line)\n", " e_biascomponent = np.dot(e ,g) / np.sum(g * g) * g\n", " \n", " # Neutralize e by substracting e_biascomponent from it \n", " # e_debiased should be equal to its orthogonal projection. (≈ 1 line)\n", " e_debiased = e - e_biascomponent\n", " ### END CODE HERE ###\n", " \n", " return e_debiased" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cosine similarity between receptionist and g, before neutralizing: 0.330779417506\n", "cosine similarity between receptionist and g, after neutralizing: -4.08872263257e-17\n" ] } ], "source": [ "e = \"receptionist\"\n", "print(\"cosine similarity between \" + e + \" and g, before neutralizing: \", cosine_similarity(word_to_vec_map[\"receptionist\"], g))\n", "\n", "e_debiased = neutralize(\"receptionist\", g, word_to_vec_map)\n", "print(\"cosine similarity between \" + e + \" and g, after neutralizing: \", cosine_similarity(e_debiased, g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Expected Output**: The second result is essentially 0, up to numerical rounding (on the order of $10^{-17}$).\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **cosine similarity between receptionist and g, before neutralizing:** :\n", " \n", " 0.330779417506\n", "
\n", " **cosine similarity between receptionist and g, after neutralizing:** :\n", " \n", " -3.26732746085e-17\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2 - Equalization algorithm for gender-specific words\n", "\n", "Next, lets see how debiasing can also be applied to word pairs such as \"actress\" and \"actor.\" Equalization is applied to pairs of words that you might want to have differ only through the gender property. As a concrete example, suppose that \"actress\" is closer to \"babysit\" than \"actor.\" By applying neutralizing to \"babysit\" we can reduce the gender-stereotype associated with babysitting. But this still does not guarantee that \"actor\" and \"actress\" are equidistant from \"babysit.\" The equalization algorithm takes care of this. \n", "\n", "The key idea behind equalization is to make sure that a particular pair of words are equi-distant from the 49-dimensional $g_\\perp$. The equalization step also ensures that the two equalized steps are now the same distance from $e_{receptionist}^{debiased}$, or from any other work that has been neutralized. In pictures, this is how equalization works: \n", "\n", "\n", "\n", "\n", "The derivation of the linear algebra to do this is a bit more complex. (See Bolukbasi et al., 2016 for details.) But the key equations are: \n", "\n", "$$ \\mu = \\frac{e_{w1} + e_{w2}}{2}\\tag{4}$$ \n", "\n", "$$ \\mu_{B} = \\frac {\\mu \\cdot \\text{bias_axis}}{||\\text{bias_axis}||_2^2} *\\text{bias_axis}\n", "\\tag{5}$$ \n", "\n", "$$\\mu_{\\perp} = \\mu - \\mu_{B} \\tag{6}$$\n", "\n", "$$ e_{w1B} = \\frac {e_{w1} \\cdot \\text{bias_axis}}{||\\text{bias_axis}||_2^2} *\\text{bias_axis}\n", "\\tag{7}$$ \n", "$$ e_{w2B} = \\frac {e_{w2} \\cdot \\text{bias_axis}}{||\\text{bias_axis}||_2^2} *\\text{bias_axis}\n", "\\tag{8}$$\n", "\n", "\n", "$$e_{w1B}^{corrected} = \\sqrt{ |{1 - ||\\mu_{\\perp} ||^2_2} |} * \\frac{e_{\\text{w1B}} - \\mu_B} {||(e_{w1} - \\mu_{\\perp}) - \\mu_B||} \\tag{9}$$\n", "\n", "\n", "$$e_{w2B}^{corrected} = \\sqrt{ |{1 - ||\\mu_{\\perp} ||^2_2} |} * \\frac{e_{\\text{w2B}} - \\mu_B} {||(e_{w2} - \\mu_{\\perp}) - \\mu_B||} \\tag{10}$$\n", "\n", "$$e_1 = e_{w1B}^{corrected} + \\mu_{\\perp} \\tag{11}$$\n", "$$e_2 = e_{w2B}^{corrected} + \\mu_{\\perp} \\tag{12}$$\n", "\n", "\n", "**Exercise**: Implement the function below. Use the equations above to get the final equalized version of the pair of words. Good luck!" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def equalize(pair, bias_axis, word_to_vec_map):\n", " \"\"\"\n", " Debias gender specific words by following the equalize method described in the figure above.\n", " \n", " Arguments:\n", " pair -- pair of strings of gender specific words to debias, e.g. (\"actress\", \"actor\") \n", " bias_axis -- numpy-array of shape (50,), vector corresponding to the bias axis, e.g. gender\n", " word_to_vec_map -- dictionary mapping words to their corresponding vectors\n", " \n", " Returns\n", " e_1 -- word vector corresponding to the first word\n", " e_2 -- word vector corresponding to the second word\n", " \"\"\"\n", " \n", " ### START CODE HERE ###\n", " # Step 1: Select word vector representation of \"word\". Use word_to_vec_map. (≈ 2 lines)\n", " w1, w2 = pair\n", " e_w1, e_w2 = word_to_vec_map[w1],word_to_vec_map[w2]\n", " \n", " # Step 2: Compute the mean of e_w1 and e_w2 (≈ 1 line)\n", " mu = (e_w1 + e_w2) / 2\n", "\n", " # Step 3: Compute the projections of mu over the bias axis and the orthogonal axis (≈ 2 lines)\n", " mu_B = np.dot(mu, bias_axis) / np.sum(bias_axis * bias_axis) * bias_axis\n", " mu_orth = mu - mu_B\n", "\n", " # Step 4: Use equations (7) and (8) to compute e_w1B and e_w2B (≈2 lines)\n", " e_w1B = np.dot(e_w1, bias_axis) / np.sum(bias_axis * bias_axis) * bias_axis\n", " e_w2B = np.dot(e_w2, bias_axis) / np.sum(bias_axis * bias_axis) * bias_axis\n", " \n", " # Step 5: Adjust the Bias part of e_w1B and e_w2B using the formulas (9) and (10) given above (≈2 lines)\n", " corrected_e_w1B = np.sqrt(np.abs(1 - np.sum(mu_orth * mu_orth))) * (e_w1B - mu_B) / np.linalg.norm(e_w1 - mu_orth - mu_B)\n", " corrected_e_w2B = np.sqrt(np.abs(1 - np.sum(mu_orth * mu_orth))) * (e_w2B - mu_B) / np.linalg.norm(e_w2 - mu_orth - mu_B)\n", "\n", " # Step 6: Debias by equalizing e1 and e2 to the sum of their corrected projections (≈2 lines)\n", " e1 = corrected_e_w1B + mu_orth\n", " e2 = corrected_e_w2B + mu_orth\n", " \n", " ### END CODE HERE ###\n", " \n", " return e1, e2" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cosine similarities before equalizing:\n", "cosine_similarity(word_to_vec_map[\"man\"], gender) = -0.117110957653\n", "cosine_similarity(word_to_vec_map[\"woman\"], gender) = 0.356666188463\n", "\n", "cosine similarities after equalizing:\n", "cosine_similarity(e1, gender) = -0.700436428931\n", "cosine_similarity(e2, gender) = 0.700436428931\n" ] } ], "source": [ "print(\"cosine similarities before equalizing:\")\n", "print(\"cosine_similarity(word_to_vec_map[\\\"man\\\"], gender) = \", cosine_similarity(word_to_vec_map[\"man\"], g))\n", "print(\"cosine_similarity(word_to_vec_map[\\\"woman\\\"], gender) = \", cosine_similarity(word_to_vec_map[\"woman\"], g))\n", "print()\n", "e1, e2 = equalize((\"man\", \"woman\"), g, word_to_vec_map)\n", "print(\"cosine similarities after equalizing:\")\n", "print(\"cosine_similarity(e1, gender) = \", cosine_similarity(e1, g))\n", "print(\"cosine_similarity(e2, gender) = \", cosine_similarity(e2, g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Expected Output**:\n", "\n", "cosine similarities before equalizing:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **cosine_similarity(word_to_vec_map[\"man\"], gender)** =\n", " \n", " -0.117110957653\n", "
\n", " **cosine_similarity(word_to_vec_map[\"woman\"], gender)** =\n", " \n", " 0.356666188463\n", "
\n", "\n", "cosine similarities after equalizing:\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " **cosine_similarity(u1, gender)** =\n", " \n", " -0.700436428931\n", "
\n", " **cosine_similarity(u2, gender)** =\n", " \n", " 0.700436428931\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please feel free to play with the input words in the cell above, to apply equalization to other pairs of words. \n", "\n", "These debiasing algorithms are very helpful for reducing bias, but are not perfect and do not eliminate all traces of bias. For example, one weakness of this implementation was that the bias direction $g$ was defined using only the pair of words _woman_ and _man_. As discussed earlier, if $g$ were defined by computing $g_1 = e_{woman} - e_{man}$; $g_2 = e_{mother} - e_{father}$; $g_3 = e_{girl} - e_{boy}$; and so on and averaging over them, you would obtain a better estimate of the \"gender\" dimension in the 50 dimensional word embedding space. Feel free to play with such variants as well. \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Congratulations\n", "\n", "You have come to the end of this notebook, and have seen a lot of the ways that word vectors can be used as well as modified. \n", "\n", "Congratulations on finishing this notebook! \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**References**:\n", "- The debiasing algorithm is from Bolukbasi et al., 2016, [Man is to Computer Programmer as Woman is to\n", "Homemaker? Debiasing Word Embeddings](https://papers.nips.cc/paper/6228-man-is-to-computer-programmer-as-woman-is-to-homemaker-debiasing-word-embeddings.pdf)\n", "- The GloVe word embeddings were due to Jeffrey Pennington, Richard Socher, and Christopher D. Manning. (https://nlp.stanford.edu/projects/glove/)\n" ] } ], "metadata": { "coursera": { "course_slug": "nlp-sequence-models", "graded_item_id": "8hb5s", "launcher_item_id": "5NrJ6" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }