<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ConceptNet blog (Posts about Numberbatch)</title><link>http://concepts.arborelia.net/</link><description></description><atom:link href="http://concepts.arborelia.net/categories/numberbatch.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2021 &lt;a href="mailto:elia@arborelia.net"&gt;Robyn Speer&lt;/a&gt; </copyright><lastBuildDate>Fri, 16 Apr 2021 17:55:18 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Tutorial: Distinguishing attributes using ConceptNet</title><link>http://concepts.arborelia.net/posts/2018/distinguishing-attributes-using-conceptnet/</link><dc:creator>Robyn Speer</dc:creator><description>&lt;div&gt;&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;In &lt;a href="http://blog.conceptnet.io/posts/2018/conceptnet-at-semeval-2018/"&gt;a previous post&lt;/a&gt;,
we mentioned the good results that systems built using ConceptNet got at SemEval this year.
One of those systems was our own entry to the "Capturing Discriminative Attributes" task,
about determining differences in meanings between words.&lt;/p&gt;
&lt;p&gt;The system we submitted got second place, by combining information from ConceptNet, WordNet,
Wikipedia, and Google Books. That system has some messy dependencies and fiddly details, so
in this tutorial, we're going to build a much simpler version of the system that also performs
well.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;h3 id="Distinguishing-attributes-the-simple-way"&gt;Distinguishing attributes the simple way&lt;a class="anchor-link" href="http://concepts.arborelia.net/posts/2018/distinguishing-attributes-using-conceptnet/#Distinguishing-attributes-the-simple-way"&gt;¶&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Our poster, a prettier version of &lt;a href="http://aclweb.org/anthology/S18-1162"&gt;our SemEval paper&lt;/a&gt;, mainly presents the full version of the system, the one that uses five different methods of distinguishing attributes and combines them all in an SVM classifier. But here, I particularly want you to take note of the "ConceptNet is all you need" section, describing a simpler version we discovered while evaluating what made the full system work.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.conceptnet.io/2018/06/naacl2018-poster.pdf"&gt;&lt;img src="https://blog.conceptnet.io/2018/06/naacl2018-poster.png" alt="A rendering of our SemEval 2018 poster. The link leads to a PDF version."&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It seems that, instead of using five kinds of features, we may have been able to do just as well using just the pre-trained embeddings we call &lt;a href="https://github.com/commonsense/conceptnet-numberbatch"&gt;ConceptNet Numberbatch&lt;/a&gt;. So we'll build that system here, using the ConceptNet Numberbatch data and a small amount of code, with only common dependencies (pandas and sklearn).&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [1]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;f1_score&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;pd&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;I want you to be able to reproduce this result, so I've put the SemEval data files, along with the exact version of ConceptNet Numberbatch we were using, in a zip file on my favorite scientific data hosting service, Zenodo.&lt;/p&gt;
&lt;p&gt;These shell commands should serve the purpose of downloading and extracting that data, if the &lt;code&gt;wget&lt;/code&gt; and &lt;code&gt;unzip&lt;/code&gt; commands are available on your system.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [2]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;wget https://zenodo.org/record/1289942/files/conceptnet-distinguishing-attributes-data.zip
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt"&gt;&lt;/div&gt;


&lt;div class="output_subarea output_stream output_stdout output_text"&gt;
&lt;pre&gt;--2018-06-15 10:47:28--  https://zenodo.org/record/1289942/files/conceptnet-distinguishing-attributes-data.zip
Resolving zenodo.org (zenodo.org)... 137.138.76.77
Connecting to zenodo.org (zenodo.org)|137.138.76.77|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2457101853 (2.3G) [application/octet-stream]
Saving to: ‘conceptnet-distinguishing-attributes-data.zip’

conceptnet-distingu 100%[===================&amp;gt;]   2.29G  2.88MB/s    in 10m 9s  

2018-06-15 10:57:38 (3.85 MB/s) - ‘conceptnet-distinguishing-attributes-data.zip’ saved [2457101853/2457101853]

&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [8]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;unzip conceptnet-distinguishing-attributes-data.zip
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt"&gt;&lt;/div&gt;


