在 Keras 中实现 word2vec

Implement word2vec in Keras

我想在keras中实现word2vec算法,这可能吗? 我怎样才能适应这个模型?我应该使用自定义损失函数吗?

Is this possible?

你自己已经回答了:是的。除了 word2veckeras, which uses gensim, here's another CBOW implementation 没有额外的依赖项(以防万一,我不隶属于此 repo)。您可以将它们用作示例。

How can I fit the model?

由于训练数据是大量的句子语料,最方便的方法是model.fit_generator, which "fits the model on data generated batch-by-batch by a Python generator". The generator runs indefinitely yielding (word, context, target) CBOW (or SG) tuples, but you manually specify sample_per_epoch and nb_epoch to limit the training. This way you decouple sentence analysis (tokenization, word index table, sliding window, etc) and actual keras model, plus save a lot of resources

Should I use custom loss function?

CBOW 最小化了中心词的预测分布和真实分布之间的距离,所以最简单的形式 categorical_crossentropy 就可以做到。 如果你实现 , which is a bit more complex, yet much more efficient, the loss function changes to binary_crossentropy。自定义损失函数是不必要的。

对于任何对数学和概率模型的细节感兴趣的人,我强烈推荐斯坦福大学的 CS224D class。 Here is the lecture notes 关于 word2vec、CBOW 和 Skip-Gram。

另一个有用的参考:word2vec implementation in pure numpy and c.