首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >text2vec R包中字嵌入的准备

text2vec R包中字嵌入的准备
EN

Stack Overflow用户
提问于 2016-09-15 15:28:37
回答 1查看 2.9K关注 0票数 2

在text2vec包的基础上,给出了一个生成word embedding.The wiki数据的实例,并在此基础上建立了术语共现矩阵(TCM),利用包中提供的手套函数来创建词嵌入。我想为随包提供的电影评论数据构建word嵌入。我的问题是:

  1. 我是否需要将所有电影评论折叠成一个长字符串,然后进行标记化。

这将导致两个评审之间的边界标记同时发生,这是没有意义的。

代码语言:javascript
复制
**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)

我对开发word嵌入感兴趣的数据如下:

代码语言:javascript
复制
library(text2vec)
data("movie_review")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-15 21:49:20

不,你不需要连接评论。您只需从令牌上的正确迭代器构造tcm

代码语言:javascript
复制
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版本)。对于DEV0.4,不需要重新初始化迭代器):

代码语言:javascript
复制
it = itoken(tokens)
tcm = create_tcm(it, vectorizer)

Fit模型:

代码语言:javascript
复制
fit <- glove(tcm = tcm,
             word_vectors_size = 50,
             x_max = 10, learning_rate = 0.2,
             num_iters = 15)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39514941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档