Pytorch Softmax 给出 nans 和负值作为输出

Pytorch Softmax giving nans and negative values as output

我在模型末尾使用 softmax。

然而,经过一些训练后,softmax 给出了负 probability.In 一些情况,我也遇到了 nans 作为概率。

我在搜索中找到的一个解决方案是使用归一化的 softmax……但是我找不到任何 pytorch imlpementaion 来解决这个问题。

有人可以帮忙告知是否有归一化的 softmax 可用,或者如何实现这一点,以便前向和反向传播顺利进行。

请注意,我已经在使用 torch.nn.utils.clip_grad_norm_(model.parameters(), 40) 来避免梯度爆炸

我正在使用 pytorch 1.6.0

Softmax 将始终 return 正结果,但它会跟踪其他结果:

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
print(input)
output = m(input)
output

输出:

tensor([[ 0.0983,  0.4150, -1.1342],
        [ 0.3411,  0.5553,  0.0182]])

tensor([[0.3754, 0.5152, 0.1094],
        [0.3375, 0.4181, 0.2444]])

您正在跟踪行数。 请注意如何

0.0983, 0.4150, -1.1342 你会得到 0.3411, 0.5553, 0.0182

表示0.4150是最大值

硬最大值(我们知道这是 max())将只是 return 最大值。

因此,如果您对 softmax 的结果为负,这是不可能的,您可能遇到了一些实现失败。