scala – 是否可以将word2vec预训练的可用矢量加载到spark中?
有没有办法将0700或
Glove’s预训练的矢量(模型)(例如
GoogleNews-vectors-negative300.bin.gz)加载到spark中并执行诸如从spark提供的findSynonyms等操作?或者我是否需要从头开始进行装载和操作?
在这篇文章Load Word2Vec model in Spark中,Tom Lous建议将bin文件转换为txt并从那里开始,我已经这样做了……但接下来是什么? 在我昨天发布的一个问题中,我得到了一个答案,Parquet格式的模型可以加载到spark中,因此我发布此问题以确保没有其他选项. 解决方法
免责声明:我很新兴,但下面至少对我有用.
诀窍是弄清楚如何从一组单词向量构造Word2VecModel,以及在尝试以这种方式创建模型时处理一些陷阱. 首先,将单词向量加载到Map中.例如,我将单词向量保存为镶木地板格式(在名为“wordvectors.parquet”的文件夹中),其中“term”列包含String字,“vector”列将向量作为数组[float]保存,我可以在Java中加载它: // Loads the dataset with the "term" column holding the word and the "vector" column // holding the vector as an array[float] Dataset<Row> vectorModel = pSpark.read().parquet("wordvectors.parquet"); //convert dataset to a map. Map<String,List<Float>> vectorMap = Arrays.stream((Row[])vectorModel.collect()) .collect(Collectors.toMap(row -> row.getAs("term"),row -> row.getList(1))); //convert to the format that the word2vec model expects float[] rather than List<Float> Map<String,float[]> word2vecMap = vectorMap.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey,entry -> (float[]) Floats.toArray(entry.getValue()))); //need to convert to scala immutable map because that's what word2vec needs scala.collection.immutable.Map<String,float[]> scalaMap = toScalaImmutableMap(word2vecMap); private static <K,V> scala.collection.immutable.Map<K,V> toScalaImmutableMap(Map<K,V> pFromMap) { final List<Tuple2<K,V>> list = pFromMap.entrySet().stream() .map(e -> Tuple2.apply(e.getKey(),e.getValue())) .collect(Collectors.toList()); Seq<Tuple2<K,V>> scalaSeq = JavaConverters.asScalaBufferConverter(list).asScala().toSeq(); return (scala.collection.immutable.Map<K,V>) scala.collection.immutable.Map$.MODULE$.apply(scalaSeq); } 现在您可以从头开始构建模型.由于Word2VecModel如何工作的怪癖,您必须手动设置矢量大小,并以一种奇怪的方式这样做.否则它默认为100,并且在尝试调用.transform()时出错.这是我发现有效的方法,不确定是否一切都是必要的: //not used for fitting,only used for setting vector size param (not sure if this is needed or if result.set is enough Word2Vec parent = new Word2Vec(); parent.setVectorSize(300); Word2VecModel result = new Word2VecModel("w2vmodel",new org.apache.spark.mllib.feature.Word2VecModel(scalaMap)).setParent(parent); result.set(result.vectorSize(),300); 现在,您应该能够像使用自我训练的模型一样使用result.transform(). 我没有测试过其他Word2VecModel函数,看它们是否正常工作,我只测试过.transform(). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |