Hi Guys,
So I am struggling with my model, my loss sometimes explodes and I don’t understand why.
This is the model that goes crazy:

class MLPgocrazy(torch.nn.Module):
   def __init__(self, layersize, dropout):
       super(MLPgocrazy, self).__init__()
       self.f1=nn.Linear(layersize[0],layersize[1])
       self.f2=nn.Linear(layersize[1],layersize[2])
       self.f3=nn.Linear(layersize[2],layersize[3])
       self.f4=nn.Linear(layersize[3],layersize[4])
       self.f5=nn.Linear(layersize[4],layersize[5])
       self.bn1 = nn.BatchNorm1d(layersize[1])
       self.bn2 = nn.BatchNorm1d(layersize[2])
       self.bn3 = nn.BatchNorm1d(layersize[3])
       self.bn4 = nn.BatchNorm1d(layersize[4])
       self.bn5 = nn.BatchNorm1d(layersize[5])
       
       self.dropout= dropout
       
   def forward(self, x):

       x = F.relu(self.f1(x))
       #x = F.dropout(x, self.dropout, training=self.training)
       #x = self.bn1(x)
        
       x = F.relu(self.f2(x))
       #x = F.dropout(x, self.dropout, training=self.training)
       #x = self.bn2(x)
        
       x = F.relu(self.f3(x))
       #x = F.dropout(x, self.dropout, training=self.training)
      # x = self.bn3(x)
        
       x = F.relu(self.f4(x))
       #x = F.dropout(x, self.dropout, training=self.training)
       #fingerprint= x = self.bn4(x)
       fingerprint=1
       x = self.f5(x)
       return(x, fingerprint)

I wanted to see what happens, in most cases I have a decent loss but sometimes the loss explodes and is unreasonable high, so that I thought something is wrong with my loss function.
If I run this example:

torch.manual_seed(1234)
model = MLPgocrazy([2048,1000,1000,1000,1024,1],0.3)
model.cuda()


loss_function =nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(),lr=0.1)
loss_function2= nn.BCELoss()


eloss =[]
eloss2 =[]
for i in range(5):

    optimizer.zero_grad()    
    output, fp=model(cv_train_x[0][i].cuda())
    
    
    loss=loss_function(output, cv_train_y[0][i].cuda())
    eloss2.append(loss_function2(torch.sigmoid(output), cv_train_y[0][i].cuda()).detach().cpu().numpy())
    
    loss.backward()
        
    optimizer.step()
    eloss.append(loss.detach().cpu().numpy())

Then my eloss2 (BCELoss):
[array(0.69223803, dtype=float32),
 array(12.088572, dtype=float32),
 array(11.225102, dtype=float32),
 array(9.498163, dtype=float32),
 array(17.269388, dtype=float32)]


looks like this. But my eloss (BCEWithLogits) looks like this:
[array(0.69223803, dtype=float32),
 array(8991448., dtype=float32),
 array(1325363., dtype=float32),
 array(998557.4, dtype=float32),
 array(9826264., dtype=float32)]

I have no idea what causes that behavior.