liveBook · Manning

Training loop

As explained in Fight Fraud with Machine Learning

A model training loop is an iterative procedure that updates a model using training data and evaluates its learning progress across repeated steps or epochs.

A training loop repeatedly processes training data, computes model predictions and loss, backpropagates errors, and updates parameters across epochs. Validation runs measure learning without changing parameters; an untouched test set provides the final performance check. section 6.3.3 section 8.2.2

A practical loop follows these steps:

  • Set the device, model, number of epochs, loss function, optimizer, learning rate, and any regularization such as weight decay. section 8.2.2 section 9.2.3
  • Enter training mode, iterate over batches or the training graph, clear gradients, run a forward pass, calculate loss, backpropagate, and update parameters. section 6.3.3 section 9.2.3
  • Enter evaluation mode and run the validation data without backpropagation; record loss, accuracy, or F1 across epochs as learning curves. section 8.2.2 section 9.2.3
  • Evaluate once on unseen test data after training, because decreasing training loss and improving validation metrics do not replace a final generalization check. section 8.2 section 9.3.4

Voice-faking detection

A PyTorch transformer trains for 10 epochs with binary classification loss and Adam at a learning rate of 0.001. Each epoch updates parameters on audio batches, then measures validation accuracy without gradients; reported validation accuracy rises from 82.66% to 96.74%. section 8.2.2

Listing 8.3 Training deep learning model using PyTorch
num_epochs = 10
for epoch in range(num_epochs):
    model.train()  #A
    running_loss = 0.0
    for i, (inputs, labels) in enumerate(train_loader):
        inputs, labels = inputs.to(device), labels.to(device)
        optimizer.zero_grad()  #B
        outputs = model(inputs)  #C
        loss = criterion(outputs, labels)  #D
        loss.backward()  #E
        optimizer.step()  #F
        running_loss += loss.item()
    print(f"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss/len(train_loader)}")
        model.eval()  #G
    correct = 0
    total = 0
    with torch.no_grad():
        for inputs, labels in val_loader:
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model(inputs)
            _, predicted = torch.max(outputs, 1)  #H
            total += labels.size(0)
            correct += (predicted == labels).sum().item()  #I
    print(f'Validation Accuracy: {100 * correct / total} %')

Inspect the complete PyTorch pattern for device transfer, training mode, batch iteration, gradient updates, evaluation mode, and validation predictions. section 8.2.2

How Fight Fraud with Machine Learning covers it

How deep it goes
Explains executable training and validation steps, metrics, and learning curves, but defers model architecture and broader test interpretation.
Examples
Uses receipt-image segmentation, transformer-based voice-faking detection, MLP transaction classification, and GCN modeling on the Elliptic Bitcoin dataset.
What it assumes
Assumes a defined model, dataset loaders or graph masks, loss function, optimizer, and hyperparameters before the loop begins.
What you can do after
Build it from scratch in PyTorch, monitor training and validation metrics, and evaluate trained models on unseen data.
How it connects
Connects forward propagation, loss functions, backpropagation, optimizers, epochs, validation, test sets, learning curves, and overfitting.

Also explained in

Compare how 7 books explain training loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fight Fraud with Machine Learning. The section links point to the parts of the book it draws on.