&lt;div class="output_subarea output_stream output_stdout output_text"&gt;
&lt;pre&gt;Archive:  conceptnet-distinguishing-attributes-data.zip
  inflating: numberbatch-20180108-biased.h5  
  inflating: discriminatt-test.txt   
  inflating: discriminatt-train.txt  
  inflating: discriminatt-validation.txt  
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;In our actual solution, we imported some utilities from the &lt;a href="https://github.com/commonsense/conceptnet5"&gt;ConceptNet5 codebase&lt;/a&gt;. In this simplified version, we'll re-define the utilities that we need.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [9]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;text_to_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;    An extremely cut-down version of ConceptNet's `standardized_concept_uri`.&lt;/span&gt;
&lt;span class="sd"&gt;    Converts a term such as "apple" into its ConceptNet URI, "/c/en/apple".&lt;/span&gt;
&lt;span class="sd"&gt;    &lt;/span&gt;
&lt;span class="sd"&gt;    Only works for single English words, with no punctuation besides hyphens.&lt;/span&gt;
&lt;span class="sd"&gt;    """&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'/c/en/'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'-'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;normalize_vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;    Normalize a vector to a unit vector, so that dot products are cosine&lt;/span&gt;
&lt;span class="sd"&gt;    similarities.&lt;/span&gt;
&lt;span class="sd"&gt;    &lt;/span&gt;
&lt;span class="sd"&gt;    If it's the zero vector, leave it as is, so all its cosine similarities&lt;/span&gt;
&lt;span class="sd"&gt;    will be zero.&lt;/span&gt;
&lt;span class="sd"&gt;    """&lt;/span&gt;
    &lt;span class="n"&gt;norm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;norm&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;norm&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;We would need a lot more support from the ConceptNet code if we wanted to apply ConceptNet's strategy for out-of-vocabulary words. Fortunately, the words in this task are quite common. Our out-of-vocabulary strategy can be to return the zero vector.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [10]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AttributeHeuristic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hdf5_filename&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Load a word embedding matrix that is the 'mat' member of an HDF5 file,&lt;/span&gt;
