我想使用一个预先训练的模型与text2vec。我的理解是,这里的好处是,这些模型已经接受了大量数据的培训,例如Google新闻模型。
读取text2vec 文档看上去像是刚开始的代码读取文本数据,然后用它来训练一个模型:
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来完成这个任务吗?
发布于 2018-05-28 17:52:13
目前,text2vec没有提供任何下载/操作预先训练过的单词嵌入的功能。我有一个草案来添加这样的实用程序到下一个版本。
但是在另一边,你可以很容易地用标准的R工具手动完成它。例如,这里是如何读取快速文本向量:
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)那么您只需要解析它-- strsplit和rbind是您的朋友。
发布于 2019-09-17 18:33:15
这有点晚,但可能会引起其他用户的兴趣。Taylor Van Anne提供了一个关于如何在GloVe:https://gist.github.com/tjvananne/8b0e7df7dcad414e8e6d5bf3947439a9中使用经过预训练的text2vec矢量模型的小教程。
https://stackoverflow.com/questions/50569420
复制相似问题