-
Notifications
You must be signed in to change notification settings - Fork 29k
[MLlib] [SPARK-2510]Word2Vec: Distributed Representation of Words #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8d6befe
0aafb1b
e4a04d3
57dc50d
2e92b59
720b5a3
6bcc8be
7efbb6f
1a8fb41
e93e726
384c771
c14da41
26a948d
e248441
2ba9483
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,9 @@ import org.apache.spark.SparkContext._ | |
| import org.apache.spark.mllib.linalg.Vector | ||
| import org.apache.spark.HashPartitioner | ||
|
|
||
| /** | ||
| * Entry in vocabulary | ||
| */ | ||
| private case class VocabWord( | ||
| var word: String, | ||
| var cn: Int, | ||
|
|
@@ -39,6 +42,9 @@ private case class VocabWord( | |
| var codeLen:Int | ||
| ) | ||
|
|
||
| /** | ||
| * Vector representation of word | ||
| */ | ||
| class Word2Vec( | ||
| val size: Int, | ||
| val startingAlpha: Double, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is word2vec sensitive to alpha? If not, we should try to expose less parameters to users.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. word2vec is sensitive to alpha. Larger alpha may generate meaningless result
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can suggest a reasonable default value in the doc. |
||
|
|
@@ -51,7 +57,8 @@ class Word2Vec( | |
| private val MAX_CODE_LENGTH = 40 | ||
| private val MAX_SENTENCE_LENGTH = 1000 | ||
| private val layer1Size = size | ||
|
|
||
| private val modelPartitionNum = 100 | ||
|
|
||
| private var trainWordsCount = 0 | ||
| private var vocabSize = 0 | ||
| private var vocab: Array[VocabWord] = null | ||
|
|
@@ -169,6 +176,7 @@ class Word2Vec( | |
| * Computes the vector representation of each word in | ||
| * vocabulary | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move |
||
| * @param dataset an RDD of strings | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need more information about what each record should be, a word, a sentence, or a paragraph? |
||
| * @return a Word2VecModel | ||
| */ | ||
|
|
||
| def fit(dataset:RDD[String]): Word2VecModel = { | ||
|
|
@@ -274,11 +282,14 @@ class Word2Vec( | |
| wordMap(i) = (word, vector) | ||
| i += 1 | ||
| } | ||
| val modelRDD = sc.parallelize(wordMap,100).partitionBy(new HashPartitioner(100)) | ||
| val modelRDD = sc.parallelize(wordMap, modelPartitionNum).partitionBy(new HashPartitioner(modelPartitionNum)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too wide |
||
| new Word2VecModel(modelRDD) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please call |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Word2Vec model | ||
| */ | ||
| class Word2VecModel (val _model:RDD[(String, Array[Double])]) extends Serializable { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| val model = _model | ||
|
|
@@ -292,22 +303,46 @@ class Word2VecModel (val _model:RDD[(String, Array[Double])]) extends Serializab | |
| blas.ddot(n, v1, 1, v2,1) / norm1 / norm2 | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a word to its vector representation | ||
| * @param word a word | ||
| * @return vector representation of word | ||
| */ | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove empty line |
||
| def transform(word: String): Array[Double] = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we use |
||
| val result = model.lookup(word) | ||
| if (result.isEmpty) Array[Double]() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should create an empty vector of the same size or throw an exception |
||
| else result(0) | ||
| } | ||
|
|
||
| /** | ||
| * Transforms an RDD to its vector representation | ||
| * @param dataset a an RDD of words | ||
| * @return RDD of vector representation | ||
| */ | ||
|
|
||
| def transform(dataset: RDD[String]): RDD[Array[Double]] = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type should be |
||
| dataset.map(word => transform(word)) | ||
| } | ||
|
|
||
| /** | ||
| * Find synonyms of a word | ||
| * @param word a word | ||
| * @param num number of synonyms to find | ||
| * @return array of (word, similarity) | ||
| */ | ||
| def findSynonyms(word: String, num: Int): Array[(String, Double)] = { | ||
| val vector = transform(word) | ||
| if (vector.isEmpty) Array[(String, Double)]() | ||
| else findSynonyms(vector,num) | ||
| } | ||
|
|
||
| /** | ||
| * Find synonyms of the vector representation of a word | ||
| * @param vector vector representation of a word | ||
| * @param num number of synonyms to find | ||
| * @return array of (word, similarity) | ||
| */ | ||
| def findSynonyms(vector: Array[Double], num: Int): Array[(String, Double)] = { | ||
| require(num > 0, "Number of similar words should > 0") | ||
| val topK = model.map( | ||
|
|
@@ -321,6 +356,15 @@ class Word2VecModel (val _model:RDD[(String, Array[Double])]) extends Serializab | |
| } | ||
|
|
||
| object Word2Vec extends Serializable with Logging { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| /** | ||
| * Train Word2Vec model | ||
| * @param input RDD of words | ||
| * @param size vectoer dimension | ||
| * @param startingAlpha initial learning rate | ||
| * @param window context words from [-window, window] | ||
| * @param minCount minimum frequncy to consider a vocabulary word | ||
| * @return Word2Vec model | ||
| */ | ||
| def train( | ||
| input: RDD[String], | ||
| size: Int, | ||
|
|
@@ -329,25 +373,4 @@ object Word2Vec extends Serializable with Logging { | |
| minCount: Int): Word2VecModel = { | ||
| new Word2Vec(size,startingAlpha, window, minCount).fit(input) | ||
| } | ||
|
|
||
| def main(args: Array[String]) { | ||
| if (args.length < 6) { | ||
| println("Usage: word2vec input size startingAlpha window minCount num") | ||
| sys.exit(1) | ||
| } | ||
| val conf = new SparkConf() | ||
| .setAppName("word2vec") | ||
|
|
||
| val sc = new SparkContext(conf) | ||
| val input = sc.textFile(args(0)) | ||
| val size = args(1).toInt | ||
| val startingAlpha = args(2).toDouble | ||
| val window = args(3).toInt | ||
| val minCount = args(4).toInt | ||
| val num = args(5).toInt | ||
| val model = train(input, size, startingAlpha, window, minCount) | ||
| val vec = model.findSynonyms("china", num) | ||
| for((w, dist) <- vec) logInfo(w.toString + " " + dist.toString) | ||
| sc.stop() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need more docs here, for example, link to the C implementation and the original papers for word2vec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and briefly explain what it does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, this is definitely an experimental feature. Please add
@Experimentaltag. Example:https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala#L45