多分类模型训练使用交叉熵损失的一个注意的点

使用交叉熵损失的网络模型最后一层不要用softmax,交叉熵损失函数会在计算的时候做softmax,如果用了会导致模型训练异常,

如果模型最后一层有softmax,则损失函数要写成

loss_fun = nn.NLLLoss()
x = model(data)
loss = loss_fun(torch.log(x), label)

 

posted @ 2023-06-25 10:44  Liang-ml  阅读(39)  评论(0)    收藏  举报