liveBook · Manning

Learning rate

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

A learning rate is a training hyperparameter that controls the size of parameter updates made while fitting a model.

A learning rate controls how far a neural network’s weights move during each training update. A neuron’s delta supplies the direction of the change; the learning rate scales its size. Finding a useful value for an unfamiliar problem takes trial and error. section 7.2

In a Swift network, the update follows three steps:

  • Run the inputs forward and cache each layer’s outputs. section 7.5
  • Compare the output with the expected result and calculate neuron deltas by backpropagation. This step does not change weights. section 7.5
  • For each weight outside the input layer, add learningRate × previous-layer output × neuron delta to its existing value. section 7.5
    /// backpropagate() doesn't actually change any weights
    /// this function uses the deltas calculated in backpropagate()
    /// to actually make changes to the weights
    func updateWeights() {
        for layer in layers.dropFirst() { // skip input layer
            for neuron in layer.neurons {
                for w in 0..<neuron.weights.count {
                    neuron.weights[w] = neuron.weights[w] +
                     (neuron.learningRate * (layer.previousLayer?
                     .outputCache[w])! * neuron.delta)
                }
            }
        }
    }

The Swift updateWeights() implementation shows exactly where each neuron’s learningRate scales a weight change. section 7.5

Tune against classification results

An iris classifier starts with Network(layerStructure: [4, 6, 3], learningRate: 0.3). It trains on 140 shuffled examples for 20 passes, then checks the remaining 10. A wine classifier uses [13, 7, 3] and a learning rate of 0.9. If accuracy is suboptimal, try changing the learning rate alongside the hidden-layer size and number of training passes. Random starting weights can make results vary between runs. section 7.6

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

How deep it goes
Shows the executable update using cached outputs and neuron deltas, while deferring the partial-derivative justification.
Examples
Uses iris classification with a learning rate of 0.3 and wine classification with 0.9.
What it assumes
Assumes Swift familiarity while building neurons, layers, and a network incrementally.
What you can do after
Build it from scratch in Swift and tune the learning rate against held-out classification results.
How it connects
Links gradient descent, backpropagation, neuron deltas, and classification accuracy.

Also explained in

Compare how 16 books explain learning rate →

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.