2

R rword2vec パッケージを使用してテキスト本文の単語の類推を計算すると、優れた直感的な答えが得られます。ただし、bin_to_txt を使用してこれから単語ベクトルを出力し、これらを読み込んで text2vec パッケージを使用して類推を計算すると、非常に質の低い回答が得られます。

2 つのパッケージがアナロジーを計算する方法に違いはありますか?もしそうなら、このパッケージなしで rword2vec の計算をどのように再現できますか? 残念ながら、本番環境で使用する必要があるマシンに rword2vec をインストールできません。

以下の例では、類推 king:queen::man:_ を使用しています。同じベクトルで、rword2vec は「女性」、「女の子」、「金髪」を与え、sim2 を使用する text2vec メソッドは「男」、「ahab」、「王」を与えます。後者の方法論で私は何を台無しにしていますか?

再現するための以下のコード:

# 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 
4

0 に答える 0