# Training Neural Networks

### Early Stopping and Model complexity graph

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc0JAfmrJaVcZqoP4R%2Fimage.png?alt=media\&token=1feba1e7-e6f1-40cc-8350-4e20cf8c6ab7)

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc0SfFAgB2n8UJ4oGH%2Fimage.png?alt=media\&token=3288d016-82dd-46d7-86d4-e6b55cd96c1b)

This determines the number of epochs we should do.&#x20;

We do gradient descent until the testing error stops decreasing and starts to increase. at that moment we stop.&#x20;

**Exercise**

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc35n9pZQIXMffb8A9%2Fimage.png?alt=media\&token=78c0bcc0-86d9-443f-be5c-1ec9a6e1323b)

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc311SaPnmz5-L1tJV%2Fimage.png?alt=media\&token=f485d171-e18d-44de-9124-a561709e605e)

The model on the right will generate large errors, so it's going to be difficult for the model to tune and correct them. The function is very steep so it's hard to do gradient descent. The derivatives will all be close to 0 except for the middle part where they'll be very large.&#x20;

So how do we prevent this type of overfitting from happenning? We have to tweak the error function a little bit. We want to punish the high coefs. We take the old error function and add a term. 2 options:&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc4QWdiB2ZnJqvNoab%2Fimage.png?alt=media\&token=16c22a8b-cbf6-4153-b93d-e7c4377faa66)

Lambda param will tell us how much we want to penalize the large coefs.&#x20;



Small weights will tend to go to 0. If we want to reduce our weights and end up with a small set - L1. It's also good for feature selection as L1 will help us determine which ones are important.

L2 tries to keep weights reasonably small. Normally better for training models.&#x20;

Why? Taking the squared error of 0.5^2, 0.5^2 will give 0.5. Thus, L2 will prefer the vector (0.5, 0.5) over the vector (1,0) because it produces a smaller number&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-Llc4xZ7idpbXRs52YgG%2Fimage.png?alt=media\&token=a5b2d0a3-3587-4f9d-8fa6-ef17289562e9)

### Dropout

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-LlcEPhLLv_UI-iAp0V-%2Fimage.png?alt=media\&token=9bbd8443-38ad-46d6-a1b4-a17121327ec8)

Sometimes part of the network doesn't train because its weights are less important. We can turn off part of the network to allow the neglected part to train. We do this by randomly turning off the nodes as we pass through the epochs.&#x20;

We can give the algo a parameter, which is the prob that a node will get turned off during an epoch.&#x20;

### Local Minima, Random Restart and momemtum

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-LlcFZf20K4Uuv4pmKxB%2Fimage.png?alt=media\&token=3b62766f-3802-49b2-b11d-5c3800fa35c0)

If we hit a local minimal what can we do? Gradient descent alone will not help us. One way to help is random restart, and do gradient descent on all of them.&#x20;

**Momemtum:**

Momemtum is a constant Beta that varies between 0 and 1 and attaches to the previous steps of our gradient descent.&#x20;

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-LlbRv_FqXdK-ISeZuKu%2F-LlcKkzMxOJflHSkhBLx%2Fimage.png?alt=media\&token=480c99db-974a-404e-a19d-53e482c89b78)



### Stochastic Gradient Descent

We take small subsets of data run them through the NN and calculate the gradient based on those points and take a step in that direction. We still want to use all of our data. We do this batch by batch.&#x20;
