diff options
author | Junyang <fly.shenjy@gmail.com> | 2016-04-30 10:16:35 +0100 |
---|---|---|
committer | Sean Owen <sowen@cloudera.com> | 2016-04-30 10:16:35 +0100 |
commit | 1192fe4cd2a934790dc1ff2d459cf380e67335b2 (patch) | |
tree | 9995dd068d3fb91fdb41061805b945dfb4365878 /python/pyspark | |
parent | 0368ff30dd55dd2127d4cb196898c7bd437e9d28 (diff) | |
download | spark-1192fe4cd2a934790dc1ff2d459cf380e67335b2.tar.gz spark-1192fe4cd2a934790dc1ff2d459cf380e67335b2.tar.bz2 spark-1192fe4cd2a934790dc1ff2d459cf380e67335b2.zip |
[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.
Author: Junyang <fly.shenjy@gmail.com>
Author: flyskyfly <fly.shenjy@gmail.com>
Closes #11812 from flyjy/TVec.
Diffstat (limited to 'python/pyspark')
-rw-r--r-- | python/pyspark/ml/feature.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/python/pyspark/ml/feature.py b/python/pyspark/ml/feature.py index 610d167f3a..1b059a7199 100644 --- a/python/pyspark/ml/feature.py +++ b/python/pyspark/ml/feature.py @@ -2186,13 +2186,14 @@ class Word2Vec(JavaEstimator, HasStepSize, HasMaxIter, HasSeed, HasInputCol, Has | c|[-0.3794820010662...| +----+--------------------+ ... - >>> model.findSynonyms("a", 2).show() - +----+-------------------+ - |word| similarity| - +----+-------------------+ - | b| 0.2505344027513247| - | c|-0.6980510075367647| - +----+-------------------+ + >>> from pyspark.sql.functions import format_number as fmt + >>> model.findSynonyms("a", 2).select("word", fmt("similarity", 5).alias("similarity")).show() + +----+----------+ + |word|similarity| + +----+----------+ + | b| 0.25053| + | c| -0.69805| + +----+----------+ ... >>> model.transform(doc).head().model DenseVector([0.5524, -0.4995, -0.3599, 0.0241, 0.3461]) |