The perceptron does not need its weights to be set by hand. The perceptron learning algorithm adjusts them whenever the output is wrong, and it is guaranteed to succeed if the classes can be separated by a line.

The Rule

Start with small or zero weights and bias. For each training instance with input x and target t, compute the output y. Then update:

  • wᵢ ← wᵢ + η (t − y) xᵢ and b ← b + η (t − y),

where η is the learning rate. If the output is right, t − y = 0 and nothing changes. If the output is 0 but should be 1, the weights of active inputs are increased; if it is 1 but should be 0, they are decreased. One pass over all instances is an epoch. Training stops when an epoch has no errors, or after a fixed number of epochs.

The Convergence Theorem

The perceptron convergence theorem states that if the training data are linearly separable, the algorithm finds a separating line in a finite number of updates, for any positive learning rate. If the data are not separable, as with XOR, the weights keep changing forever, so we limit the number of epochs.

Limits and Next Steps

The perceptron gives only a hard class, not a probability, and finds any separating line rather than the best one. The support vector machine (Unit IV) improves on this by choosing the line with the widest margin. Multi-layer perceptrons trained by back-propagation handle non-linear problems.

Definition: Learning rate. A small positive number (η) that controls how large a step the weights take at each update of the learning algorithm.

Table 3.7: One update step of the rule

Case Target and output Effect on weights
Correct t = y No change
Missed positive t = 1, y = 0 Add η x to the weights, b increases
False alarm t = 0, y = 1 Subtract η x from the weights, b decreases

Example 3.7: Training a perceptron on AND

Problem: Train a perceptron on AND with η = 1, starting from w = (0, 0) and b = 0, taking the inputs in the order (0,0), (0,1), (1,0), (1,1). The output is 1 when z > 0.

Solution:

  1. Epoch 1: the first three inputs give z = 0 (output 0, correct). For (1, 1) the output is 0 but t = 1, so w = (1, 1) and b = 1.
  2. Epoch 2: (0,0) gives z = 1, output 1 but t = 0, so b = 0. (0,1) gives z = 1, wrong, so w = (1, 0) and b = −1. (1,0) gives z = 0, correct. (1,1) gives z = 0, wrong, so w = (2, 1) and b = 0.
  3. Epochs 3 and 4 make further corrections, and epoch 5 ends with w = (2, 1) and b = −2.
  4. Epoch 6 has no errors, so training stops.

Result: The learned weights are w = (2, 1) and b = −2, which implements AND.

data = [((0, 0), 0), ((0, 1), 0), ((1, 0), 0), ((1, 1), 1)]
w, b, eta = [0, 0], 0, 1
for epoch in range(1, 11):
    errors = 0
    for (x1, x2), t in data:
        y = 1 if w[0] * x1 + w[1] * x2 + b > 0 else 0
        if y != t:
            w[0] += eta * (t - y) * x1
            w[1] += eta * (t - y) * x2
            b += eta * (t - y)
            errors += 1
    print("epoch", epoch, "errors", errors, "w", w, "b", b)
    if errors == 0:
        break

Output

epoch 1 errors 1 w [1, 1] b 1
epoch 2 errors 3 w [2, 1] b 0
epoch 3 errors 3 w [2, 1] b -1
epoch 4 errors 2 w [2, 2] b -1
epoch 5 errors 1 w [2, 1] b -2
epoch 6 errors 0 w [2, 1] b -2

Common mistakes

  • Using a learning rate of zero, so nothing is learned.
  • Running a perceptron on XOR and waiting for it to converge.
  • Updating the weights even when the output is correct.
  • Forgetting to update the bias together with the weights.

Key takeaways

  • Update: w ← w + η (t − y) x and b ← b + η (t − y).
  • No change when the output is right.
  • The algorithm converges for linearly separable data.
  • It never settles on non-separable data such as XOR.

Practice questions

  1. When are the perceptron weights changed?
  2. What does the convergence theorem guarantee?
  3. What is an epoch?

Answers to the practice questions

  1. Only when the output differs from the target.
  2. A separating line is found in finitely many updates for linearly separable data.
  3. One complete pass through the training data.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit III