在 text2vec R 包中准备词嵌入
Preparing word embeddings in text2vec R package
基于 text2vec 包的小插图,提供了一个创建单词的示例 embedding.The wiki 数据被标记化,然后创建术语共现矩阵 (TCM),用于使用 glove 创建单词嵌入包中提供的功能。
我想为包中提供的电影评论数据构建词嵌入。我的问题是:
- 我是否需要将所有电影评论折叠成一个长字符串,然后进行分词。
这将导致 2 条评论之间的边界标记同时出现,这是没有意义的。
**vignettes code:**
library(text2vec)
library(readr)
temp <- tempfile()
download.file('http://mattmahoney.net/dc/text8.zip', temp)
wiki <- read_lines(unz(temp, "text8"))
unlink(temp)
# Create iterator over tokens
tokens <- strsplit(wiki, split = " ", fixed = T)
# Create vocabulary. Terms will be unigrams (simple words).
vocab <- create_vocabulary(itoken(tokens))
vocab <- prune_vocabulary(vocab, term_count_min = 5L)
# We provide an iterator to create_vocab_corpus function
it <- itoken(tokens)
# Use our filtered vocabulary
vectorizer <- vocab_vectorizer(vocab,
# don't vectorize input
grow_dtm = FALSE,
# use window of 5 for context words
skip_grams_window = 5L)
tcm <- create_tcm(it, vectorizer)
fit <- glove(tcm = tcm,
word_vectors_size = 50,
x_max = 10, learning_rate = 0.2,
num_iters = 15)
我感兴趣的开发词嵌入的数据可以得到如下:
library(text2vec)
data("movie_review")
不,您不需要串联评论。您只需要从正确的令牌迭代器构造 tcm
:
library(text2vec)
data("movie_review")
tokens = movie_review$review %>% tolower %>% word_tokenizer
it = itoken(tokens)
# create vocabulary
v = create_vocabulary(it) %>%
prune_vocabulary(term_count_min = 5)
# create co-occurrence vectorizer
vectorizer = vocab_vectorizer(v, grow_dtm = F, skip_grams_window = 5)
现在我们需要重新初始化(对于稳定的 0.3 版本。对于 dev 0.4 不需要重新初始化迭代器):
it = itoken(tokens)
tcm = create_tcm(it, vectorizer)
适合模特:
fit <- glove(tcm = tcm,
word_vectors_size = 50,
x_max = 10, learning_rate = 0.2,
num_iters = 15)
基于 text2vec 包的小插图,提供了一个创建单词的示例 embedding.The wiki 数据被标记化,然后创建术语共现矩阵 (TCM),用于使用 glove 创建单词嵌入包中提供的功能。 我想为包中提供的电影评论数据构建词嵌入。我的问题是:
- 我是否需要将所有电影评论折叠成一个长字符串,然后进行分词。
这将导致 2 条评论之间的边界标记同时出现,这是没有意义的。
**vignettes code:**
library(text2vec)
library(readr)
temp <- tempfile()
download.file('http://mattmahoney.net/dc/text8.zip', temp)
wiki <- read_lines(unz(temp, "text8"))
unlink(temp)
# Create iterator over tokens
tokens <- strsplit(wiki, split = " ", fixed = T)
# Create vocabulary. Terms will be unigrams (simple words).
vocab <- create_vocabulary(itoken(tokens))
vocab <- prune_vocabulary(vocab, term_count_min = 5L)
# We provide an iterator to create_vocab_corpus function
it <- itoken(tokens)
# Use our filtered vocabulary
vectorizer <- vocab_vectorizer(vocab,
# don't vectorize input
grow_dtm = FALSE,
# use window of 5 for context words
skip_grams_window = 5L)
tcm <- create_tcm(it, vectorizer)
fit <- glove(tcm = tcm,
word_vectors_size = 50,
x_max = 10, learning_rate = 0.2,
num_iters = 15)
我感兴趣的开发词嵌入的数据可以得到如下:
library(text2vec)
data("movie_review")
不,您不需要串联评论。您只需要从正确的令牌迭代器构造 tcm
:
library(text2vec)
data("movie_review")
tokens = movie_review$review %>% tolower %>% word_tokenizer
it = itoken(tokens)
# create vocabulary
v = create_vocabulary(it) %>%
prune_vocabulary(term_count_min = 5)
# create co-occurrence vectorizer
vectorizer = vocab_vectorizer(v, grow_dtm = F, skip_grams_window = 5)
现在我们需要重新初始化(对于稳定的 0.3 版本。对于 dev 0.4 不需要重新初始化迭代器):
it = itoken(tokens)
tcm = create_tcm(it, vectorizer)
适合模特:
fit <- glove(tcm = tcm,
word_vectors_size = 50,
x_max = 10, learning_rate = 0.2,
num_iters = 15)