gensim Word2Vec
有一个选项,相当于TensorFlow word2vec示例中的“训练步骤”:Word2Vec Basic?如果没有,gensim使用什么默认值? gensim参数iter
与训练步骤有关吗?
TensorFlow脚本包括此部分。
with tf.Session(graph=graph) as session:
# We must initialize all variables before we use them.
init.run()
print('Initialized')
average_loss = 0
for step in xrange(num_steps):
batch_inputs, batch_labels = generate_batch(
batch_size, num_skips, skip_window)
feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels}
# We perform one update step by evaluating the optimizer op (including it
# in the list of returned values for session.run()
_, loss_val = session.run([optimizer, loss], feed_dict=feed_dict)
average_loss += loss_val
if step % 2000 == 0:
if step > 0:
average_loss /= 2000
# The average loss is an estimate of the loss over the last 2000 batches.
print('Average loss at step ', step, ': ', average_loss)
average_loss = 0
# Note that this is expensive (~20% slowdown if computed every 500 steps)
if step % 10000 == 0:
sim = similarity.eval()
for i in xrange(valid_size):
valid_word = reverse_dictionary[valid_examples[i]]
top_k = 8 # number of nearest neighbors
nearest = (-sim[i, :]).argsort()[1:top_k + 1]
log_str = 'Nearest to %s:' % valid_word
for k in xrange(top_k):
close_word = reverse_dictionary[nearest[k]]
log_str = '%s %s,' % (log_str, close_word)
print(log_str)
final_embeddings = normalized_embeddings.eval()
在TensorFlow示例中,如果我在嵌入上执行T-SNE并使用matplotlib进行绘图,则当步数高时,该绘图看起来更合理。我正在使用1,200封电子邮件的小型语料库。一种看起来更合理的方式是将数字聚集在一起。我希望使用gensim达到相同的质量水平。
是的,Word2Vec
类构造函数有iter
参数:
iter =语料库上的迭代次数(epochs)。默认值为5。
此外,如果直接调用Word2Vec.train()
方法,则可以传入具有相同含义的epochs
参数。
实际训练步骤的数量是从时期推断出来的,但取决于其他参数,如文本大小,窗口大小和批量大小。如果您只是想提高嵌入向量的质量,那么增加iter
是正确的方法。