我正在调整 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)
我在这里没有做任何异国情调的事情,更改损失函数应该非常简单。那么为什么它不起作用呢?
谢谢,乔里斯