scala - 在 Spark 中加载 Word2Vec 模型

标签 scala apache-spark word2vec

是否可以将预训练(二进制)模型加载到 Spark(使用 scala)?我尝试加载由谷歌生成的二进制模型之一,如下所示:

    import org.apache.spark.mllib.feature.{Word2Vec, Word2VecModel}


    val model = Word2VecModel.load(sc, "GoogleNews-vectors-negative300.bin")

但无法找到元数据目录。我还创建了该文件夹并在其中附加了二进制文件,但无法解析它。我没有找到这个问题的任何包装。

最佳答案

我编写了一个快速函数,将 google news 预训练模型加载到 Spark word2vec 模型中。享受吧。

def loadBin(file: String) = {
  def readUntil(inputStream: DataInputStream, term: Char, maxLength: Int = 1024 * 8): String = {
    var char: Char = inputStream.readByte().toChar
    val str = new StringBuilder
    while (!char.equals(term)) {
      str.append(char)
      assert(str.size < maxLength)
      char = inputStream.readByte().toChar
    }
    str.toString
  }
  val inputStream: DataInputStream = new DataInputStream(new GZIPInputStream(new FileInputStream(file)))
  try {
    val header = readUntil(inputStream, '\n')
    val (records, dimensions) = header.split(" ") match {
      case Array(records, dimensions) => (records.toInt, dimensions.toInt)
    }
    new Word2VecModel((0 until records).toArray.map(recordIndex => {
      readUntil(inputStream, ' ') -> (0 until dimensions).map(dimensionIndex => {
        java.lang.Float.intBitsToFloat(java.lang.Integer.reverseBytes(inputStream.readInt()))
      }).toArray
    }).toMap)
  } finally {
    inputStream.close()
  }
}

关于scala - 在 Spark 中加载 Word2Vec 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866703/

相关文章:

json - Spark 将 StructType/JSON 转换为字符串

mysql - [运行时异常 : SqlMappingError(No rows when expecting a single one)]

python - Gensim 中的 FastText

machine-learning - 使用weka对word2vec进行分类

python - 使用什么命令来检查 scala REPL 中的实例?

scala - 如何同步 Scala 项目的 Intellij 和 sbt 构建

scala - 为什么 Spark 应用程序以 "ClassNotFoundException: Failed to find data source: jdbc"作为带有 sbt 程序集的 uber-jar 失败?

apache-spark - Zeppelin 0.8.2 - localRepoPath 应该有一个值

scala - saveAsNewAPIHadoopFile() 在用作输出格式时出错

machine-learning - Glove 和 word2vec 之间的主要区别是什么?