torch.nn.functional.nll_loss
torch.nn.functional.nll_loss(input, target, weight=None, size_average=None,
ignore_index=- 100, reduce=None, reduction='mean')
負(fù)對(duì)數(shù)似然損失
參數(shù)解釋
- input:(N,C)其中C表示分類的類別數(shù),2D損失中(N,C,H,W),或者多維損失(N,C,d1,d2,...,dk)。input期望是一個(gè)對(duì)數(shù)的概率,所以之前的概率最好是對(duì)數(shù)形式,比如使用torch.nn.functional.log_softmax求預(yù)測(cè)的概率
- target:(N), 其中的數(shù)值在【0,c-1】之間。對(duì)于k維度的損失來(lái)說(shuō)形式為(N,d1,d2,...,dk)
- weight:(tensor,optional),一個(gè)手動(dòng)的標(biāo)量給每個(gè)分類,如果有,是一個(gè)大小為C的張量
- size_average:(bool,optional) ,默認(rèn)下是True,在每個(gè)批處理中平均損失。如果是False,那就是對(duì)每個(gè)批處理的損失進(jìn)行求和。棄用
- ignore_index(int,optional),指定目標(biāo)值不影響輸入梯度,當(dāng)時(shí)size_average=TRUE,損失在非忽略的目標(biāo)上平均,默認(rèn)-100
- reduce(bool,optional)),默認(rèn)情況下,根據(jù)size_average,每個(gè)minibatch的觀察值平均或求和損失,當(dāng)reduce為False時(shí),將返回每個(gè)批處理元素的損失,并忽略size_average。默認(rèn)為True,棄用
- reduction(string,optional):指定要應(yīng)用到輸出的縮減,值為'none' | 'mean' | 'sum'。'none'表示不會(huì)降低, 'mean'表示求平均,'sum'表示輸出求和,默認(rèn)是mean
Example
input = torch.randn(3,5,requires_grad=True)
target = torch.tensor([1, 0, 4])
output = F.nll_loss(F.log_softmax(input), target)
output.backward()
參考: