Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R text2vec和rword2vec模拟结果在包之间不同_R_Word2vec_Cosine Similarity_Word Embedding - Fatal编程技术网

R text2vec和rword2vec模拟结果在包之间不同

R text2vec和rword2vec模拟结果在包之间不同,r,word2vec,cosine-similarity,word-embedding,R,Word2vec,Cosine Similarity,Word Embedding,当我使用rword2vec包计算文本体上的单词类比时,我得到了非常直观的答案。然而,当我使用bin_to_txt输出这个词的向量,然后读入并使用text2vec包计算类比时,我得到的答案质量很低 两个软件包计算类比的方式是否有差异,如果有,我如何在没有这个软件包的情况下重现rword2vec的计算?不幸的是,我不能在生产中使用的机器上安装rword2vec 在下面的示例中,我使用了king:queen::man:\的类比。使用相同的向量,rword2vec给了我“女人”、“女孩”和“金发女郎”,

当我使用rword2vec包计算文本体上的单词类比时,我得到了非常直观的答案。然而,当我使用bin_to_txt输出这个词的向量,然后读入并使用text2vec包计算类比时,我得到的答案质量很低

两个软件包计算类比的方式是否有差异,如果有,我如何在没有这个软件包的情况下重现rword2vec的计算?不幸的是,我不能在生产中使用的机器上安装rword2vec

在下面的示例中,我使用了king:queen::man:\的类比。使用相同的向量,rword2vec给了我“女人”、“女孩”和“金发女郎”,而使用sim2的text2vec方法给了我“男人”、“亚哈”和“国王”。后一种方法我搞砸了什么

复制代码如下:

# Text file: http://mattmahoney.net/dc/text8.zip
# USING rword2vec
# require(devtools)
# install_github("mukul13/rword2vec")
require(rword2vec)

file1="C:\\Users\\bdk\\Downloads\\text8"

model = word2vec(train_file = file1, output_file = "vec_repro.bin",binary=1)

anal1 = word_analogy(file_name = "vec_repro.bin",search_words = "king queen man",num = 20)
print(anal1)
# first 10 results:
# word              dist
# 1       woman 0.718326687812805
# 2        girl 0.607264637947083
# 3      blonde  0.56832879781723
# 4        lady 0.518971383571625
# 5      lovely 0.515585899353027
# 6    stranger 0.504840195178986
# 7      ladies 0.500177025794983
# 8      totoro 0.497228592634201
# 9        baby 0.497049778699875
# 10   handsome 0.490864992141724

bin_to_txt("vec_repro.bin","vector_repro.txt")

# Read in these word vectors and do the same calculation but with text2vec
require(text2vec)
data1=as.data.frame(read.table("vector_repro.txt",skip=1))
vocab_all2 <- data1
rownames(vocab_all2) <- vocab_all2[,1]
vocab_all2$V1 <- NULL
colnames(vocab_all2) <- NULL
vocab_all2 <- vocab_all2[complete.cases(vocab_all2),]
vocab_all3 <- data.matrix(vocab_all2)

guess1 <- (
  vocab_all3["king", , drop = FALSE] -
    vocab_all3["queen", , drop = FALSE] +
    vocab_all3["man", , drop = FALSE]
)

cos_sim = sim2(x = vocab_all3, y = guess1, method = "cosine", norm = "l2")
head(sort(cos_sim[,1], decreasing = TRUE), 10)
# first 10 results:
# man      ahab      king       god   prophet   faramir   saladin      saul      enki   usurper 
# 0.7212826 0.4715135 0.4696279 0.4625656 0.4522798 0.4391127 0.4358722 0.4326022 0.4310836 0.4300992 
#文本文件:http://mattmahoney.net/dc/text8.zip
#使用rword2vec
#需要(devtools)
#安装github(“mukul13/rword2vec”)
需要(rword2vec)
file1=“C:\\Users\\bdk\\Downloads\\text8”
model=word2vec(train_file=file1,output_file=“vec_repro.bin”,binary=1)
anal1=单词类比(文件名=“vec\u repo.bin”,搜索词=“国王女王人”,数值=20)
打印(1)
#前10项结果:
#字距
#1名妇女0.718326687812805
#2女孩0.607264637947083
#3金发女郎0.56832879781723
#4女士0.518971383571625
#5可爱的0.51558599353027
#6陌生人0.504840195178986
#7女士0.500177025794983
#8托托罗0.497228592634201
#9婴儿0.497049778699875
#10.0.490864992141724
bin_to_txt(“vec_repro.bin”,“vector_repro.txt”)
#读入这些单词向量并进行相同的计算,但使用text2vec
需要(text2vec)
data1=as.data.frame(read.table(“vector_repo.txt”,skip=1))

vocab_all 2您可以在开始时添加库(devtools)。此外,我已经在您的“model=”语句中两次中止了RStudio下的R会话。我正在使用数据帧的字符向量作为我的训练文件。有什么想法吗?
word2vec
类比评估过程排除(静默地)正在查询的单词。这个技巧并不广为人知(但这就是原版word2vec和gensim的工作方式)。@律师,我不知道为什么会这样!即使重新启动了R会话,这也适用于我:(感谢devtools的建议,添加了。@DmitriySelivanov,谢谢……这不仅仅是第一个单词。我编辑了我的帖子,因此更清楚的是,返回的单词列表的质量完全不同。@bkauder我无法复制,因为我总是收到“catch segfault”从
rword2vec
。您可以在某处与word vectors共享文件吗?