C: training a perceptron until it makes no mistakes

A perceptron outputs 1 if the weighted sum of its inputs plus a bias is greater than zero, otherwise 0. The learning rule changes the weights only when the output is wrong: each weight changes by the error times its input, and the bias by the error. With integer weights and a learning rate of 1, every number in the run is exact.

C
#include <stdio.h>

int main(void) {
  int inputs[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  int targets[4] = {0, 0, 0, 1};
  int w1 = 0, w2 = 0, bias = 0;

  int epochs = 0;
  int mistakes = 1;
  while (mistakes > 0) {
    mistakes = 0;
    for (int i = 0; i < 4; i++) {
      int sum = w1 * inputs[i][0] + w2 * inputs[i][1] + bias;
      int output = sum > 0 ? 1 : 0;
      int error = targets[i] - output;
      if (error != 0) {
        w1 += error * inputs[i][0];
        w2 += error * inputs[i][1];
        bias += error;
        mistakes++;
      }
    }
    epochs++;
  }
  printf("epochs: %d\n", epochs);
  printf("weights: w1=%d w2=%d bias=%d\n", w1, w2, bias);
  for (int i = 0; i < 4; i++) {
    int sum = w1 * inputs[i][0] + w2 * inputs[i][1] + bias;
    printf("%d AND %d = %d\n", inputs[i][0], inputs[i][1], sum > 0 ? 1 : 0);
  }
  return 0;
}
Read the full lesson