Inference when output sigmoid is within BCEWithLogitsLoss

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?

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

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

Is model everything except BCEWithLogitsLoss?

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)
1 Like