我在text2vec
软件包中使用了潜在的语义分析来生成单词向量,并在发现奇怪的东西时使用了transform来拟合新数据,而当对相同数据进行训练时,空格不会对齐。
该方法似乎存在一些不一致(或随机性?)。也就是说,即使在完全相同的数据上重新运行LSA模型 时,尽管输入相同,但生成的词向量还是有很大差异。环顾四周时,我仅发现这些旧的,已关闭的github问题link link以及变更日志中有关正在清除LSA的提及。我使用movie_review数据集和(略作修改)文档中的代码重现了该行为:
library(text2vec)
packageVersion("text2vec") # ‘0.5.1’
data("movie_review")
N = 1000
tokens = word_tokenizer(tolower(movie_review$review[1:N]))
it=itoken(tokens)
voc = create_vocabulary(it) %>% prune_vocabulary(term_count_min = 5, doc_proportion_max =0.9)
vectorizer = vocab_vectorizer(voc)
tcm = create_tcm(it, vectorizer)
# edit: make tcm symmetric:
tcm = tcm + Matrix::t(Matrix::triu(tcm))
n_topics = 10
lsa_1 = LatentSemanticAnalysis$new(n_topics)
d1 = lsa_1$fit_transform(tcm)
lsa_2 = LatentSemanticAnalysis$new(n_topics)
d2 = lsa_2$fit_transform(tcm)
# despite being trained on the same data, words have completely different vectors:
sim2(d1["film",,drop=F], d2["film",,drop=F])
# yields values like -0.993363 but sometimes 0.9888435 (should be 1)
mean(diag(sim2(d1, d2)))
# e.g. -0.2316826
hist(diag(sim2(d1, d2)), main="self-similarity between models")
# note: these numbers are different every time!
# But: within each model, results seem consistent and reasonable:
# top similar words for "film":
head(sort(sim2(d1, d1["film",,drop=F])[,1],decreasing = T))
# film movie show piece territory bay
# 1.0000000 0.9873934 0.9803280 0.9732380 0.9680488 0.9668800
# same in the second model:
head(sort(sim2(d2, d2["film",,drop=F])[,1],decreasing = T))
# film movie show piece territory bay
# 1.0000000 0.9873935 0.9803279 0.9732364 0.9680495 0.9668819
# transform works:
sim2(d2["film",,drop=F], transform(tcm["film",,drop=F], lsa_2 )) # yields 1
# LSA in quanteda doesn't have this problem, same data => same vectors
library(quanteda)
d1q = textmodel_lsa(as.dfm(tcm), 10)
d2q = textmodel_lsa(as.dfm(tcm), 10)
mean(diag(sim2(d1q$docs, d2q$docs))) # yields 1
# the top synonyms for "film" are also a bit different with quanteda's LSA
# film movie hunk show territory bay
# 1.0000000 0.9770574 0.9675766 0.9642915 0.9577723 0.9573138
这是怎么回事,这是一个错误,还是出于某些原因而导致的预期行为,还是我误解了? (我有点希望后者...)。如果是故意的,为什么Quanteda的行为会有所不同?
答案 0 :(得分:1)
问题在于您的矩阵似乎病态,因此存在数值稳定性问题。
library(text2vec)
library(magrittr)
data("movie_review")
N = 1000
tokens = word_tokenizer(tolower(movie_review$review[1:N]))
it=itoken(tokens)
voc = create_vocabulary(it) %>% prune_vocabulary(term_count_min = 5, doc_proportion_max =0.9)
vectorizer = vocab_vectorizer(voc)
tcm = create_tcm(it, vectorizer)
# condition number
kappa(tcm)
# Inf
现在,如果您要截断SVD(LSA后面的算法),您会注意到奇异向量非常接近零:
library(irlba)
truncated_svd = irlba(tcm, 10)
str(truncated_svd)
# $ d : num [1:10] 2139 1444 660 559 425 ...
# $ u : num [1:4387, 1:10] -1.44e-04 -1.62e-04 -7.77e-05 -8.44e-04 -8.99e-04 ...
# $ v : num [1:4387, 1:10] 6.98e-20 2.37e-20 4.09e-20 -4.73e-20 6.62e-20 ...
# $ iter : num 3
# $ mprod: num 50
因此,嵌入的符号不稳定,并且它们之间的余弦角也不稳定。
答案 1 :(得分:0)
类似于它在Python的sklearn中的工作方式,在R中使用截断的SVD函数具有内置的随机数函数。这两者都使其对于大型模型的构建如此强大,但对于较小的用途却有些困难。如果在创建SVD矩阵之前将值设置为种子set.seed()
,那么就不会有问题。在执行LSA时,这曾经使我感到恐惧。
让我知道是否有帮助!