【问题标题】:Train TensorFlow language model with NCE or sampled softmax使用 NCE 或采样的 softmax 训练 TensorFlow 语言模型
【发布时间】:2016-11-16 17:58:55
【问题描述】:

我正在调整 TensorFlow RNN 教程来训练具有 NCE 损失或采样 softmax 的语言模型,但我仍然想报告困惑。然而,我得到的困惑非常奇怪:对于 NCE,我得到数百万(可怕!),而对于采样的 softmax,我在一个 epoch 后得到 700 的 PPL(好得令人难以置信?!)。我想知道我做错了什么。

这是我对 PTBModel 的改编:

class PTBModel(object):
  """The PTB model."""

  def __init__(self, is_training, config, loss_function="softmax"):
    ...
    w = tf.get_variable("proj_w", [size, vocab_size])
    w_t = tf.transpose(w)
    b = tf.get_variable("proj_b", [vocab_size])

    if loss_function == "softmax":
      logits = tf.matmul(output, w) + b
      loss = tf.nn.seq2seq.sequence_loss_by_example(
          [logits],
          [tf.reshape(self._targets, [-1])],
          [tf.ones([batch_size * num_steps])])
      self._cost = cost = tf.reduce_sum(loss) / batch_size
    elif loss_function == "nce":
      num_samples = 10
      labels = tf.reshape(self._targets, [-1,1])
      hidden = output
      loss = tf.nn.nce_loss(w_t, b,                           
                            hidden,
                            labels,
                            num_samples, 
                            vocab_size)
    elif loss_function == "sampled_softmax":
      num_samples = 10
      labels = tf.reshape(self._targets, [-1,1])
      hidden = output
      loss = tf.nn.sampled_softmax_loss(w_t, b,
                                        hidden, 
                                        labels, 
                                        num_samples,
                                        vocab_size)

    self._cost = cost = tf.reduce_sum(loss) / batch_size
    self._final_state = state

对这个模型的调用是这样的:

mtrain = PTBModel(is_training=True, config=config, loss_function="nce")
mvalid = PTBModel(is_training=True, config=config)

我在这里没有做任何奇特的事情,更改损失函数应该非常简单。那么为什么它不起作用呢?

谢谢, 乔里斯

【问题讨论】:

    标签: tensorflow lstm softmax language-model


    【解决方案1】:

    使用基线模型 (Softmax),在一个 epoch 中,您应该会超过 700。通过更改损失,您可能需要重新调整一些超参数 - 特别是学习率。

    此外,您的评估模型应该使用 Softmax 报告真正的困惑——您这样做了吗?

    【讨论】:

    • 似乎采样的 softmax 确实有效,它在 13 个 epoch(SmallConfig)后以 129 个负样本结束。
    • 另一方面,NCE 仍然让我失望。困惑(如您所说,使用完整的 softmax 计算)大约为数百万。同意我需要重新调整,但即使没有调整,我也希望困惑度会有所下降而不是从约 10k 增加到 2M?!
    • 仅供参考:NCE 实际上为少量时间步提供了合理的值。当你增加这个数字时,它开始变得疯狂。
    • @niefpaarschoenen 嗨,我目前正在研究它。您是否发现使用 NCE 可以提高性能?特别是在每秒字数方面?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2019-08-03
    • 2020-08-12
    相关资源
    最近更新 更多