簡體   English   中英

手套詞移動相似

[英]Glove Word Mover Similarity

我想使用輕松的字移動器距離來計算文本相似度。 我有兩個不同的數據集(語料庫)。 見下文。

A <- data.frame(name = c(
  "X-ray right leg arteries",
  "consultation of gynecologist",
  "x-ray leg arteries",
  "x-ray leg with 20km distance",
  "x-ray left hand"
), stringsAsFactors = F)

B <- data.frame(name = c(
  "X-ray left leg arteries",
  "consultation (inspection) of gynecalogist",
  "MRI right leg arteries",
  "X-ray right leg arteries with special care"
), stringsAsFactors = F)

我在R中使用text2vec包。

library(text2vec)
library(stringr)
prep_fun = function(x) {
  x %>% 
    # make text lower case
    str_to_lower %>% 
    # remove non-alphanumeric symbols
    str_replace_all("[^[:alnum:]]", " ") %>% 
    # collapse multiple spaces
    str_replace_all("\\s+", " ")
}
Combine both datasets
C = rbind(A, B)

C$name = prep_fun(C$name)

it = itoken(C$name, progressbar = FALSE)
v = create_vocabulary(it) %>% prune_vocabulary()
vectorizer = vocab_vectorizer(v)
dtm = create_dtm(it, vectorizer)
tcm = create_tcm(it, vectorizer, skip_grams_window = 3)
glove_model = GloVe$new(word_vectors_size = 10, vocabulary = v, x_max = 3)
wv = glove_model$fit_transform(tcm, n_iter = 10)

# get average of main and context vectors as proposed in GloVe paper
wv = wv + t(glove_model$components)
rwmd_model = RWMD$new(wv)
rwmd_dist = dist2(dtm[1:nrow(A), ], dtm[nrow(A)+1:nrow(C), ], method = rwmd_model, norm = 'none')

head(rwmd_dist)

          [,1]      [,2]      [,3]      [,4]
[1,] 0.1220713 0.7905035 0.3085216 0.4182328
[2,] 0.7043127 0.1883473 0.8031200 0.7038919
[3,] 0.1220713 0.7905035 0.3856520 0.4836772
[4,] 0.5340587 0.6259011 0.7146630 0.2513135
[5,] 0.3403019 0.5575993 0.7568583 0.5124514

確實skip_grams_window = 3tcm = create_tcm(it, vectorizer, skip_grams_window = 3)代碼裝置檢查3個字到右,同時創造共生矩陣? 例如,文字“ X射線右腿動脈”將成為矢量-目標:“ X射線”

right   leg arteries
1   1   1

word_vectors_size的用途是word_vectors_size 我已經閱讀了手套的算法,但無法理解此功能的用法。

Gloves_model = GloVe $ new(word_vectors_size = 10,詞匯= v,x_max = 3)

建議與skip_grams_window參數一起指定skip_grams_window_context (有效值: "symmetric""right""left" )。 [文檔]

word_vectors_size參數用於定義基礎單詞向量的維數。 這意味着每個單詞都將在N維向量空間中轉換為向量。 有幾篇文章對詞向量有很好的解釋( 第1條第2條 )。

在您的示例中, glove_model = GloVe$new(word_vectors_size = 10, vocabulary = v, x_max = 3) ,它意味着10維單詞向量。

為單詞向量選擇合適的維度很重要。 根據這個在2014年10月的回復,

典型的間隔是100-300之間。 我要說您至少需要50D才能達到最低的精度。 如果選擇的維數較少,則將開始失去高維空間的屬性。 如果培訓時間對您的應用程序來說不是很重要,那么我會堅持使用200D尺寸,因為它具有不錯的功能。 使用300D可獲得極高的精度。 300D單詞功能將不會顯着改善,並且訓練將非常緩慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM