Skip to content

Commit 6df0dcb

Browse files
committed
add Word2Vec documentation
1 parent 73ab7f1 commit 6df0dcb

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

docs/mllib-feature-extraction.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,58 @@ displayTitle: <a href="mllib-guide.html">MLlib</a> - Feature Extraction
99

1010
## Word2Vec
1111

12+
Wor2Vec computes distributed vector representation of words. The main advantage of the distributed representations is that similar words are close in the vector space, which makes generalization to novel patterns easier and model estimation more robust. Distributed vector representation is showed to be useful in many natural language processing applications such as named entity recognition, disambiguation, parsing, tagging and machine translation.
13+
14+
### Model
15+
In our implementation of Word2Vec, we used skip-gram model. The training objective of skip-gram is to learn word vector representations that are good at predicting its context in the same sentence. Mathematically, given a sequence of training words `$w_1, w_2, \dots, w_T$`, the objective of the skip-gram model is to maximize the average log-likelihood
16+
`\[
17+
\frac{1}{T} \sum_{t = 1}^{T}\sum_{j=-k}^{j=k} \log p(w_{t+j} | w_t)
18+
\]`
19+
where $k$ is the size of the training window.
20+
21+
In the skip-gram model, every word $w$ is associated with two vectors $u_w$ and $v_w$ which are vector representations of $w$ as word and context respectively. The probability of correctly predicting word $w_i$ given word $w_j$ is determined by the softmax model, which is
22+
`\[
23+
p(w_i | w_j ) = \frac{\exp(u_{w_i}^{\top}v_{w_j})}{\sum_{l=1}^{V} \exp(u_l^{\top}v_{w_j})}
24+
\]`
25+
where $V$ is the vocabulary size.
26+
27+
The skip-gram model with softmax is expensive because the cost of computing $\log p(w_i | w_j)$
28+
is proportional to $V$, which can be easily in order of millions. To speed up Word2Vec training, we used hierarchical softmax, which reduced the complexity of computing of $\log p(w_i | w_j)$ to
29+
$O(\log(V))$
30+
31+
### Example
32+
33+
The example below demonstrates how to load a text file, parse it as an RDD of `Seq[String]` and then construct a `Word2Vec` instance with specified parameters. Then we fit a Word2Vec model with the input data. Finally, we display the top 40 similar words to the specified word.
34+
35+
<div class="codetabs">
36+
<div data-lang="scala">
37+
{% highlight scala %}
38+
import org.apache.spark._
39+
import org.apache.spark.rdd._
40+
import org.apache.spark.SparkContext._
41+
import org.apache.spark.mllib.feature.Word2Vec
42+
43+
val input = sc.textFile().map(line => line.split(" ").toSeq)
44+
val size = 100
45+
val startingAlpha = 0.025
46+
val numPartitions = 1
47+
val numIterations = 1
48+
49+
val word2vec = new Word2Vec()
50+
.setVectorSize(size)
51+
.setSeed(42L)
52+
.setNumPartitions(numPartitions)
53+
.setNumIterations(numIterations)
54+
55+
val model = word2vec.fit(input)
56+
57+
val vec = model.findSynonyms("china", 40)
58+
59+
for((word, cosineSimilarity) <- vec) {
60+
println(word + " " + cosineSimilarity.toString)
61+
}
62+
{% endhighlight %}
63+
</div>
64+
</div>
65+
1266
## TFIDF

0 commit comments

Comments
 (0)