45. Your First Neural Network in PyTorch
45.2 Loss and Optimizer
Why. The model alone does not learn — you need a loss and an optimizer.
What. For binary targets as 0/1 floats, BCEWithLogitsLoss pairs well with a raw score output. torch.optim.Adam or SGD updates weights.
How.
Wire loss + optimizer
- Create the model from 45.1.
- Pick
BCEWithLogitsLoss. - Pick Adam with a small lr (e.g. 0.01).
import torch.nn as nn
import torch.optim as optim
model = nn.Sequential(nn.Linear(3, 8), nn.ReLU(), nn.Linear(8, 1))
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
print(criterion, optimizer)