If we combine these three parts into a single loss function L(w0,w1,...,w30), we will see that it depends only on parameters as the x and y values come from the training set.
We need to find the combination of w values which gives the lowest value of L.
Gradient descent
We will use gradient descent algorithm to achieve the goal.
The idea is to pick random values for parameters first.
Then adjust them slightly in the right direction, so the loss value is less than before.
Repeat it multiple times, and gradually approach a minimum.
Derivatives
The algorithm sounds simple, but how exactly to adjust the parameters values in each step to make the loss value smaller?
Gradient descent uses derivatives for that.
Derivatives tell us how much the value of a function y changes relative to a tiny (infinitesimally small) change in x.
dxdy=h→0limhf(x+h)−f(x)
The dxdy is a notation for derivative
The h represents that tiny change in the input
For example, the derivative of a function y=x2 is dxdy=2x, because
For positive x, the function grows faster as x gets bigger. For example, at x=3, a small increase in x will result in about six times larger increase in y
For negative x, the derivative is negative, meaning that a small increase in x will decrease y
Chain rule
Our loss function is more complex.
As it consists of three functions we calculate the derivative for each function separately and multiply the results.
This is called a chain rule.
dwjdL=dy^dL⋅dzdy^⋅dwjdz
If we do the calculation applying the definition of the derivative to each function, it will result in a very clean form (and using e constant in the sigmoid and the logarithm contributes to that):
dw0dL=n1∑i=1n(y^i−yi)
dw1dL=n1∑i=1n(y^i−yi)⋅xi,1
...
dw30dL=n1∑i=1n(y^i−yi)⋅xi,30
Updating the parameters
Knowing the derivatives values, we can get an idea of how to change the parameters to decrease the loss.
If the value of a derivative with respect to a parameter is positive, we should decrease the parameter
If the value of a derivative with respect to a parameter is negative, we should increase the parameter.
In other words, we should step in the opposite direction of the derivative:
winew=wiold−η⋅dwidL
The η here is a learning rate.
It is needed to control the step size.
If the step is too big, we might pass the minimum.
If it is too small, we might need to make many steps before reaching the minimum which makes the training slow.
To better illustrate it, let's look at a model with only one feature, one parameter (w0 is predefined as -6) and a small training set.
y^=σ(−6+w1⋅x1)
See how the learning rate value affects how the model reaches the minimum.
y = σ(-6 + 4.5·x₁)
step 0
w₁ = 4.5
loss = 0.65
derivative = 0.42
Complete algorithm
Putting it all together, for our model with 30 features and 900 messages in the training set, the complete algorithm is:
Start with random values for all parameters
Repeat several times (one complete step is called an epoch and the total number of epochs is adjustable):
Compute predictions for all training examples (this is called a forward pass)
y^i=σ(w0+w1xi,1+...+w30xi,30)
Compute derivatives with respect to each parameter (this is called a backward pass)
dw0dL=n1∑i=1n(y^i−yi)
dw1dL=n1∑i=1n(y^i−yi)⋅xi,1
...
dw30dL=n1∑i=1n(y^i−yi)⋅xi,30
Update all parameters:
w0new=w0old−η⋅dw0dL
w1new=w1old−η⋅dw1dL
...
w30new=w30old−η⋅dw30dL
Testing the model
Running this algorithm on all 900 training examples, we reach the 0.11 training error mentioned earlier.
The generalisation error is 0.14 and the accuracy on the test set is 93%.
Here are the adjusted parameters values and how the model behaves on any text message: