liveBook · Manning

Objective function

As explained in Natural Language Processing in Action, Second Edition

Loss is a numerical measure of discrepancy between a model's prediction and a desired target, with lower values generally indicating better agreement.

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

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines loss through an analogy, compares task-specific losses, and works through PyTorch optimization; defers detailed mean squared error treatment.
Examples
Uses birth-certificate name classification, frequency-weighted binary cross-entropy, and printed training and test measurements.
What it assumes
Builds on logistic regression and numerical feature vectors while introducing loss with a test-grade analogy.
What you can do after
Apply it via PyTorch to select a classification loss, update weights, and monitor training and test results.
How it connects
Links loss to binary cross-entropy, backpropagation, gradients, stochastic gradient descent, and learning rates.

Also explained in

Compare how 30 books explain objective function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.