![](/img/trans.png)
[英]Huggingface GPT2 and T5 model APIs for sentence classification?
[英]Where is perplexity calculated in the Huggingface gpt2 language model code?
我看到一些 github 評論說 model() 調用損失的 output 是困惑的形式: https://github.com/huggingface/transformers/issues/473
但是當我查看相關代碼時... https://huggingface.co/transformers/_modules/transformers/modeling_openai.html#OpenAIGPTLMHeadModel.forward
if labels is not None:
# Shift so that tokens < n predict n
shift_logits = lm_logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# Flatten the tokens
loss_fct = CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
outputs = (loss,) + outputs
return outputs # (loss), lm_logits, (all hidden states), (all attentions)
我看到正在計算交叉熵,但沒有轉化為困惑。 損失最終在哪里轉化? 或者是否已經存在我不理解的轉變?
啊好吧,我找到了答案。 代碼實際上是返回交叉熵。 在 github 評論中,他們說這是令人困惑的……他們是這樣說的,因為 OP 確實如此
return math.exp(loss)
它將熵轉化為困惑:)
沒有 latex 沒問題。 根據定義,困惑度(三重 P)是:
PP(p) = e^(H(p))
其中 H 代表混沌(古希臘語:χìος)或熵。 在一般情況下,我們有交叉熵:
PP(p) = e^(H(p,q))
e 是對數的自然底,這就是 PyTorch 更喜歡計算熵和交叉熵的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.