首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Text2vec中实现Arora 2017

在Text2vec中实现Arora 2017
EN

Stack Overflow用户
提问于 2018-01-19 23:36:19
回答 1查看 130关注 0票数 0

我正在尝试使用text2vec复制Arora2017 (https://github.com/PrincetonML/SIF / https://openreview.net/forum?id=SyK00v5xx)。作者通过平均单词嵌入并减去第一主成分来计算句子嵌入。

多亏了text2vec的作者,我可以计算手套嵌入并对它们进行平均。下一步是计算主成分/svd,并从嵌入中减去第一个成分。

我可以使用irlba包(我相信它也在tex2vec中使用)来计算svd,但是接下来我被困在如何准确地从平均单词嵌入中减去de主成分。

本文中的python代码(https://github.com/PrincetonML/SIF/blob/master/src/SIF_embedding.py)具有以下功能

代码语言:javascript
复制
def remove_pc(X, npc=1):
"""
Remove the projection on the principal components
:param X: X[i,:] is a data point
:param npc: number of principal components to remove
:return: XX[i, :] is the data point after removing its projection
"""
pc = compute_pc(X, npc)
if npc==1:
    XX = X - X.dot(pc.transpose()) * pc
else:
    XX = X - X.dot(pc.transpose()).dot(pc)
return XX

我的R代码是

代码语言:javascript
复制
# get the word vectors
wv_context = glove$components
word_vectors = wv_main + t(wv_context)

# create document term matrix
dtm = create_dtm(it, vectorizer)

# assign the word embeddings
common_terms = intersect(colnames(dtm), rownames(word_vectors) )

# normalise
dtm_averaged <-  text2vec::normalize(dtm[, common_terms], "l1")

例如,如果我有1K个句子x 300个变量,我运行irlba函数,得到三个矩阵。例如,这些数据有4个分量x 1K观测值。

如何转换此函数的输出(1K x x变量/组件),以便从句子嵌入中减去组件(1K x 300变量)?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-01-20 02:52:24

这个想法是,使用截断的SVD/PCA,你可以用最小的平方误差重建原始矩阵。所以你得到(U, D, V)形式的奇异值分解,原始矩阵的重建是A ~ U * D * t(V)。现在我们从原始矩阵中减去这个重建-这将是作者提出的。下面是一个例子:

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

it = itoken(movie_review$review, preprocessor = tolower, tokenizer = word_tokenizer)
dtm = create_dtm(it, hash_vectorizer(2**14))

lsa = LSA$new(n_topics = 64)
doc_emb = lsa$fit_transform(dtm)

doc_emb_pc1 = doc_emb_svd$u %*% doc_emb_svd$d %*% t(doc_emb_svd$v)
doc_emb_minus_pc1 = doc_emb - doc_emb_pc1

如果您有机会完成您的实现,请考虑将其贡献给text2vec -这是Arora语句嵌入- https://github.com/dselivanov/text2vec/issues/157的门票。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48344547

复制
相关文章

相似问题

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