[英]Gensim Word2vec model is not updating the previous word's embedding weights during increased training
[英]Gensim 3.8.3 Word2Vec is not updating the weights/parameters on a toy dataset
我正在尝试在 4 个句子的简单玩具数据集上训练 word2vec 模型。 我需要的 Word2vec 版本是:
我面临的问题是:无论我如何更改上述参数,都不会更新/学习词向量。 epochs=1 和 epochs=500 的词向量是相同的。
from gensim.models import Word2Vec
import numpy as np
import matplotlib.pyplot as plt
import nltk
# toy dataset with 4 sentences
sents = ['what is the time',
'what is the day',
'what time is the meeting',
'cancel the meeting']
sents = [nltk.word_tokenize(string) for string in sents]
# model initialization and training
model = Word2Vec(alpha=0.5, min_alpha =0.25, min_count = 0, size=2, window=4,
workers=1, sg = 1, hs = 0, negative = 0, sample=0, seed = 42)
model.build_vocab(sents)
model.train(sents, total_examples=4, epochs=500)
# getting word vectors into array
vocab = model.wv.vocab.keys()
vocab_vectors = model.wv[vocab]
print(vocab)
print(vocab_vectors)
#plotting word vectors
plt.scatter(vocab_vectors[:,0], vocab_vectors[:,1], c ="blue")
for i, word in enumerate(vocab):
plt.annotate(word, (vocab_vectors[i,0], vocab_vectors[i,1]))
print(vocab)
如下
['what', 'is', 'time', 'cancel', 'the', 'meeting', 'day']
print(vocab_vectors)
的输出如下
[[ 0.08136337 -0.05059118]
[ 0.06549312 -0.22880174]
[-0.08925873 -0.124718 ]
[ 0.05645624 -0.03120007]
[ 0.15067646 -0.14344342]
[-0.12645201 0.06202405]
[-0.22905378 -0.01489289]]
为什么我认为没有学习向量? 我将 epochs 值更改为 1、10、50、500...并运行整个代码以检查每次运行的输出。 对于 epochs = #any_value <1,10,50,500>,所有运行的输出(vocab、vocab_vectors 和绘图)都相同。
通过提供参数negative=0, hs=0
,您已经禁用了两种训练模式,并且没有进行任何训练。
您应该保留默认的非零negative
,或者在禁用负采样(使用hs=1, negative=0
)的同时启用非默认的分层 softmax 模式。
其他想法:
min_count
通常都是一个坏主意,因为 word2vec 需要多个不同的单词用法示例来训练有用的向量——通常忽略稀有单词比混合不完整的信息更好。alpha
/ min_alpha
通常也是一个坏主意 - 尽管在这里您可能只是尝试使用极端值来触发任何更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.