# Weight decay regularisation

Most machine learning techniques follow a similar strategy:

2. Generalise by testing the model on the test dataset

The test dataset consists of data that are never used during training and it allows to test how the algorithm will perform over “not seen before” data.

Step 1 can be seen as addressing “underfitting” and step 2 as addressing “overfitting“.

We address “underfitting” by making sure our model can map over the training set with a “good enough” accuracy.

If we use a first degree linear regression over our training set we might not be able to fit the dataset correctly. In this case we suffer from underfitting.

$$f(x) = wx + b$$

In order to address this issue we may introduce higher-degree terms.

$$f(x) = \sum\limits_{i=0}^{deg} w_{i}x^i$$

Note: We introduce higher degree terms and the input-output mapping is no longer linear. however the optimisation problem remains linear as the cost function is linear with respect to the model parameters $$w$$.

This allow to predict non-linear relations. However has we increase the polynomial degree we may end up with a model that performs greatly over the training dataset but poorly over the test data. That’s “overfitting“.

Using higher degree terms we can go through almost all of the datapoints in the training set. However the “shape” of this function is very specific to the training set and is not a good representation of the underlying distribution. (This is especially true as the training datasets is small).

We can alter our algorithm to favour lower degree terms or favour the lowest parameter values.

In linear regression this is done by introducing a correction term $$\lambda$$ into the cost function $$J(w)$$.

$$J(w) = MSE_{train} + \lambda w^\top w$$

The $$\lambda$$ term is known as the regularisation term. It makes sure that the bigger the weights, the bigger the cost. As we minimise the cost function we favour lower weights. This technique is called “weight decay“.

$$\lambda$$ is an “hyperparameter” of the linear regression. It is not a model parameter because its value is not optimised by the training process. Instead its value is chosen upfront and influences the optimisation process.

Similarly the degree of the polynomial used for linear regression is another hyperparameter. It is chosen upfront and influences the model “capacity” (i.e. The ability for the model to respond to complex datapoint distribution).
{"ft_lang_label":"__label__en","ft_lang_prob":0.9126772,"math_prob":0.9920898,"size":2457,"snap":"2023-14-2023-23","text_gpt3_token_len":532,"char_repetition_ratio":0.12556054,"word_repetition_ratio":0.010075566,"special_character_ratio":0.21571022,"punctuation_ratio":0.06787331,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9977462,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-05T03:06:58Z\",\"WARC-Record-ID\":\"<urn:uuid:de451ae3-881a-407e-94be-506c178b51d2>\",\"Content-Length\":\"106051\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b6b92fd8-ae20-45c3-b5c2-2486df3dfb98>\",\"WARC-Concurrent-To\":\"<urn:uuid:4f2aaab0-bd8f-4a27-a5f2-59fdf37a5aec>\",\"WARC-IP-Address\":\"77.92.69.132\",\"WARC-Target-URI\":\"https://beyondthelines.net/machine-learning/weight-decay-regularisation/\",\"WARC-Payload-Digest\":\"sha1:J2UIGQVL52FDDWYKW4YNYONPRFBOXBDS\",\"WARC-Block-Digest\":\"sha1:FYA4QTA6WDWJKKGZFIVXCLPHJJMSDKAR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224650620.66_warc_CC-MAIN-20230605021141-20230605051141-00720.warc.gz\"}"}