The output layer is the last layer of a feed-forward neural network. It receives signals from the preceding layer and produces floating-point values for an external caller to interpret. Its number of neurons depends on the result the network must produce: a classifier can use one output neuron per category and select the category with the highest output. section 7.2
Three-way iris classification
A Swift `Network(layerStructure: [4, 6, 3], learningRate: 0.3)` takes four flower measurements, passes them through six hidden neurons, and returns three outputs corresponding to the three iris species. Training targets encode each species as a three-element array such as `[1.0, 0.0, 0.0]`. `irisInterpretOutput(output:)` finds the largest value and returns its species name; `validate()` uses that interpretation to count correct classifications. section 7.6
In the Swift implementation, a non-input `Layer` maps each neuron’s `output(inputs:)` over the incoming values and caches the resulting array. `Network.outputs(input:)` passes that array through successive layers, so its final return value comes from the output layer. During training, the output layer also starts backpropagation: each neuron’s delta uses its expected value, its cached output, and its activation-function derivative. Earlier layers calculate their deltas from the following layer; weight updates happen afterward. section 7.5
// should only be called on an output layer
func calculateDeltasForOutputLayer(expected: [Double]) {
for n in 0..<neurons.count {
neurons[n].delta =
neurons[n].derivativeActivationFunction(neurons[n]
.outputCache) * (expected[n] - outputCache[n])
}
}
// should not be called on output layer
func calculateDeltasForHiddenLayer(nextLayer: Layer) {
for (index, neuron) in neurons.enumerated() {
let nextWeights = nextLayer.neurons.map { $0.weights[index] }
let nextDeltas = nextLayer.neurons.map { $0.delta }
let sumOfWeightsXDeltas = dotProduct(nextWeights, nextDeltas)
neuron.delta = neuron.derivativeActivationFunction(neuron
.outputCache) * sumOfWeightsXDeltas
}
}
}
The Swift methods show why output-layer deltas use expected values directly, while hidden-layer deltas use the next layer’s weights and deltas. section 7.5





