45. Your First Neural Network in PyTorch
45.4 Predict with Sigmoid
Why. After training, convert logits to probabilities and decisions.
What. torch.sigmoid(logits) → probability. Threshold 0.5 → class 0/1 for a demo.
How.
Predict
- Set
model.eval(). - Use
torch.no_grad()so you do not build a graph. - Print probs and hard labels.
model.eval()
with torch.no_grad():
logits = model(X)
probs = torch.sigmoid(logits)
preds = (probs >= 0.5).float()
print("probs:\n", probs.round(decimals=3))
print("preds:\n", preds)
Ravindra Bagale's Tip
Before you make predictions, call model.eval() and run the code inside torch.no_grad(). Together they put the model in inference mode, which is different from training mode, so make this a strong habit.
Ravindra Bagale's Tip – मराठी
Predictions करण्याआधी model.eval() call करा आणि code torch.no_grad() च्या आत चालवा. हे दोन्ही मिळून model ला inference mode मध्ये ठेवतात, जो training mode पेक्षा वेगळा आहे, म्हणून ही सवय पक्की करा.
Ravindra Bagale's Tip – हिंदी
Predictions करने से पहले model.eval() call करो और code को torch.no_grad() के अंदर चलाओ. ये दोनों मिलकर model को inference mode में रखते हैं, जो training mode से अलग है, इसलिए यह आदत पक्की करो.