【问题标题】:How does the tensorflow word2vec tutorial update embeddings?tensorflow word2vec 教程如何更新嵌入?
【发布时间】:2017-06-22 16:22:32
【问题描述】:

这个话题接近了:What is the purpose of weights and biases in tensorflow word2vec example?

但我对此的解释仍然遗漏了一些东西:https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/word2vec/word2vec_basic.py

据我了解,您将字典中的目标词和上下文词的索引提供给网络。

_, loss_val = session.run([optimizer, loss], feed_dict=feed_dict)
average_loss += loss_val

然后查找批输入以返回开始时随机生成的向量

    embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
    # Look up embeddings for inputs.
    embed = tf.nn.embedding_lookup(embeddings, train_inputs)

然后优化器调整权重和偏差以最好地预测标签,而不是 num_sampled 随机选择

 loss = tf.reduce_mean(
  tf.nn.nce_loss(weights=nce_weights,
                 biases=nce_biases,
                 labels=train_labels,
                 inputs=embed,
                 num_sampled=num_sampled,
                 num_classes=vocabulary_size))

  # Construct the SGD optimizer using a learning rate of 1.0.
  optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)

我的问题如下:

  1. 嵌入变量在哪里更新?。在我看来,我可以通过神经网络运行单词的索引来获得最终结果,或者只使用final_embeddings 向量并使用它。但我不明白embeddings 的随机初始化在哪里发生了变化。

  2. 如果我要绘制这个计算图,它会是什么样子(或者更好的是,实际这样做的最佳方法是什么)?

  3. 这是一次运行批处理中的所有上下文/目标对吗?还是一个一个?

【问题讨论】:

    标签: python tensorflow word2vec word-embedding


    【解决方案1】:

    嵌入:嵌入是一个变量。每次你做反向传播时它都会更新(同时运行有损失的优化器)

    Grpah:您是否尝试保存图表并将其显示在 tensorboard 中?这是你要找的吗?

    批处理:至少在您链接的示例中,他正在使用第 96 行的函数进行批处理。https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/examples/tutorials/word2vec/word2vec_basic.py#L96

    如果我误解了您的问题,请纠正我。

    【讨论】:

    • 所以您建议优化器同时更新嵌入权重/偏差?