# A Neural Network in 11 lines of Python

A bare bones neural network implementation to describe the inner workings of back-propagation.

**Line 36:**Now we're getting to the good stuff! This is the secret sauce! There's a lot going on in this line, so let's further break it into two parts.

**First Part: The Derivative**

```
1. nonlin(l1,True)
```

If l1 represents these three dots, the code above generates the slopes of the lines below. Notice that very high values such as x=2.0 (green dot) and very low values such as x=-1.0 (purple dot) have rather shallow slopes. The highest slope you can have is at x=0 (blue dot). This plays an important role. Also notice that all derivatives are between 0 and 1.

**Entire Statement: The Error Weighted Derivative**

```
1. l1_delta = l1_error * nonlin(l1,True)
```

There are more "mathematically precise" ways than "The Error Weighted Derivative" but I think that this captures the intuition. l1_error is a (4,1) matrix. nonlin(l1,True) returns a (4,1) matrix. What we're doing is multiplying them "elementwise". This returns a (4,1) matrix l1_delta with the multiplied values.

When we multiply the "slopes" by the error, we are

**reducing the error of high confidence predictions**. Look at the sigmoid picture again! If the slope was really shallow (close to 0), then the network either had a very high value, or a very low value. This means that the network was quite confident one way or the other. However, if the network guessed something close to (x=0, y=0.5) then it isn't very confident. We update these "wishy-washy" predictions most heavily, and we tend to leave the confident ones alone by multiplying them by a number close to 0.

**Line 39:**We are now ready to update our network! Let's take a look at a single training example.

In this training example, we're all setup to update our weights. Let's update the far left weight (9.5).

**weight_update = input_value * l1_delta**

For the far left weight, this would multiply 1.0 * the l1_delta. Presumably, this would increment 9.5 ever so slightly. Why only a small ammount? Well, the prediction was already very confident, and the prediction was largely correct. A small error and a small slope means a VERY small update. Consider all the weights. It would ever so slightly increase all three.

However, because we're using a "full batch" configuration, we're doing the above step on all four training examples. So, it looks a lot more like the image above. So, what does line 39 do? It computes the weight updates for each weight for each training example, sums them, and updates the weights, all in a simple line. Play around with the matrix multiplication and you'll see it do this!

**Takeaways:**

So, now that we've looked at how the network updates, let's look back at our training data and reflect. When both an input and a output are 1, we increase the weight between them. When an input is 1 and an output is 0, we decrease the weight between them.

Thus, in our four training examples above, the weight from the first input to the output would

**consistently increment or remain unchanged**, whereas the other two weights would find themselves

**both increasing and decreasing across training examples**(cancelling out progress). This phenomenon is what causes our network to learn based on correlations between the input and output.

**Original Tutorial**

**Related:**