【问题标题】:gensim word2vec - array dimensions in updating with online word embeddinggensim word2vec - 在线词嵌入更新中的数组维度
【发布时间】:2017-07-10 12:09:26
【问题描述】:

gensim 0.13.4.1 中的 Word2Vec 无法即时更新词向量。

model.build_vocab(sentences, update=False)

工作正常;然而,

model.build_vocab(sentences, update=True)

没有。


我正在使用this website 来尝试模仿他们所做的事情;因此我有时会使用以下脚本:

model = gensim.models.Word2Vec()
sentences = gensim.models.word2vec.LineSentence("./text8/text8")
model.build_vocab(sentences, keep_raw_vocab=False, trim_rule=None, progress_per=10000, update=False)
model.train(sentences)

但是,虽然它使用 update=False 运行,但使用 update=True 会给我以下回溯:

Traceback (most recent call last):
  File "word2vecAttempt.py", line 34, in <module>
    model.build_vocab(sentences, progress_per=10000, update=True)
  File "/home/brownc/anaconda3/lib/python3.5/site-packages/gensim/models/word2vec.py", line 535, in build_vocab
    self.finalize_vocab(update=update)  # build tables & arrays
  File "/home/brownc/anaconda3/lib/python3.5/site-packages/gensim/models/word2vec.py", line 708, in finalize_vocab
    self.update_weights()
  File "/home/brownc/anaconda3/lib/python3.5/site-packages/gensim/models/word2vec.py", line 1070, in update_weights
    self.wv.syn0 = vstack([self.wv.syn0, newsyn0])
  File "/home/brownc/anaconda3/lib/python3.5/site-packages/numpy/core/shape_base.py", line 230, in vstack
    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

【问题讨论】:

    标签: python numpy gensim


    【解决方案1】:

    我能够重现您的错误。我认为您在模型尚未训练时调用update=True。你应该只在它经过预训练后才调用它。

    这行得通:

    import gensim
    
    model = gensim.models.Word2Vec()
    sentences = gensim.models.word2vec.LineSentence("text8")
    model.build_vocab(sentences, update=False)
    model.train(sentences)
    
    model.build_vocab(sentences, update=True)
    model.train(sentences)
    

    但这会失败:

    import gensim
    
    model = gensim.models.Word2Vec()
    sentences = gensim.models.word2vec.LineSentence("text8")
    model.build_vocab(sentences, update=True)
    model.train(sentences)
    
    ValueError: all the input array dimensions except for the concatenation axis must match exactly
    

    使用最新版本的 gensim 0.13.4.1。

    【讨论】:

    • 谢谢!我认为这样做的目的是创建一个动态模型,当您将非训练数据输入其中时,该模型会更新。
    猜你喜欢
    • 2018-03-20
    • 2017-09-21
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2014-04-02
    • 2019-01-03
    相关资源
    最近更新 更多