liveBook · Manning

Training

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Training is the process of fitting a machine-learning model by providing examples and adjusting its parameters to reduce output error.

A neural network does not start out knowing which output is right. Training gives it inputs with known answers, compares its actual outputs with those answers, and changes neuron weights to reduce the error. This is supervised machine learning: the correct answers must be supplied. section 7.2

For each training input, the network repeats a short cycle:

  • Produce an output and compare it with the expected output to find the error. section 7.2
  • Backpropagate the error, assigning changes to output and hidden-layer neurons. Neurons more responsible for the error are modified more heavily. section 7.2
  • Update weights using the calculated changes, prior inputs, and a chosen learning rate. Backpropagation happens during training, not when simply using the trained network. section 7.2

Train and test an iris classifier

Each iris sample supplies four measurements and a species label. In Swift, encode the three species as expected output arrays such as [1.0, 0.0, 0.0], then create a Network with layerStructure: [4, 6, 3] and learningRate: 0.3. After shuffling the CSV rows, train on 140 samples for 20 passes. Reserve the remaining 10 for validation, translating the largest output value back into a species name. One run reports 9 correct out of 10; that result describes the test, not a guaranteed accuracy. section 7.6

// train over first 140 irises in data set 20 times
let irisTrainers = Array(irisParameters[0..<140])
let irisTrainersCorrects = Array(irisClassifications[0..<140])
for _ in 0..<20 {
    irisNetwork.train(inputs: irisTrainers, expecteds: irisTrainersCorrects,
     printError: false)
}

The Swift training loop pairs each input with its expected classification and passes the same 140 examples through the network 20 times. section 7.6

Training can require substantial data and computation: even the iris example strains a Swift Playground, while an image classifier might need millions of labeled images. Using an already trained network is cheaper than training one. A trained model can therefore be used in an app without training there; the first version of Core ML supports this inference-only approach. To explore the tradeoffs, try another data set or activation function and compare accuracy and training needs. section 7.7 section 7.9

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Works through repeated training and validation in Swift, with backpropagation and weight updates explained without formal derivations.
Examples
Trains on iris measurements and wine samples, then checks predictions against reserved examples.
What it assumes
Assumes working familiarity with Swift arrays and functions; introduces labeled examples and explains backpropagation without requiring a formal mathematical derivation.
What you can do after
Build it from scratch with a Swift Network and validate iris or wine classifications on held-out samples.
How it connects
Connects supervised learning, backpropagation, learning rates, activation functions, feature scaling, classification, and validation.

Also explained in

Compare how 24 books explain training →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.