Will torch.exp cut off compute graph?

I want to implement this function: x / ( 1 - e^(-x )). However, after I’ve done that, all of my loss became nan. I don’t know what happen to me :sob:

and here is my code:

class MyAttn(nn.Module):
    def __init__(self, channel, reduction=16):
        super(MyAttn, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            spectral_norm(nn.Linear(channel, channel // reduction, bias=False)),
            nn.ReLU(inplace=True),
            spectral_norm(nn.Linear(channel // reduction, channel, bias=False)),
        )

    def forward(self,  x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y)
        
        energy = y / (1  - torch.exp(-y))
        energy = energy.view(b, c, 1, 1)
        return x * energy.expand_as(x)

Hi,

exp will no break the graph.
But given the function, if x is 0 then you will end up dividing by 0 and will get bad numbers. Can you check that this is not the cause of your problem?

1 Like

thanks a lot!
but what should I do if I just want to make dividing operation?

but what should I do if I just want to make dividing operation?

Not sure what you mean by that?
In general, you need to make sure you don’t divide by 0 (by either clamping to eps or adding eps to before the division).