Gensim - 詞嵌入開發



本章將幫助我們理解如何在 Gensim 中開發詞嵌入。

詞嵌入是一種表示單詞和文件的方法,它是一種密集的文字向量表示,其中具有相同含義的單詞具有相似的表示。以下是詞嵌入的一些特徵:

  • 它是一類技術,將單個單詞表示為預定義向量空間中的實值向量。

  • 這項技術通常被歸入深度學習 (DL) 領域,因為每個單詞都對映到一個向量,並且向量的值以與神經網路 (NN) 相同的方式學習。

  • 詞嵌入技術的主要方法是為每個單詞提供密集的分散式表示。

不同的詞嵌入方法/演算法

如上所述,詞嵌入方法/演算法從文字語料庫中學習實值向量表示。這個學習過程可以使用 NN 模型用於文件分類等任務,或者是一個無監督的過程,例如文件統計。在這裡,我們將討論兩種可以用來從文字中學習詞嵌入的方法/演算法:

Google 的 Word2Vec

Word2Vec 由 Tomas Mikolov 等人在 Google 於 2013 年開發,是一種有效地從文字語料庫學習詞嵌入的統計方法。它實際上是為使基於 NN 的詞嵌入訓練更高效而開發的。它已成為詞嵌入的事實標準。

Word2Vec 的詞嵌入涉及對學習到的向量的分析以及對單詞表示的向量數學的探索。以下是可以用作 Word2Vec 方法一部分的兩種不同的學習方法:

  • CBoW(連續詞袋)模型
  • 連續 Skip-Gram 模型

斯坦福大學的 GloVe

GloVe(用於單詞表示的全域性向量)是 Word2Vec 方法的擴充套件。它由斯坦福大學的 Pennington 等人開發。GloVe 演算法是兩者的混合:

  • 矩陣分解技術的全域性統計,例如 LSA(潛在語義分析)
  • Word2Vec 中基於區域性上下文的學習。

如果我們談論它的工作原理,那麼 GloVe 並沒有使用視窗來定義區域性上下文,而是使用跨整個文字語料庫的統計資料來構建顯式的詞共現矩陣。

開發 Word2Vec 嵌入

在這裡,我們將使用 Gensim 開發 Word2Vec 嵌入。為了使用 Word2Vec 模型,Gensim 為我們提供了Word2Vec 類,可以從models.word2vec匯入。為了實現它,word2vec 需要大量的文字,例如整個亞馬遜評論語料庫。但是在這裡,我們將把這個原理應用於小型記憶體文字。

實現示例

首先,我們需要從 gensim.models 中匯入 Word2Vec 類,如下所示:

from gensim.models import Word2Vec

接下來,我們需要定義訓練資料。與其採用大型文字檔案,我們使用一些句子來實現這個原理。

sentences = [
   ['this', 'is', 'gensim', 'tutorial', 'for', 'free'],
   ['this', 'is', 'the', 'tutorials' 'point', 'website'],
   ['you', 'can', 'read', 'technical','tutorials', 'for','free'],
   ['we', 'are', 'implementing','word2vec'],
   ['learn', 'full', 'gensim', 'tutorial']
]

提供訓練資料後,我們需要訓練模型。可以按如下方式完成:

model = Word2Vec(sentences, min_count=1)

我們可以總結模型如下:

print(model)

我們可以總結詞彙表如下:

words = list(model.wv.vocab)
print(words)

接下來,讓我們訪問一個單詞的向量。我們正在對單詞“tutorial”進行操作。

print(model['tutorial'])

接下來,我們需要儲存模型:

model.save('model.bin')

接下來,我們需要載入模型:

new_model = Word2Vec.load('model.bin')

最後,列印儲存的模型如下:

print(new_model)

完整的實現示例

from gensim.models import Word2Vec
sentences = [
   ['this', 'is', 'gensim', 'tutorial', 'for', 'free'],
   ['this', 'is', 'the', 'tutorials' 'point', 'website'],
   ['you', 'can', 'read', 'technical','tutorials', 'for','free'],
   ['we', 'are', 'implementing','word2vec'],
   ['learn', 'full', 'gensim', 'tutorial']
]
model = Word2Vec(sentences, min_count=1)
print(model)
words = list(model.wv.vocab)
print(words)
print(model['tutorial'])
model.save('model.bin')
new_model = Word2Vec.load('model.bin')
print(new_model)

輸出

