[英]gensim Doc2Vec vs tensorflow Doc2Vec
我正在尝试比较我的Doc2Vec(通过tf)和gensims实现的实现。 从视觉上看,gensim的表现更好。
我运行以下代码来训练gensim模型和下面的那个用于tensorflow模型。 我的问题如下:
window=5
参数是否意味着我在两边使用两个词来预测中间的一个? 或者两边都是5。 事情是有相当多的文件小于长度10。 model = Doc2Vec(dm=1, dm_concat=1, size=100, window=5, negative=10, hs=0, min_count=2, workers=cores)
model.build_vocab(corpus)
epochs = 100
for i in range(epochs):
model.train(corpus)
batch_size = 512
embedding_size = 100 # Dimension of the embedding vector.
num_sampled = 10 # Number of negative examples to sample.
graph = tf.Graph()
with graph.as_default(), tf.device('/cpu:0'):
# Input data.
train_word_dataset = tf.placeholder(tf.int32, shape=[batch_size])
train_doc_dataset = tf.placeholder(tf.int32, shape=[batch_size/context_window])
train_labels = tf.placeholder(tf.int32, shape=[batch_size/context_window, 1])
# The variables
word_embeddings = tf.Variable(tf.random_uniform([vocabulary_size,embedding_size],-1.0,1.0))
doc_embeddings = tf.Variable(tf.random_uniform([len_docs,embedding_size],-1.0,1.0))
softmax_weights = tf.Variable(tf.truncated_normal([vocabulary_size, (context_window+1)*embedding_size],
stddev=1.0 / np.sqrt(embedding_size)))
softmax_biases = tf.Variable(tf.zeros([vocabulary_size]))
###########################
# Model.
###########################
# Look up embeddings for inputs and stack words side by side
embed_words = tf.reshape(tf.nn.embedding_lookup(word_embeddings, train_word_dataset),
shape=[int(batch_size/context_window),-1])
embed_docs = tf.nn.embedding_lookup(doc_embeddings, train_doc_dataset)
embed = tf.concat(1,[embed_words, embed_docs])
# Compute the softmax loss, using a sample of the negative labels each time.
loss = tf.reduce_mean(tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases, embed,
train_labels, num_sampled, vocabulary_size))
# Optimizer.
optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss)
在这里查看jupyter笔记本(我在这里有两个型号工作和测试)。 在初始分析中,gensim模型仍然表现得更好。
老问题,但答案对未来的访客有用。 所以这是我的一些想法。
张量tensorflow
实现中存在一些问题:
window
是单面大小,因此window=5
将是5*2+1
= 11
单词。 batch_size
将是文档数。 因此train_word_dataset
形状将是batch_size * context_window
,而train_doc_dataset
和train_labels
形状将是batch_size
。 sampled_softmax_loss
不是negative_sampling_loss
。 它们是softmax_loss
两种不同近似值。 所以对于OP列出的问题:
doc2vec
中tensorflow
这种实现以其自己的方式工作和纠正,但它与gensim
实现和论文不同。 window
是如上所述的单面尺寸。 如果文档大小小于上下文大小,则使用较小的文档大小。 gensim
实现速度更快的原因有很多。 首先, gensim
进行了大量优化,所有操作都比天真的python操作更快,尤其是数据I / O. 其次,一些预处理步骤如min_count
在过滤gensim
将减小数据集大小。 更重要的是, gensim
使用negative_sampling_loss
,这比sampled_softmax_loss
,我猜这是主要原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.