Spark MLib Word2Vec Error: The vocabulary size should be > 0

Spark MLib Word2Vec Error: The vocabulary size should be > 0

我正在尝试使用 Spark 的 MLLib 实现词向量化。我正在按照给出的示例 here.

我有一堆句子想作为输入来训练模型。但是我不确定这个模型是接受句子还是只接受所有的单词作为一个字符串序列。

我的输入如下:

scala> v.take(5)
res31: Array[Seq[String]] = Array(List([WrappedArray(0_42)]), List([WrappedArray(big, baller, shoe, ?)]), List([WrappedArray(since, eliud, win, ,, quick, fact, from, runner, from, country, kalenjins, !, write, ., happy, quick, fact, kalenjins, location, :, kenya, (, kenya's, western, highland, rift, valley, ), population, :, 4, ., 9, million, ;, compose, 11, subtribes, language, :, kalenjin, ;, swahili, ;, english, church, :, christianity, ~, africa, inland, church, [, aic, ],, church, province, kenya, [, cpk, ],, roman, catholic, church, ;, islam, translation, :, kalenjin, translate, ", tell, ", formation, :, wwii, ,, gikuyu, tribal, member, wish, separate, create, identity, ., later, ,, student, attend, alliance, high, school, (, first, british, public, school, kenya, ), form, ...

但是当我尝试在此输入上训练我的 word2vec 模型时它不起作用。

scala> val word2vec = new Word2Vec()
word2vec: org.apache.spark.mllib.feature.Word2Vec = org.apache.spark.mllib.feature.Word2Vec@51567040

scala> val model = word2vec.fit(v)
java.lang.IllegalArgumentException: requirement failed: The vocabulary size should be > 0. You may need to check the setting of minCount, which could be large enough to remove all your words in sentences.

Word2Vec不以句子为输入吗?

您的输入是正确的。但是,Word2Vec 将自动删除未在词汇表中出现最少次数的单词(所有句子组合)。默认情况下,此值为 5。在您的情况下,很可能没有单词在您使用的数据中出现 5 次或更多次。

要更改所需的最小单词出现次数,请使用 setMinCount(),例如最小计数为 2:

val word2vec = new Word2Vec().setMinCount(2)