计算在 penn treebank 上训练 LSTM 的困惑度
calculating perplexity for training LSTM on penn treebank
我正在 penn treebank 上实施语言模型训练。
我正在为每个时间步添加损失,然后计算困惑度。
即使经过一段时间的训练,这也让我产生了数千亿的无意义的高困惑。
损失本身会减少,但最多只能减少到 20 左右。 (我需要一位数的损失来获得明智的困惑)。
这让我想知道我的困惑度计算是否被误导了。
是否应该基于每个时间步长的损失然后取平均值而不是将它们全部相加?
我的batch_size是20,num_steps是35。
def perplexity(loss):
perplexity = np.exp(loss)
return perplexity
...
loss = 0
x = nn.Variable((batch_size, num_steps))
t = nn.Variable((batch_size, num_steps))
e_list = [PF.embed(x_elm, num_words, state_size, name="embed") for x_elm in F.split(x, axis=1)]
t_list = F.split(t, axis=1)
for i, (e_t, t_t) in enumerate(zip(e_list, t_list)):
h1 = l1(F.dropout(e_t,0.5))
h2 = l2(F.dropout(h1,0.5))
y = PF.affine(F.dropout(h2,0.5), num_words, name="pred")
t_t = F.reshape(t_t,[batch_size,1])
loss += F.mean(F.softmax_cross_entropy(y, t_t))
for epoch in range(max_epoch):
....
for i in range(iter_per_epoch):
x.d, t.d = get_words(train_data, i, batch_size)
perp = perplexity(loss.d)
....
您似乎在计算交叉熵损失之和的指数。困惑,通过,被定义为熵的二次方。
困惑度(M)=2^熵(M)
困惑度(M) = 2^(-1/n)(log2(P(w1, w2,...,wn)))
其中 log2 = 以 2 为底的对数
所以是的,它应该基于每个时间步长的损失,而不是求和或均值。像现在这样求和会大大增加你的交叉熵损失,所以然后提高 2 的那个值的次方会非常大。
可以找到更多详细信息here
为了计算训练的困惑度,损失需要按照here中的描述取幂。
Tensorflow 使用自然对数计算交叉熵损失,因此我们使用 tf.exp 迭代计算每个时间步的训练损失,例如 tf.exp(accumulative_iteration_costs / accumulative_num_steps_iters).
检查tensorflow PTB字lstm example
我正在 penn treebank 上实施语言模型训练。
我正在为每个时间步添加损失,然后计算困惑度。
即使经过一段时间的训练,这也让我产生了数千亿的无意义的高困惑。
损失本身会减少,但最多只能减少到 20 左右。 (我需要一位数的损失来获得明智的困惑)。
这让我想知道我的困惑度计算是否被误导了。
是否应该基于每个时间步长的损失然后取平均值而不是将它们全部相加?
我的batch_size是20,num_steps是35。
def perplexity(loss):
perplexity = np.exp(loss)
return perplexity
...
loss = 0
x = nn.Variable((batch_size, num_steps))
t = nn.Variable((batch_size, num_steps))
e_list = [PF.embed(x_elm, num_words, state_size, name="embed") for x_elm in F.split(x, axis=1)]
t_list = F.split(t, axis=1)
for i, (e_t, t_t) in enumerate(zip(e_list, t_list)):
h1 = l1(F.dropout(e_t,0.5))
h2 = l2(F.dropout(h1,0.5))
y = PF.affine(F.dropout(h2,0.5), num_words, name="pred")
t_t = F.reshape(t_t,[batch_size,1])
loss += F.mean(F.softmax_cross_entropy(y, t_t))
for epoch in range(max_epoch):
....
for i in range(iter_per_epoch):
x.d, t.d = get_words(train_data, i, batch_size)
perp = perplexity(loss.d)
....
您似乎在计算交叉熵损失之和的指数。困惑,通过,被定义为熵的二次方。
困惑度(M)=2^熵(M)
困惑度(M) = 2^(-1/n)(log2(P(w1, w2,...,wn)))
其中 log2 = 以 2 为底的对数
所以是的,它应该基于每个时间步长的损失,而不是求和或均值。像现在这样求和会大大增加你的交叉熵损失,所以然后提高 2 的那个值的次方会非常大。
可以找到更多详细信息here
为了计算训练的困惑度,损失需要按照here中的描述取幂。
Tensorflow 使用自然对数计算交叉熵损失,因此我们使用 tf.exp 迭代计算每个时间步的训练损失,例如 tf.exp(accumulative_iteration_costs / accumulative_num_steps_iters).
检查tensorflow PTB字lstm example