Accuracy is the share of test inputs a classifier assigns to the correct category. For a trained neural network, compare each predicted category with its expected category, count the matches, and divide by the number of test inputs. Multiply that fraction by 100 to display a percentage. section 7.5
/// for generalized results that require classification
/// this function will return the correct number of trials
/// and the percentage correct out of the total
func validate<T: Equatable>(inputs:[[Double]], expecteds:[T],
interpretOutput: ([Double]) -> T)
-> (correct: Int, total: Int, percentage: Double) {
var correct = 0
for (input, expected) in zip(inputs, expecteds) {
let result = interpretOutput(outputs(input: input))
if result == expected {
correct += 1
}
}
let percentage = Double(correct) / Double(inputs.count)
return (correct, inputs.count, percentage)
}
}
The Swift `validate()` function runs each input through the network, converts its numeric output into a comparable category with `interpretOutput`, and returns `(correct, total, percentage)`. Unlike `train()`, it does not update weights. section 7.5
To measure accuracy on a classification problem:
- Train the network on inputs paired with expected outputs, then reserve other inputs and their labels for testing. section 7.5 section 7.6
- Provide an `interpretOutput` function that turns the network’s floating-point outputs into the same kind of label as the expected result. For iris classification, it selects a species from three output positions. section 7.5 section 7.6
- Pass the test inputs, expected labels, and interpreter to `validate()`. Report both the count correct and the percentage, so the size of the test is visible. section 7.5 section 7.6
Two classification checks
An iris network trained on 140 shuffled specimens is tested on the remaining 10; a sample result is 9 correct of 10, or 90%. A wine classifier reports 27 correct of 28, or about 96.43%. These percentages describe the tested inputs, not the training error printed during weight updates. section 7.6 section 7.5
Results can change between runs because the network starts with random weights. If accuracy is suboptimal, experiment with the learning rate, hidden-layer size, or number of training iterations. Another exercise is to change the activation function and check how that affects accuracy and training time. section 7.6 section 7.9































