使用交叉熵损失的网络模型最后一层不要用softmax,交叉熵损失函数会在计算的时候做softmax,如果用了会导致模型训练异常,
如果模型最后一层有softmax,则损失函数要写成
loss_fun = nn.NLLLoss() x = model(data) loss = loss_fun(torch.log(x), label)
使用交叉熵损失的网络模型最后一层不要用softmax,交叉熵损失函数会在计算的时候做softmax,如果用了会导致模型训练异常,
如果模型最后一层有softmax,则损失函数要写成
loss_fun = nn.NLLLoss() x = model(data) loss = loss_fun(torch.log(x), label)