Inference when output sigmoid is within BCEWithLogitsLoss


(Max) #1

BCEWithLogitsLoss() combines my binary classifier’s output Sigmoid layer and the BCELoss. Having the model trained and switched to .eval(), how do I obtain sigmoid output for given sample[s]?

For an inference on a single sample, I could provide a target of 1 and reconstruct the sigmoid output from the loss value. But what could I do with a batch of samples?


#2

You can just pass the raw logits to torch.sigmoid to get the probabilities:

outputs = model(data)
probs = torch.sigmoid(outputs)

(Max) #3

Is model everything except BCEWithLogitsLoss?


#4

Yes, BCEWithLogitsLoss would be the criterion, so your training loop most likely looks like this:

criterion = nn.BCEWithLogitsLoss()
model = MyModel()

for data, target in loader:
    optimizer.zero_grad()
    output = model(data)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
    
    preds = torch.sigmoid(output)