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























