使用带有text2vec的预训练模型?

时间:2018-05-28 15:18:43

标签: r nlp word2vec text2vec

我想使用带有text2vec的预训练模型。我的理解是,这里的好处是这些模型已经在大量数据上进行了训练,例如: Google News Model

阅读text2vec documentation看起来像入门代码读取文本数据然后用它训练模型:

library(text2vec)
text8_file = "~/text8"
if (!file.exists(text8_file)) {
  download.file("http://mattmahoney.net/dc/text8.zip", "~/text8.zip")
  unzip ("~/text8.zip", files = "text8", exdir = "~/")
}
wiki = readLines(text8_file, n = 1, warn = FALSE)

然后文档继续展示如何创建令牌和词汇:

# Create iterator over tokens
tokens <- space_tokenizer(wiki)
# Create vocabulary. Terms will be unigrams (simple words).
it = itoken(tokens, progressbar = FALSE)
vocab <- create_vocabulary(it)
vocab <- prune_vocabulary(vocab, term_count_min = 5L)
# Use our filtered vocabulary
vectorizer <- vocab_vectorizer(vocab)
# use window of 5 for context words
tcm <- create_tcm(it, vectorizer, skip_grams_window = 5L)

然后,这看起来像是适合模型的步骤:

glove = GlobalVectors$new(word_vectors_size = 50, vocabulary = vocab, x_max = 10)
glove$fit(tcm, n_iter = 20)

我的问题是,熟悉谷歌预训练的word2vec模型是否可以使用,而无需依靠我自己的词汇或我自己的本地设备来训练模型?如果是,我怎么能读它并在r?

中使用它

我想我在这里误解或遗漏了什么?我可以使用text2vec执行此任务吗?

2 个答案:

答案 0 :(得分:1)

目前text2vec并未提供下载/操作预训练字嵌入的任何功能。 我有一个草稿,可以将这些实用程序添加到下一个版本中。

但另一方面,您可以使用标准R工具轻松手动完成。例如,这里是如何阅读fasttext向量:

con = url("https://s3-us-west-1.amazonaws.com/fasttext-vectors/word-vectors-v2/cc.af.300.vec.gz", "r")
con = gzcon(con)
wv = readLines(con, n = 10)

然后你只需要解析它 - strsplitrbind是你的朋友。

答案 1 :(得分:0)

这有点晚了,但其他用户可能会感兴趣。泰勒·范·安妮(Taylor Van Anne)在此处提供了一个小教程,该教程如何将预训练的GloVe矢量模型与text2vec一起使用: https://gist.github.com/tjvananne/8b0e7df7dcad414e8e6d5bf3947439a9