How LLMs work

LLMs
Coming soon
Implementation
Coming soon

Unblock all features

Sign in to track progress, get AI help and save conversations.

ML fundamentals

Training

Training is a process of adjusting parameters automatically based on the training set. Let's find out how exactly that works.

Goal

We want to minimise the loss function.

For our current logistic regression model with 30 features the loss function consists of three parts:

L=1ni=1n[yiloge(y^i)+(1yi)loge(1y^i)]L = -\frac{1}{n} \sum_{i=1}^{n} \big[ y_i \log_e(\hat{y}_i) + (1 - y_i) \log_e(1 - \hat{y}_i) \big] yi=11+eziy_i = \frac{1}{1 + e^{-z_i}} zi=w0+w1xi,1+w2xi,2+...+w30xi,30z_i = w_0 + w_1 x_{i, 1} + w_2 x_{i, 2} + ... + w_{30} x_{i, 30}

If we combine these three parts into a single loss function L(w0,w1,...,w30)L(w_0, w_1, ..., w_{30}), we will see that it depends only on parameters as the xx and yy values come from the training set.

We need to find the combination of ww values which gives the lowest value of LL.

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 yy changes relative to a tiny (infinitesimally small) change in xx.

dydx=limh0f(x+h)f(x)h\frac{dy}{dx} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}
  • The dydx\frac{dy}{dx} is a notation for derivative
  • The hh represents that tiny change in the input

For example, the derivative of a function y=x2y=x^2 is dydx=2x\frac{dy}{dx} = 2x, because

dydx=limh0(x+h)2x2h=limh0x2+2xh+h2x2h=limh02xh+h2h=limh0h(2x+h)h=limh0(2x+h)=2x\begin{aligned} \frac{dy}{dx} &= \lim_{h \to 0} \frac{(x+h)^2 - x^2}{h} \\ &= \lim_{h \to 0} \frac{x^2 + 2xh + h^2 - x^2}{h} \\ &= \lim_{h \to 0} \frac{2xh + h^2}{h} \\ &= \lim_{h \to 0} \frac{h(2x + h)}{h} \\ &= \lim_{h \to 0} (2x + h) \\ &= 2x \end{aligned}
  • The curve is flat at x=0x = 0 since dydx=0\frac{dy}{dx} = 0
  • For positive xx, the function grows faster as xx gets bigger. For example, at x=3x = 3, a small increase in xx will result in about six times larger increase in yy
  • For negative xx, the derivative is negative, meaning that a small increase in xx will decrease yy
0510152025-5-4-3-2-1012345y = x²xy

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.

dLdwj=dLdy^dy^dzdzdwj\frac{dL}{dw_j} = \frac{dL}{d\hat{y}} \cdot \frac{d\hat{y}}{dz} \cdot \frac{dz}{dw_j}

If we do the calculation applying the definition of the derivative to each function, it will result in a very clean form (and using ee constant in the sigmoid and the logarithm contributes to that):

  • dLdw0=1ni=1n(y^iyi)\frac{dL}{dw_0} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)
  • dLdw1=1ni=1n(y^iyi)xi,1\frac{dL}{dw_1} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i) \cdot x_{i, 1}
  • ...
  • dLdw30=1ni=1n(y^iyi)xi,30\frac{dL}{dw_{30}} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i) \cdot x_{i, 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ηdLdwiw_{i_\text{new}} = w_{i_\text{old}} - \eta \cdot \frac{dL}{dw_i}

The η\eta 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 (w0w_0 is predefined as -6) and a small training set.

y^=σ(6+w1x1)\hat{y} = \sigma(-6 + w_1 \cdot x_1)

See how the learning rate value affects how the model reaches the minimum.

0123012345minw₁Loss (L)
00.51012345x₁ (number of !)y
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)\hat{y}_i = \sigma(w_0 + w_1 x_{i,1} + ... + w_{30} x_{i,30})
    • Compute derivatives with respect to each parameter (this is called a backward pass)
      • dLdw0=1ni=1n(y^iyi)\frac{dL}{dw_0} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i)
      • dLdw1=1ni=1n(y^iyi)xi,1\frac{dL}{dw_1} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i) \cdot x_{i,1}
      • ...
      • dLdw30=1ni=1n(y^iyi)xi,30\frac{dL}{dw_{30}} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i) \cdot x_{i,30}
    • Update all parameters:
      • w0new=w0oldηdLdw0w_{0_\text{new}} = w_{0_\text{old}} - \eta \cdot \frac{dL}{dw_0}
      • w1new=w1oldηdLdw1w_{1_\text{new}} = w_{1_\text{old}} - \eta \cdot \frac{dL}{dw_1}
      • ...
      • w30new=w30oldηdLdw30w_{{30}_\text{new}} = w_{{30}_\text{old}} - \eta \cdot \frac{dL}{dw_{30}}

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:

ParameterValue
w₀-3.479
w₁0.440
w₂2.319
Spam score (y)
0.99