How to build a simple neural network in 9 lines of Python code

How to build a simple neural network in 9 lines of Python code

Diagram 1

Diagram 2

  1. Take the inputs from a training set example, adjust them by the weights, and pass them through a special formula to calculate the neuron’s output.
  2. Calculate the error, which is the difference between the neuron’s output and the desired output in the training set example.
  3. Depending on the direction of the error, adjust the weights slightly.
  4. Repeat this process 10, 000 times.

Diagram 3

Diagram 4

  1. We used the Sigmoid curve to calculate the output of the neuron.
  2. If the output is a large positive or negative number, it signifies the neuron was quite confident one way or another.
  3. From Diagram 4, we can see that at large numbers, the Sigmoid curve has a shallow gradient.
  4. If the neuron is confident that the existing weight is correct, it doesn’t want to adjust it very much. Multiplying by the Sigmoid curve gradient achieves this.

  • exp — the natural exponential
  • array — creates a matrix
  • dot — multiplies matrices
  • random — gives us random numbers