An objective function gives a model a numerical goal. In training, that goal is commonly expressed as a loss function: a score for how wrong its predictions are. Lower loss means the predictions better match the supplied targets. Think of loss as the percentage of questions a student got wrong, and an objective score as the grade earned. section 5.1.5 section 5.3
Choose a loss that suits the prediction task. Mean squared error is a candidate for regression; binary cross-entropy is commonly used for binary classification. Cross-entropy gives a larger penalty to a confidently wrong prediction. For name classification using birth-certificate labels, the loss can also be weighted by how frequently each name occurs with a recorded label. section 5.2.5
Train a PyTorch classifier
A neuron takes character n-gram features from names and returns a probability through a sigmoid activation. Set up `torch.nn.BCELoss` for the labeled examples, then repeat: clear gradients, predict, calculate loss, call `loss.backward()`, and update weights with an SGD optimizer. Track training loss alongside test loss and test accuracy; a falling training loss alone does not report how well the classifier performs on held-out names. section 5.2.4 section 5.2.5
>>> from tqdm import tqdm >>> num_epochs = 200 >>> pbar_epochs = tqdm(range(num_epochs), desc='Epoch:', total=num_epochs, ascii=' =') >>> for epoch in pbar_epochs: ... optimizer.zero_grad() #1 ... outputs = model(X_train) ... loss_train = loss_func_train(outputs, y_train) #2 ... loss_train.backward() #3 ... optimizer.step() #4 Epoch:: 100%|================================| 200/200 [00:04<00:00, 42.84it/s] 96.26it/s]
The PyTorch loop shows where the loss is calculated, differentiated, and used to update the model. section 5.2.5
The optimizer changes weights to reduce loss. A gradient indicates how each weight affects error; backpropagation computes those gradients across neurons. Picture the loss across possible weights as a slope: training seeks lower ground, often using an error aggregated over a batch rather than a single prediction. The same idea extends to CNN training, where the learning rate and optimizer govern each update. section 5.2.4 section 5.3 section 7.5.4
An objective function can also score choices without training a model. A chatbot can combine features of its generated responses to evaluate and select a reply according to its dialog goals. In either role, the score is useful only when it represents what the system is meant to do. section 1.8





