Word2Vec(vocab=20, size=100, alpha=0.025)
[
   'this', 'is', 'gensim', 'tutorial', 'for', 'free', 'the', 'tutorialspoint', 
   'website', 'you', 'can', 'read', 'technical', 'tutorials', 'we', 'are', 
   'implementing', 'word2vec', 'learn', 'full'
]
[
   -2.5256255e-03 -4.5352755e-03 3.9024993e-03 -4.9509313e-03
   -1.4255195e-03 -4.0217536e-03 4.9407515e-03 -3.5925603e-03
   -1.1933431e-03 -4.6682903e-03 1.5440651e-03 -1.4101702e-03
   3.5070938e-03 1.0914479e-03 2.3334436e-03 2.4452661e-03
   -2.5336299e-04 -3.9676363e-03 -8.5054158e-04 1.6443320e-03
   -4.9968651e-03 1.0974540e-03 -1.1123562e-03 1.5393364e-03
   9.8941079e-04 -1.2656028e-03 -4.4471184e-03 1.8309267e-03
   4.9302122e-03 -1.0032534e-03 4.6892050e-03 2.9563988e-03
   1.8730218e-03 1.5343715e-03 -1.2685956e-03 8.3664013e-04
   4.1721235e-03 1.9445885e-03 2.4097660e-03 3.7517555e-03
   4.9687522e-03 -1.3598346e-03 7.1032363e-04 -3.6595813e-03
   6.0000515e-04 3.0872561e-03 -3.2115565e-03 3.2270295e-03
   -2.6354722e-03 -3.4988276e-04 1.8574356e-04 -3.5757164e-03
   7.5391348e-04 -3.5205986e-03 -1.9795434e-03 -2.8321696e-03
   4.7155009e-03 -4.3349937e-04 -1.5320212e-03 2.7013756e-03
   -3.7055744e-03 -4.1658725e-03 4.8034848e-03 4.8594419e-03
   3.7129463e-03 4.2385766e-03 2.4612297e-03 5.4920948e-04
   -3.8912550e-03 -4.8226118e-03 -2.2763973e-04 4.5571579e-03
   -3.4609400e-03 2.7903817e-03 -3.2709218e-03 -1.1036445e-03
   2.1492650e-03 -3.0384419e-04 1.7709908e-03 1.8429896e-03
   -3.4038599e-03 -2.4872608e-03 2.7693063e-03 -1.6352943e-03
   1.9182395e-03 3.7772327e-03 2.2769428e-03 -4.4629495e-03
   3.3151123e-03 4.6509290e-03 -4.8521687e-03 6.7615538e-04
   3.1034781e-03 2.6369948e-05 4.1454583e-03 -3.6932561e-03
   -1.8769916e-03 -2.1958587e-04 6.3395966e-04 -2.4969708e-03
]
Word2Vec(vocab=20, size=100, alpha=0.025)

詞嵌入的視覺化

我們還可以透過視覺化來探索詞嵌入。這可以透過使用經典的投影方法(如 PCA)將高維詞向量簡化為二維圖來完成。簡化後,我們就可以將它們繪製到圖表上。

使用 PCA 繪製詞向量

首先,我們需要從訓練好的模型中檢索所有向量,如下所示:

Z = model[model.wv.vocab]

接下來,我們需要使用 PCA 類建立一個詞向量的二維 PCA 模型,如下所示:

pca = PCA(n_components=2)
result = pca.fit_transform(Z)

現在,我們可以使用 matplotlib 繪製結果投影,如下所示:

Pyplot.scatter(result[:,0],result[:,1])

我們還可以使用單詞本身在圖表上註釋點。使用 matplotlib 繪製結果投影,如下所示:

words = list(model.wv.vocab)
for i, word in enumerate(words):
   pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))

完整的實現示例

from gensim.models import Word2Vec
from sklearn.decomposition import PCA
from matplotlib import pyplot
sentences = [
   ['this', 'is', 'gensim', 'tutorial', 'for', 'free'],
	['this', 'is', 'the', 'tutorials' 'point', 'website'],
	['you', 'can', 'read', 'technical','tutorials', 'for','free'],
	['we', 'are', 'implementing','word2vec'],
	['learn', 'full', 'gensim', 'tutorial']
]
model = Word2Vec(sentences, min_count=1)
X = model[model.wv.vocab]
pca = PCA(n_components=2)
result = pca.fit_transform(X)
pyplot.scatter(result[:, 0], result[:, 1])
words = list(model.wv.vocab)
for i, word in enumerate(words):
   pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))
pyplot.show()

輸出

Word2Vec
廣告