&lt;span class="sd"&gt;        with UTF-8 labels for its rows.&lt;/span&gt;
&lt;span class="sd"&gt;        &lt;/span&gt;
&lt;span class="sd"&gt;        (This is the format that ConceptNet Numberbatch word embeddings use.)&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_hdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hdf5_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mat'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'utf-8'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Look up the vector for a term, returning it normalized to a unit vector.&lt;/span&gt;
&lt;span class="sd"&gt;        If the term is out-of-vocabulary, return a zero vector.&lt;/span&gt;
&lt;span class="sd"&gt;        &lt;/span&gt;
&lt;span class="sd"&gt;        Because many terms appear repeatedly in the data, cache the result.&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text_to_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;normalize_vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Series&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fillna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Get the cosine similarity between the embeddings of two terms.&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Our heuristic for whether an attribute applies more to term1 than&lt;/span&gt;
&lt;span class="sd"&gt;        to term2: find the cosine similarity of each term with the&lt;/span&gt;
&lt;span class="sd"&gt;        attribute, and take the difference of the square roots of those&lt;/span&gt;
&lt;span class="sd"&gt;        similarities.&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="n"&gt;match1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
        &lt;span class="n"&gt;match2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;match1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;match2&lt;/span&gt;
    
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Convert the attribute heuristic into a yes-or-no decision, by testing&lt;/span&gt;
&lt;span class="sd"&gt;        whether the difference is larger than a given threshold.&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;semeval_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sd"&gt;"""&lt;/span&gt;
&lt;span class="sd"&gt;        Evaluate the heuristic on a file containing instances of this form:&lt;/span&gt;
&lt;span class="sd"&gt;        &lt;/span&gt;
&lt;span class="sd"&gt;            banjo,harmonica,stations,0&lt;/span&gt;
&lt;span class="sd"&gt;            mushroom,onions,stem,1&lt;/span&gt;
&lt;span class="sd"&gt;            &lt;/span&gt;
&lt;span class="sd"&gt;        Return the macro-averaged F1 score. (As in the task, we use macro-&lt;/span&gt;
&lt;span class="sd"&gt;        averaged F1 instead of raw accuracy, to avoid being misled by&lt;/span&gt;
&lt;span class="sd"&gt;        imbalanced classes.)&lt;/span&gt;
&lt;span class="sd"&gt;        """&lt;/span&gt;
        &lt;span class="n"&gt;our_answers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="n"&gt;real_answers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;semeval_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'utf-8'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;discriminative&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strval&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="n"&gt;real_answers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;discriminative&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;our_answers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f1_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;real_answers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;our_answers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'macro'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;When we ran this solution, our latest set of word embeddings calculated from ConceptNet was 'numberbatch-20180108-biased'. This name indicates that it was built on January 8, 2018, and acknowledges that we haven't run it through the &lt;a href="http://blog.conceptnet.io/posts/2017/conceptnet-numberbatch-17-04-better-less-stereotyped-word-vectors/"&gt;de-biasing&lt;/a&gt; process, which we consider important when deploying a machine learning system.&lt;/p&gt;
&lt;p&gt;Here, we didn't want to complicate things by adding the de-biasing step. But keep in mind that this heuristic would probably have some unfortunate trends if it were asked to distinguish attributes of people's name, gender, or ethnicity.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [11]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AttributeHeuristic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'numberbatch-20180108-biased.h5'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;The classifier has one parameter that can vary, which is the "threshold": the minimum difference between cosine similarities that will count as a discriminative attribute. When we ran the training code &lt;a href="https://github.com/LuminosoInsight/semeval-discriminatt"&gt;for our full SemEval entry&lt;/a&gt; on this one feature, we got a classifier that's equivalent to a threshold of 0.096. Let's simplify that by rounding it off to 0.1.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [13]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'discriminatt-train.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[13]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.6620320353802582&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;When we were creating this code, we didn't have access to the test set -- this is pretty much the point of SemEval. We could compare results on the validation set, which is how we decided to use a combination of five features, where the feature you see here is only one of them. It's also how we found that taking the square root of the cosine similarities was helpful.&lt;/p&gt;
&lt;p&gt;When we're just revisiting a simplified version of the classifier, there isn't much that we need to do with the validation set, but let's take a look at how it does anyway.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [14]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'discriminatt-validation.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[14]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.693873461779053&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;But what's really interesting about this simple heuristic is how it performs on the previously held-out test set.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [15]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'discriminatt-test.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[15]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.7358997147499388&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;It's pretty remarkable to see a test accuracy that's so much higher than the training accuracy! It should actually make you suspicious that this classifier is somehow tuned to the test data.&lt;/p&gt;
&lt;p&gt;But that's why it's nice to have a result we can compare to that followed the SemEval process. Our actual SemEval entry got the same accuracy, 73.6%, and showed that we could attain that number without having any access to the test data.&lt;/p&gt;
&lt;p&gt;Many entries to this task performed better on the test data than on the validation data. It seems that the test set is cleaner overall than the validation set, which in turn is cleaner than the training set. Simple classifiers that generalize well had the chance to do much better on the test set. Classifiers which had the ability to focus too much on the specific details of the training set, some of which are erroneous, performed worse.&lt;/p&gt;
&lt;p&gt;But you could still question whether the simplified system that we came up with after the fact can actually be compared to the system we submitted, which will leads me on a digression about "lucky systems" at the end of this post.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;h3 id="Examples"&gt;Examples&lt;a class="anchor-link" href="http://concepts.arborelia.net/posts/2018/distinguishing-attributes-using-conceptnet/#Examples"&gt;¶&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Let's see how this heuristic does on some examples of these "discriminative attribute" questions.&lt;/p&gt;
&lt;p&gt;When we look at &lt;code&gt;heuristic.compare_attributes(a, b, c)&lt;/code&gt;, we're asking if &lt;em&gt;a&lt;/em&gt; is more associated with &lt;em&gt;c&lt;/em&gt; than &lt;em&gt;b&lt;/em&gt; is. The heuristic returns a number. By our evaluation above, we consider the attribute to be discriminative if the number is 0.1 or greater.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;Let's start with an easy one: Most windows are made of glass, and most doors aren't.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [17]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'window'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'door'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'glass'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[17]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.16762984210407628&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;From the examples in the code above: mushrooms have stems, while onions don't.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [35]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'mushroom'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'onions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'stem'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[35]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.11308354447365421&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;This one comes straight from the task description: cappuccino contains milk, while americano doesn't. Unfortunately, our heuristic is not confident about the distinction, and returns a value less than 0.1. It would fail this example in the evaluation.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [37]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'cappuccino'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'americano'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'milk'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[37]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.06309686358452515&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;An example of a non-discriminative attribute: trains and subways both involve rails. Our heuristic barely gets this right, but only due to lack of confidence.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [38]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'train'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'subway'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'rails'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[38]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;0.08336122961828196&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;p&gt;This was not required for the task, but the heuristic can also tell us when an attribute is discriminative in the opposite direction. Water is much more associated with soup than it is with fingers. It is a discriminative attribute that distinguishes &lt;em&gt;soup&lt;/em&gt; from &lt;em&gt;finger&lt;/em&gt;, not &lt;em&gt;finger&lt;/em&gt; from &lt;em&gt;soup&lt;/em&gt;. The heuristic gives us back a negative number indicating this.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [39]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;heuristic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compare_attributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'finger'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'soup'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'water'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="output_wrapper"&gt;
&lt;div class="output"&gt;


&lt;div class="output_area"&gt;

    &lt;div class="prompt output_prompt"&gt;Out[39]:&lt;/div&gt;




&lt;div class="output_text output_subarea output_execute_result"&gt;
&lt;pre&gt;-0.2778968364707769&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;div class="cell border-box-sizing text_cell rendered"&gt;&lt;div class="prompt input_prompt"&gt;
&lt;/div&gt;&lt;div class="inner_cell"&gt;
&lt;div class="text_cell_render border-box-sizing rendered_html"&gt;
&lt;h3 id="Lucky-systems"&gt;Lucky systems&lt;a class="anchor-link" href="http://concepts.arborelia.net/posts/2018/distinguishing-attributes-using-conceptnet/#Lucky-systems"&gt;¶&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;As a kid, I used to hold marble racing tournaments in my room, rolling marbles simultaneously down plastic towers of tracks and funnels. I went so far as to set up a bracket of 64 marbles to find the fastest marble. I kind of thought that running marble tournaments was peculiar to me and my childhood, but now I've found out that marble racing videos on YouTube are a big thing! Some of them even have &lt;a href="https://www.youtube.com/watch?v=7oZKioNQq-k"&gt;overlays as if they're major sporting events&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the end, there's nothing special about the fastest marble compared to most other marbles. It's just lucky. If one ran the tournament again, the marble champion might lose in the first round. But the one thing you could conclude about the fastest marble is that it was no &lt;em&gt;worse&lt;/em&gt; than the other marbles. A bad marble (say, a misshapen one, or a plastic bead) would never luck out enough to win.&lt;/p&gt;
&lt;p&gt;In our paper, we tested 30 alternate versions of the classifier, including the one that was roughly equivalent to this very simple system. We were impressed by the fact that it performed as well as our real entry. And this could be because of the inherent power of ConceptNet Numberbatch, or it could be because it's the lucky marble.&lt;/p&gt;
&lt;p&gt;I tried it with other thresholds besides 0.1, and some of the nearby reasonable threshold values only score 71% or 72%. But that still tells you that this interestingly simple system is doing the right thing and is capable of getting a very good result. It's good enough to be the lucky marble, so it's good enough for this tutorial.&lt;/p&gt;
&lt;p&gt;Incidentally, the same argument about "lucky systems" applies to SemEval entries themselves. There are dozens of entries from different teams, and the top-scoring entry is going to be an entry that did the right thing and also got lucky.&lt;/p&gt;
&lt;p&gt;In the post-SemEval discussion at ACL, someone proposed that all results should be Bayesian probability distributions, estimated by evaluating systems on various subsets of the test data, and instead of declaring a single winner or a tie, we should get probabilistic beliefs as results: "There is an 80% chance that entry A is the best solution to the task, an 18% chance that entry B is the best solution..."&lt;/p&gt;
&lt;p&gt;I find this argument entirely reasonable, and probably unlikely to catch on in a world where we haven't even managed to replace the use of &lt;em&gt;p&lt;/em&gt;-values.&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="cell border-box-sizing code_cell rendered"&gt;
&lt;div class="input"&gt;
&lt;div class="prompt input_prompt"&gt;In [ ]:&lt;/div&gt;
&lt;div class="inner_cell"&gt;
    &lt;div class="input_area"&gt;
&lt;div class=" highlight hl-ipython3"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt; 
&lt;/pre&gt;&lt;/div&gt;

    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;&lt;/div&gt;</description><category>ConceptNet</category><category>Numberbatch</category><category>Tutorials</category><guid>http://concepts.arborelia.net/posts/2018/distinguishing-attributes-using-conceptnet/</guid><pubDate>Fri, 28 Sep 2018 19:00:00 GMT</pubDate></item><item><title>Bugfix: our English-only word vectors contained the wrong data</title><link>http://concepts.arborelia.net/posts/2017/bugfix-our-english-only-word-vectors-contained-the-wrong-data/</link><dc:creator>Robyn Speer</dc:creator><description>&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;If you have used the ConceptNet Numberbatch 17.04 word vectors, it turns out that you got very different results if you downloaded the English-only vectors versus if you used the multilingual, language-tagged vectors.&lt;/p&gt;
&lt;p&gt;I decided to make this downloadable file of English-only vectors as a convenience, because it would be the format that looked most like a drop-in replacement for word2vec's data. But the English-only format is not a format that we use anywhere. We test our vectors, but we don't test reimporting them from all the files we exported, so that caused a bug in the export to go unnoticed.&lt;/p&gt;
&lt;p&gt;The English-only vectors ended up labeling the rows with the wrong English words, unfortunately, making the data they contained meaningless. If you use the &lt;a href="http://conceptnet.s3.amazonaws.com/downloads/2017/numberbatch/numberbatch-17.04.txt.gz"&gt;multilingual version&lt;/a&gt;, it was and still is fine.&lt;/p&gt;
&lt;p&gt;If you use the English-only vectors, we have a new Numberbatch download, &lt;a href="http://conceptnet.s3.amazonaws.com/downloads/2017/numberbatch/numberbatch-en-17.04b.txt.gz"&gt;version 17.04b&lt;/a&gt;, that should fix the problem.&lt;/p&gt;
&lt;p&gt;I apologize for the erroneous data, and for the setback this may have caused for anyone who is just trying to use the best word vectors they can. Thank you to the users on the &lt;a href="https://groups.google.com/forum/#!forum/conceptnet-users"&gt;conceptnet-users mailing list&lt;/a&gt; who drew my attention to the problem.&lt;/p&gt;&lt;/div&gt;</description><category>Numberbatch</category><category>Word embeddings</category><guid>http://concepts.arborelia.net/posts/2017/bugfix-our-english-only-word-vectors-contained-the-wrong-data/</guid><pubDate>Mon, 19 Jun 2017 15:54:19 GMT</pubDate></item><item><title>ConceptNet Numberbatch 17.04: better, less-stereotyped word vectors</title><link>http://concepts.arborelia.net/posts/2017/conceptnet-numberbatch-17-04-better-less-stereotyped-word-vectors/</link><dc:creator>Robyn Speer</dc:creator><description>&lt;div&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Word embeddings&lt;/em&gt; or &lt;em&gt;word vectors&lt;/em&gt; are a way for computers to understand what words mean in text written by people. The goal is to represent words as lists of numbers, where small changes to the numbers represent small changes to the meaning of the word. This is a technique that helps in building AI algorithms for natural language understanding -- using word vectors, the algorithm can compare words by what they mean, not just by how they’re spelled.&lt;/p&gt;
&lt;p&gt;But the news that's breaking everywhere about word vectors is that they also represent the worst parts of what people mean. Stereotypes and prejudices are baked into what the computer believes to be the meanings of words. To put it bluntly, the computer learns to be sexist and racist, because it learns from what people say.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://youtu.be/Y-Elr5K2Vuo"&gt;&lt;img class=" size-medium wp-image-852 aligncenter" src="https://conceptnetblog.files.wordpress.com/2017/04/i-learned-it-by-watching-you1.png?w=300" alt="i-learned-it-by-watching-you" width="300" height="216"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are many articles you could read for background on the problem, including:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://science.sciencemag.org/content/356/6334/183.full"&gt;Semantics derived automatically from language corpora contain human-like biases&lt;/a&gt;, by Aylin Caliskan, Joanna J. Bryson, and Arvind Narayanan, published in &lt;em&gt;Science&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://arxiv.org/pdf/1607.06520.pdf"&gt;Man is to Computer Programmer as Woman is to Homemaker? Debiasing word embeddings&lt;/a&gt;, by Tolga Bolukbasi et al., working with Microsoft Research&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.buzzfeed.com/nidhisubbaraman/robot-racism-through-language"&gt;Scientists Taught A Robot Language. It Immediately Turned Racist.&lt;/a&gt;, written by Nidhi Subbaraman for that paragon of science reporting, &lt;em&gt;BuzzFeed&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We want to avoid letting computers be awful to people just because people are awful to people. We want to provide word vectors that are not just the technical best, but also morally good. So we're releasing a new version of &lt;a href="http://blog.conceptnet.io/2016/05/25/conceptnet-numberbatch-a-new-name-for-the-best-word-embeddings-you-can-download/"&gt;ConceptNet Numberbatch&lt;/a&gt; that has been post-processed to counteract several kinds of biases and stereotypes.&lt;/p&gt;
&lt;p&gt;If you use word vectors in your machine learning and the &lt;a href="http://blog.conceptnet.io/2017/03/02/how-luminoso-made-conceptnet-into-the-best-word-vectors-and-won-at-semeval/"&gt;state-of-the-art accuracy&lt;/a&gt; of ConceptNet Numberbatch hasn't convinced you to switch from word2vec or GloVe, we hope that built-in de-biasing makes a compelling case. Machine learning is better when your machine is less prone to learning to be a jerk.&lt;/p&gt;
&lt;p&gt;How do we evaluate that we've made ConceptNet Numberbatch less prejudiced than competing systems? There seem to be no standardized evaluations for this yet. But we've created some evaluations based on the data from these recent papers. We're making it a part of the ConceptNet build process to automatically de-bias Numberbatch and evaluate how successful that de-biasing was.&lt;/p&gt;
&lt;p&gt;The Bolukbasi et al. paper describes how to counteract gender bias, and by adapting the techniques of that paper, we can reduce the gender bias we observe to almost nothing. Biases regarding race, ethnicity, and religion are harder to define, and therefore harder to remove, but it's important to make progress on this anyway.&lt;/p&gt;
&lt;p&gt;The graph you see below shows what we've done so far. The y-axis is a scale that we came up with involving the dot products between word vectors and their possible stereotypes: closer to zero is better. The brown bar, "ConceptNet Numberbatch 17.04", is the de-biased system we're releasing. (The version number represents the date, April 2017.)&lt;/p&gt;
&lt;p&gt;&lt;img class="alignnone size-full wp-image-707" src="http://concepts.arborelia.net/2017/04/bias-graph.png" alt="bias-graph.png"&gt;&lt;/p&gt;
&lt;p&gt;We're not trying to say we've solved the problem, but we can conclude that we've made the problem smaller. Keep in mind that this evaluation itself will likely change in the future, as we gain a better understanding of how to measure bias.&lt;/p&gt;
&lt;p&gt;In dealing with machine-learning bias, there's the concern that removing the bias could also cause changes that remove accuracy. But we've found that the change is negligible: about 1% of the overall result, much smaller than the error bars. Here's the updated evaluation graph. The y-axis is the Spearman correlation with gold standard data (higher is better). The evaluations here are for English words only -- Numberbatch covers many more languages, but the systems we're comparing to don't.&lt;/p&gt;
&lt;p&gt;&lt;img class="alignnone size-full wp-image-772" src="http://concepts.arborelia.net/2017/04/eval-graph.png" alt="eval-graph"&gt;&lt;/p&gt;
&lt;p&gt;ConceptNet Numberbatch is already so much more accurate than any other released system that it can lose 1% of its accuracy for a good cause. If you want 1% more accuracy out of your word vectors, I suggest focusing on improving a knowledge graph, not putting back the stereotypes.&lt;/p&gt;
&lt;h3&gt;Systems we compare to&lt;/h3&gt;

&lt;p&gt;In the graphs above, "word2vec Google News" is the popular system from 2013-2014 that many people go to when they think "oh hey I need some word vectors". Its continued relevance is largely due to the fact that it lets you use data that's learned from Google's large corpus of news, a very nice corpus which you can't actually have. We use its results as an input to ConceptNet Numberbatch.&lt;/p&gt;
&lt;p&gt;"GloVe 1.2 840B" is a system from Stanford that is better in some cases, and learns from reading the whole Web via the Common Crawl. It seems to have some fixable problems with the scaling of its features. "GloVe renormalized" is &lt;a href="http://blog.conceptnet.io/2016/05/19/an-introduction-to-the-conceptnet-vector-ensemble/"&gt;Luminoso's improvement on GloVe&lt;/a&gt;, which we also use as an input.&lt;/p&gt;
&lt;p&gt;"fastText enWP (without OOV)" is &lt;a href="https://github.com/facebookresearch/fastText/blob/master/pretrained-vectors.md"&gt;Facebook's word vectors trained on the English Wikipedia&lt;/a&gt;, with the disclaimer that their accuracy &lt;em&gt;should&lt;/em&gt; be better than what we show here. Facebook's fastText comes with a strategy for handling out-of-vocabulary words, like Numberbatch does. But the data they make available for applying that strategy is in an undocumented binary format that I haven't deciphered yet.&lt;/p&gt;
&lt;h3&gt;Gender analogies and gender bias&lt;/h3&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;The word2vec authors (Tomas Mikolov et al.) showed that there was structure in the meanings of the vectors that word2vec learned, allowing you to do arithmetic with word meanings. If you've read anything about word vectors before, you're probably tired of this example by now, but it will make a good illustration: you can take the vector for the word “king”, then subtract “man” and add “woman” to it, and the result is close to the vector for the word “queen”. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:400;"&gt;Let’s de-mystify how that happens a bit. &lt;/span&gt;&lt;span style="font-weight:400;"&gt;Word2vec&lt;/span&gt;&lt;span style="font-weight:400;"&gt; has read many billions of words (in particular, words in articles on Google News) and learned about what contexts they appear in. The operation “king - man + woman” is essentially asking to find a word that’s used similarly to the word “king”, but one whose context is more like the word “woman” than the word “man”. For example, a word that’s like “king” but appears with the words “she” and “her”. The clear answer is “queen”.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:400;"&gt;So the vectors learned by word2vec (and many later systems) can express analogy problems as mathematical equations:&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:center;"&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;woman&lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight:400;"&gt; - &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;man &lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight:400;"&gt;≈ &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;queen&lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight:400;"&gt; - &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;king&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;This is remarkably similar to the way that analogies are presented in, for example, an English class, or a standardized test such as the Miller Analogies Test or former SAT tests:&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:center;"&gt;&lt;span style="font-weight:400;"&gt;woman : man :: queen : king&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;Evaluating analogies like these led researchers to wonder what other analogies could be created in this system by adding “woman” and subtracting “man” to a word, or vice versa. And some of the results revealed a problem.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:400;"&gt;In the following examples, the word2vec system was given the first three words of an analogy, and asked for the word in its vocabulary that best completed the equation. The results revealed gender biases that had been baked into the system:&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:center;"&gt;&lt;span style="font-weight:400;"&gt;man : woman :: shopkeeper : &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;housewife&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p style="text-align:center;"&gt;&lt;span style="font-weight:400;"&gt;man : woman :: carpentry : &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;sewing&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p style="text-align:center;"&gt;&lt;span style="font-weight:400;"&gt;man : woman :: pharmaceuticals : &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;cosmetics&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;I wonder if the &lt;a href="http://blog.conceptnet.io/2016/06/01/cramming-for-the-test-set-we-need-better-ways-to-evaluate-analogies/"&gt;excessive focus on Mikolov et al.'s analogy evaluation&lt;/a&gt; has exacerbated the problem. When a system is asked repeatedly to make analogies of the form &lt;em&gt;male word : female word :: other male word : other female word&lt;/em&gt;, and it's evaluated on this and its knowledge of geography and not much else, is it any surprise that we end up with systems that amplify stereotypes that distinguish women and men?&lt;/p&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;The problem is not just a theoretical problem that shows up when playing with analogies. Word embeddings are actively used in many different fields of natural language processing. A system that searches résumés for people with particular programming skills could end up ranking women lower: the phrase “she developed software for…”  would be a worse match for the classifier’s expectations than “he developed software for…”.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This is one of the biases that we strive to remove, and the one we are most successful at removing, as described later in this post.&lt;/p&gt;
&lt;h3&gt;Word embeddings contain ethnic biases, too&lt;/h3&gt;

&lt;p&gt;The work on understanding and removing gender biases has been published for a while. But while working on this, I noticed that the data also contained significant racial and ethnic biases, which it seemed nobody was talking about. Just recently, the Caliskan et al. article came out and provided some needed illumination on the issue.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-weight:400;"&gt;I had tried building an algorithm for sentiment analysis based on word embeddings -- evaluating how much people like certain things based on what they say about them. When I applied it to restaurant reviews, I found it was ranking Mexican restaurants lower. The reason was not reflected in the star ratings or actual text of the reviews. It’s not that people don’t like Mexican food. The reason was that the system had learned the word “Mexican” from reading the Web.&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;If a restaurant were described as doing something “illegal”, that would be a pretty negative statement about the restaurant, right? But the Web contains lots of text where people use the word “Mexican” disproportionately along with the word “illegal”, particularly to associate “Mexican immigrants” with “illegal immigrants”. The system ends up learning that “Mexican” means something similar to “illegal”, and so it must mean something bad.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;The tests I implemented for ethnic bias are to take a list of words, such as "white", "black", "Asian", and "Hispanic", and find which one has the strongest correlation with each of a list of positive and negative words, such as "cheap", "criminal", "elegant", and "genius". I did this again with a fine-grained version that lists hundreds of words for ethnicities and nationalities, and thus is more difficult to get a low score on, and again with what may be the trickiest test of all, comparing words for different religions and spiritual beliefs.&lt;/p&gt;

&lt;p style="text-align:left;"&gt;In these tests, for each positive and negative word, I find the group-of-people word that it's most strongly associated with, and compare that to the average. The difference is the bias for that word, and in the end it's averaged over all the positive and negative words. This appears in the graphs as "Ethnic bias (coarse)", "Ethnic bias (fine)", and "Religious bias".&lt;/p&gt;

&lt;p style="text-align:left;"&gt;Note that it's infeasible to reach 0 on this scale -- words for groups of people will necessarily have &lt;em&gt;some&lt;/em&gt; different associations. Even random differences between the words would give non-zero results. This is one reason I don't consider the scale to be final. I'd like to make one that works like the gender-bias scale, where reaching 0 is attainable and desirable.&lt;/p&gt;

&lt;p style="text-align:left;"&gt;The &lt;em&gt;Science&lt;/em&gt; article uncovers racial biases in a different way: it looks for different associations with predominantly-black names, such as "Deion", "Jamel", "Shereen", and "Latisha", versus predominantly-white names, such as "Amanda", "Courtney", "Adam", and "Harry". I incorporated a version of this (and also added some predominantly Hispanic and Islamic names) as another test, shown on the graph as "Bias from names".&lt;/p&gt;

&lt;p style="text-align:left;"&gt;In ConceptNet Numberbatch, we've extended Bolukbasi's de-biasing method to cover multiple types of prejudices, including ethnic and religious. This, too, is discussed below in the "What we've done" section.&lt;/p&gt;

&lt;h3 style="text-align:left;"&gt;Porn biases&lt;/h3&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;While we’re talking about the biases that an algorithm gets from reading the Web, let’s talk about another large influence: a &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;lot&lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight:400;"&gt; of the Web is porn.&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;A system that reads the text of pages sampled from the Web is going to read a lot of the text of porn pages. As such, it is going to end up learning associations for many kinds of words, such as “girlfriend”, “teen”, and “Asian”, that would be very inappropriate to put into production in a machine learning system.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;Many of these associated terms, such as “slut”, are negative in connotation. This causes gender bias, ethnic bias, and more. When countering all of these biases, we need to make sure that people are not associated with degrading terminology because of their gender, ethnicity, age, or sexual orientation. This is another aspect of words that we strive to de-bias.&lt;/span&gt;&lt;/p&gt;

&lt;h3 style="text-align:left;"&gt;How to fix machine-learning biases&lt;/h3&gt;

&lt;p&gt;&lt;span style="font-weight:400;"&gt;Biases and prejudices are clearly a big problem in machine learning, and at least some machine learning researchers are doing something about it. I’ve seen two major approaches to fixing biases, and as a shorthand, I’ll call them “Google-style” and “Microsoft-style” fixes, though I’m aware that these are just one project from each company and probably don’t represent a company-wide plan. The main difference is at what stage of the process you try to remove biases.&lt;/span&gt;&lt;/p&gt;
&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;What I call “Google-style” de-biasing is described in a post on the Google Research Blog, “&lt;/span&gt;&lt;a href="https://research.googleblog.com/2016/10/equality-of-opportunity-in-machine.html"&gt;&lt;span style="font-weight:400;"&gt;Equality of Opportunity in Machine Learning&lt;/span&gt;&lt;/a&gt;&lt;span style="font-weight:400;"&gt;”. In this view, the data is what it is; if the data is unfair, it’s a reflection of the world being unfair. So the goal is to identify the point at which a machine learning tool makes a decision that affects someone (their example is a classifier that decides whether to grant a loan), and de-bias the actual decision that it makes, providing equal opportunity regardless of what the system learned from its data.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;They caution against “fairness through unawareness”, the attempt to produce a system that’s unbiased just because it’s not told about attributes such as gender or race, because machine learning is great at picking up on correlated patterns that could be a proxy for gender or race.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;Google’s approach is a principled and reasonable approach to take, especially in a workplace that venerates data above all else. But it sounds like it involves a large and never-ending amount of programmer effort to ensure that biased data leads to unbiased decisions. I have to wonder how many of Google’s products that use machine learning really have a working “equal opportunity filter” on their output.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;In the “Microsoft-style” approach, when your data is biased against some group of people, you change the data until it’s more fair, which should help to de-bias anything you do with that data. This approach avoids “unawareness” because it adjusts &lt;/span&gt;&lt;i&gt;&lt;span style="font-weight:400;"&gt;all&lt;/span&gt;&lt;/i&gt;&lt;span style="font-weight:400;"&gt; the data that’s correlated with the identified bias. I call this “Microsoft-style” because it’s based on the Microsoft Research paper (Bolukbasi et al.) that I linked to.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;To remove a gender bias from word embeddings, for example, you can collect many examples of word pairs that are gender-biased and shouldn’t be (such as “doctor” vs. “nurse”), and use them to find the combination of word-embedding components that are responsible for gender bias. Then you mathematically adjust the components so that that combination comes out to 0.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;&lt;span style="font-weight:400;"&gt;It’s not quite that simple -- doing exactly what I said could result in a system that’s unbiased because it has no idea what gender is, which would harm its ability to understand words that carry actual information about gender (such as “she” or “uncle”). You need to also have examples of words that are appropriate to distinguish by gender, such as “he” and “she”, or “aunt” and “uncle”. You then train the system to find the right balance between destroying biased assumptions and preserving useful information about gender.&lt;/span&gt;&lt;/p&gt;

&lt;p style="text-align:left;"&gt;To summarize the effects of these approaches, I would say that Microsoft-style de-biasing is more transferable between different tasks, but Google-style lets you positively demonstrate that certain things about your system are fair, if you use it consistently. If you control your machine learning pipeline from end to end, from source data to the point where you make a decision, I would say you should do both.&lt;/p&gt;

&lt;h3 style="text-align:left;"&gt;What we've done&lt;/h3&gt;

&lt;p&gt;In the new release of &lt;a href="https://github.com/commonsense/conceptnet-numberbatch"&gt;ConceptNet Numberbatch&lt;/a&gt;, we adapt one of the "Microsoft-style" techniques from Bolukbasi et al., but we remove many types of biases, not just one.&lt;/p&gt;
&lt;p&gt;The process goes like this:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Classify words according to the &lt;em&gt;appropriateness&lt;/em&gt; of a distinction. For example, "mother" vs. "father" contains an appropriate gender distinction that we shouldn't change. "Homemaker" vs. "programmer" contains an inappropriate distinction. Bolukbasi provided some nice crowd-sourced data about what people consider appropriate.&lt;/li&gt;
    &lt;li&gt;Adjust the word vectors for words on the "inappropriate" side algebraically, so that the distinction they shouldn't be making comes out to zero.&lt;/li&gt;
    &lt;li&gt;Evaluate how successful we were at removing the bias, by testing it on a different set of words than the ones we used to find the bias.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The graph below shows the gender bias that we aim to remove. Words are plotted according to how much they're associated with male words (on the left) or female words (on the right), and according to whether our classifier says this association is appropriate (on the top) or inappropriate (on the bottom).&lt;/p&gt;
&lt;p&gt;&lt;img class="alignnone size-full wp-image-819" src="http://concepts.arborelia.net/2017/04/cn-gender-biased.png" alt="cn-gender-biased.png" width="732" height="701"&gt;&lt;/p&gt;
&lt;p&gt;And here's what the graph looks like after de-biasing. The inappropriate gender distinctions have been set to nearly zero.&lt;/p&gt;
&lt;p&gt;&lt;img class="alignnone size-full wp-image-822" src="http://concepts.arborelia.net/2017/04/cn-gender-debiased.png" alt="cn-gender-debiased" width="724" height="701"&gt;&lt;/p&gt;
&lt;p&gt;We use similar steps to remove biased associations with different races, ethnicities, and religions. Because we don't have nice crowd-sourced data for exactly what should be removed in those cases, we instead aim to de-correlate them with words representing positive and negative sentiment.&lt;/p&gt;
&lt;p&gt;We hope we're pushing word vectors away from biases and prejudices, and toward systems that don't think of you any differently whether you're named Stephanie or Shanice or Santiago or Syed.&lt;/p&gt;
&lt;h3&gt;Using the results&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/commonsense/conceptnet-numberbatch"&gt;ConceptNet Numberbatch 17.04&lt;/a&gt; is out now, with the vectors available for research into text understanding and classification. The format is designed so they can be used as a replacement for word2vec vectors.&lt;/p&gt;
&lt;p&gt;In &lt;a href="http://www.luminoso.com/"&gt;Luminoso&lt;/a&gt; products, we use a version of ConceptNet Numberbatch that's adapted to our text-understanding pipeline as a starting point, providing general background knowledge about what words mean. Numberbatch represents what Luminoso knows before it learns about your domain, and allows it to quickly learn to understand words from a particular domain because it doesn't have to learn an entire language from scratch.&lt;/p&gt;
&lt;p&gt;Our next practical step is to incorporate the newest Numberbatch into Luminoso, with both the SemEval accuracy improvements and the de-biasing.&lt;/p&gt;
&lt;p&gt;In further research, we aim to refine how we measure these different kinds of biases. One improvement would be to measure biases in languages besides English. Numberbatch vectors are aligned across different languages, so the de-biasing we performed should affect all languages, but it will be important to test this with some multilingual data.&lt;/p&gt;&lt;/div&gt;</description><category>ConceptNet</category><category>NLP fairness</category><category>Numberbatch</category><guid>http://concepts.arborelia.net/posts/2017/conceptnet-numberbatch-17-04-better-less-stereotyped-word-vectors/</guid><pubDate>Mon, 24 Apr 2017 19:17:40 GMT</pubDate></item></channel></rss>