liveBook · Manning

Output layer

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

An output layer is the final neural-network component that transforms internal representations into task-specific predictions, scores, or other outputs.

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

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

How deep it goes
Works through layer sizes, Swift output propagation, classification, and output-layer deltas; does not derive the backpropagation formula.
Examples
Uses animal-image scores, three-species iris classification, and a three-class wine classifier.
What it assumes
Assumes comfort with Swift functions, arrays, and floating-point values while introducing the roles of input, hidden, and output layers.
What you can do after
Build it from scratch in Swift by sizing output neurons, propagating values, interpreting the largest output, and calculating training deltas.
How it connects
Connects the output layer to hidden layers, activation functions, backpropagation, and classification.

Also explained in

Compare how 6 books explain output layer →

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.