Path: blob/master/notebooks/book1/11/supplementary/poly_regression_torch.ipynb
1193 views
Kernel: Python 3
Polynomial regression in 1d.
Based on sec 4.4 of http://d2l.ai/chapter_multilayer-perceptrons/underfit-overfit.html
In [1]:
Data
Make some data using this function:
In [2]:
In [3]:
Out[3]:
tensor([ 5.0000, 1.2000, -3.4000, 5.6000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000])
Train/eval loop
In [4]:
In [5]:
In [6]:
In [7]:
Degree 3 (matches true function)
Train and test loss are similar (no over or underfitting), Loss is small, since matches true function. Estimated parameters are close to the true ones.
In [8]:
Out[8]:
weight: [[ 4.966272 1.7361481 -3.319803 4.646927 ]]
Degree 1 (underfitting)
In [9]:
Out[9]:
weight: [[3.4398355 3.7907531]]
Degree 20 (overfitting)
According to the D2L book, the test loss is higher than training loss. However, SGD itself has a regularizing effect (even in full batch mode), so I cannot reproduce overfitting (even though it would occur using a second order optimizer).
In [10]:
Out[10]:
weight: [[ 4.93541479e+00 1.33025610e+00 -3.02163672e+00 4.94317865e+00
-8.01379859e-01 1.77049351e+00 -2.12022990e-01 4.42835212e-01
1.22528784e-01 6.39166236e-02 -1.53294966e-01 1.37772217e-01
-2.55810306e-03 -9.48211923e-02 1.58452168e-01 1.67681873e-01
-7.38579556e-02 -6.81523979e-02 -4.71159853e-02 1.46061331e-01]]
In [ ]: