GitHub user flyjy opened a pull request:

    https://github.com/apache/spark/pull/11812

    [SPARK-13289][MLLIB] Fix infinite distances between word vectors in 
Word2VecModel

    ## What changes were proposed in this pull request?
    
    This PR fixes the bug that generates infinite distances between word 
vectors. For example,
    
    Before this PR, we have
    ```
    val synonyms = model.findSynonyms("who", 40)
    ```
    will give the following results: 
    ```
    to Infinity
    and Infinity
    that Infinity
    with Infinity
    ```
    With this PR, the distance between words is a value between 0 and 1, as 
follows:
    ```
    scala> model.findSynonyms("who", 10)
    res0: Array[(String, Double)] = 
Array((Harvard-educated,0.5253688097000122), (ex-SAS,0.5213794708251953), 
(McMutrie,0.5187736749649048), (fellow,0.5166833400726318), 
(businessman,0.5145374536514282), (American-born,0.5127736330032349), 
(British-born,0.5062344074249268), (gray-bearded,0.5047978162765503), 
(American-educated,0.5035858750343323), (mentored,0.49849334359169006))
    
    scala> model.findSynonyms("king", 10)
    res1: Array[(String, Double)] = Array((queen,0.6787897944450378), 
(prince,0.6786158084869385), (monarch,0.659771203994751), 
(emperor,0.6490438580513), (goddess,0.643266499042511), 
(dynasty,0.635733425617218), (sultan,0.6166239380836487), 
(pharaoh,0.6150713562965393), (birthplace,0.6143025159835815), 
(empress,0.6109727025032043))
    
    scala> model.findSynonyms("queen", 10)
    res2: Array[(String, Double)] = Array((princess,0.7670737504959106), 
(godmother,0.6982434988021851), (raven-haired,0.6877717971801758), 
(swan,0.684934139251709), (hunky,0.6816608309745789), 
(Titania,0.6808111071586609), (heroine,0.6794036030769348), 
(king,0.6787897944450378), (diva,0.67848801612854), 
(lip-synching,0.6731793284416199))
    ```
    
    ### There are two places changed in this PR:
    - Normalize the word vector to avoid overflow when calculating inner 
product between word vectors. This also simplifies the distance calculation, 
since the word vectors only need to be normalized once.
    - Scale the learning rate by number of iteration, to be consistent with 
Google Word2Vec implementation
    
    
    ## How was this patch tested?
    
    Use word2vec to train text corpus, and run model.findSynonyms() to get the 
distances between word vectors.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/flyjy/spark TVec

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/11812.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #11812
    
----
commit 6915f8a7f36064a34daa9488bc0ff0f48f7e0393
Author: Junyang <[email protected]>
Date:   2016-03-17T17:07:58Z

    fixe bugs

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to