In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error, f1_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.neural_network import MLPRegressor, MLPClassifier

Multilayer Perceptron in Torch via Skorch¶

Dataset¶

The dataset was generated from a deep learning model trained on the Obesity or CVD risk dataset. https://www.kaggle.com/datasets/aravindpcoder/obesity-or-cvd-risk-classifyregressorcluster

The data consist of the estimation of obesity levels in people from the countries of Mexico, Peru and Colombia, with ages between 14 and 61 and diverse eating habits and physical condition , data was collected using a web platform with a survey where anonymous users answered each question, then the information was processed obtaining 17 attributes and 2111 records. The attributes related with eating habits are: Frequent consumption of high caloric food (FAVC), Frequency of consumption of vegetables (FCVC), Number of main meals (NCP), Consumption of food between meals (CAEC), Consumption of water daily (CH20), and Consumption of alcohol (CALC). The attributes related with the physical condition are: Calories consumption monitoring (SCC), Physical activity frequency (FAF), Time using technology devices (TUE), Transportation used (MTRANS) variables obtained : Gender, Age, Height and Weight.

NObesity values are:

  • Underweight Less than 18.5
  • Normal 18.5 to 24.9
  • Overweight 25.0 to 29.9
  • Obesity I 30.0 to 34.9
  • Obesity II 35.0 to 39.9
  • Obesity III Higher than 40
In [2]:
df = pd.read_csv("./Data/playground-series-s4e2/train.csv")
df
Out[2]:
id Gender Age Height Weight family_history_with_overweight FAVC FCVC NCP CAEC SMOKE CH2O SCC FAF TUE CALC MTRANS NObeyesdad
0 0 Male 24.443011 1.699998 81.669950 yes yes 2.000000 2.983297 Sometimes no 2.763573 no 0.000000 0.976473 Sometimes Public_Transportation Overweight_Level_II
1 1 Female 18.000000 1.560000 57.000000 yes yes 2.000000 3.000000 Frequently no 2.000000 no 1.000000 1.000000 no Automobile Normal_Weight
2 2 Female 18.000000 1.711460 50.165754 yes yes 1.880534 1.411685 Sometimes no 1.910378 no 0.866045 1.673584 no Public_Transportation Insufficient_Weight
3 3 Female 20.952737 1.710730 131.274851 yes yes 3.000000 3.000000 Sometimes no 1.674061 no 1.467863 0.780199 Sometimes Public_Transportation Obesity_Type_III
4 4 Male 31.641081 1.914186 93.798055 yes yes 2.679664 1.971472 Sometimes no 1.979848 no 1.967973 0.931721 Sometimes Public_Transportation Overweight_Level_II
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
20753 20753 Male 25.137087 1.766626 114.187096 yes yes 2.919584 3.000000 Sometimes no 2.151809 no 1.330519 0.196680 Sometimes Public_Transportation Obesity_Type_II
20754 20754 Male 18.000000 1.710000 50.000000 no yes 3.000000 4.000000 Frequently no 1.000000 no 2.000000 1.000000 Sometimes Public_Transportation Insufficient_Weight
20755 20755 Male 20.101026 1.819557 105.580491 yes yes 2.407817 3.000000 Sometimes no 2.000000 no 1.158040 1.198439 no Public_Transportation Obesity_Type_II
20756 20756 Male 33.852953 1.700000 83.520113 yes yes 2.671238 1.971472 Sometimes no 2.144838 no 0.000000 0.973834 no Automobile Overweight_Level_II
20757 20757 Male 26.680376 1.816547 118.134898 yes yes 3.000000 3.000000 Sometimes no 2.003563 no 0.684487 0.713823 Sometimes Public_Transportation Obesity_Type_II

20758 rows × 18 columns

In [3]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20758 entries, 0 to 20757
Data columns (total 18 columns):
 #   Column                          Non-Null Count  Dtype  
---  ------                          --------------  -----  
 0   id                              20758 non-null  int64  
 1   Gender                          20758 non-null  object 
 2   Age                             20758 non-null  float64
 3   Height                          20758 non-null  float64
 4   Weight                          20758 non-null  float64
 5   family_history_with_overweight  20758 non-null  object 
 6   FAVC                            20758 non-null  object 
 7   FCVC                            20758 non-null  float64
 8   NCP                             20758 non-null  float64
 9   CAEC                            20758 non-null  object 
 10  SMOKE                           20758 non-null  object 
 11  CH2O                            20758 non-null  float64
 12  SCC                             20758 non-null  object 
 13  FAF                             20758 non-null  float64
 14  TUE                             20758 non-null  float64
 15  CALC                            20758 non-null  object 
 16  MTRANS                          20758 non-null  object 
 17  NObeyesdad                      20758 non-null  object 
dtypes: float64(8), int64(1), object(9)
memory usage: 2.9+ MB
In [4]:
df.nunique()
Out[4]:
id                                20758
Gender                                2
Age                                1703
Height                             1833
Weight                             1979
family_history_with_overweight        2
FAVC                                  2
FCVC                                934
NCP                                 689
CAEC                                  4
SMOKE                                 2
CH2O                               1506
SCC                                   2
FAF                                1360
TUE                                1297
CALC                                  3
MTRANS                                5
NObeyesdad                            7
dtype: int64
In [5]:
# Split the data into features and target variable

target = "NObeyesdad"
y = df[target]
X = df.drop(columns=[target, "id"])
In [6]:
# Convert categorical variables to numeric using one-hot encoding

X[X.select_dtypes('object').columns] = X.select_dtypes('object').apply(pd.Categorical)
X = pd.get_dummies(X, drop_first=True).astype(np.float32).values
In [7]:
# Convert labels to numeric using LabelEncoder

le = LabelEncoder()
y = le.fit_transform(y)
np.unique(y), le.classes_
Out[7]:
(array([0, 1, 2, 3, 4, 5, 6]),
 array(['Insufficient_Weight', 'Normal_Weight', 'Obesity_Type_I',
        'Obesity_Type_II', 'Obesity_Type_III', 'Overweight_Level_I',
        'Overweight_Level_II'], dtype=object))
In [8]:
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Out[8]:
((16606, 22), (4152, 22), (16606,), (4152,))

Dummy Classifier¶

In [9]:
from sklearn.dummy import DummyClassifier

model = DummyClassifier(strategy="most_frequent")
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[9]:
0.06282803118308164

MLP with Sklearn¶

In [10]:
model =  MLPClassifier(
    hidden_layer_sizes=(32,),  
    solver="adam",  
    alpha=0.001, 
    max_iter=500,  
    random_state=42,  
    batch_size=2048,  
    shuffle=True,
    
    # parameters for early stopping
    early_stopping=True,  # whether to use early stopping to terminate training when validation score is not improving
    n_iter_no_change=50,  # maximum number of epochs to not meet tol improvement
    validation_fraction=0.1,  # proportion of training data to set aside as validation set for early stopping
)
In [11]:
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.
  warnings.warn(
Out[11]:
0.8338739595965879

MLP with Skorch¶

Skorch is a lightweight library that wraps PyTorch with a scikit-learn style interface. It lets you train neural networks, such as an MLP, using familiar methods like fit, predict, and score, while still keeping the flexibility of PyTorch for defining custom models and training logic.

For this notebook, Skorch is useful because it makes PyTorch models easier to integrate into standard machine learning workflows, including preprocessing pipelines, cross-validation, and hyperparameter tuning.

In [12]:
import torch
from torch import nn
import torch.nn.functional as F
from skorch import NeuralNetClassifier
from skorch.callbacks import EpochScoring
In [ ]:
# Define the MLP architecture. cpu will always work, but if you have a GPU you can use it to speed up training.
DEVICE = 'mps'
DEVICE = 'cuda'
DEVICE = 'cpu'

Vanilla¶

In [14]:
class VanillaMLP(nn.Module):
    def __init__(self, in_features, out_features):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(in_features, 32),
            nn.ReLU(),
            nn.Linear(32, out_features),
            nn.Softmax(dim=-1),  
        )

    def forward(self, X, **kwargs):
        return self.model(X)
First attempt: no scaling, mostly default parameters¶
In [15]:
net = NeuralNetClassifier(
    VanillaMLP,
    module__in_features=X_train.shape[1],
    module__out_features=len(le.classes_),

    max_epochs=200,
    lr=0.01,
    batch_size=2048,  # default is 128, but we can increase it for better performance on GPU

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
        ),
    ],
    device=DEVICE,
)
net
Out[15]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [16]:
net.fit(X_train, y_train)
  epoch    train_acc    train_loss    valid_acc    valid_loss     dur
-------  -----------  ------------  -----------  ------------  ------
      1       0.1431        4.2396       0.1942        1.9763  2.0555
      2       0.1973        1.9649       0.1671        1.9494  0.0686
      3       0.2054        1.9431       0.1942        1.9290  0.0739
      4       0.2138        1.9208       0.2008        1.9087  0.0683
      5       0.2298        1.9030       0.2026        1.8917  0.0672
      6       0.2053        1.8856       0.2050        1.8748  0.0655
      7       0.2090        1.8680       0.2083        1.8568  0.0655
      8       0.2145        1.8494       0.2092        1.8382  0.0678
      9       0.2190        1.8303       0.2137        1.8197  0.0664
     10       0.2209        1.8122       0.2146        1.8025  0.1078
     11       0.2238        1.7947       0.2143        1.7855  0.0720
     12       0.2255        1.7773       0.2164        1.7695  0.0719
     13       0.2291        1.7607       0.2191        1.7537  0.0699
     14       0.2326        1.7447       0.2243        1.7387  0.1487
     15       0.2389        1.7293       0.2255        1.7250  0.0663
     16       0.2505        1.7153       0.2402        1.7111  0.0670
     17       0.2585        1.7139       0.2065        1.8079  0.0781
     18       0.2471        1.7763       0.2634        1.7099  0.0842
     19       0.2617        1.7216       0.2080        1.7953  0.1189
     20       0.2679        1.7290       0.2435        1.7071  0.0804
     21       0.2778        1.7205       0.2306        1.7286  0.1005
     22       0.2852        1.7119       0.2547        1.6916  0.0750
     23       0.2906        1.7091       0.2267        1.7600  0.0698
     24       0.2958        1.6928       0.2565        1.6741  0.0708
     25       0.3019        1.6899       0.2321        1.7587  0.0750
     26       0.3063        1.6846       0.2643        1.6624  0.0881
     27       0.3107        1.6767       0.2357        1.7522  0.0780
     28       0.3128        1.6776       0.2703        1.6529  0.0762
     29       0.3146        1.6651       0.2508        1.7129  0.0720
     30       0.3193        1.6690       0.2884        1.6481  0.0680
     31       0.3214        1.6593       0.2559        1.7071  0.0670
     32       0.3226        1.6600       0.2842        1.6359  0.0691
     33       0.3219        1.6508       0.2610        1.6961  0.0682
     34       0.3260        1.6517       0.2941        1.6289  0.0681
     35       0.3245        1.6434       0.2601        1.6875  0.0680
     36       0.3278        1.6427       0.3004        1.6246  0.0672
     37       0.3266        1.6386       0.2628        1.6815  0.0676
     38       0.3296        1.6336       0.3323        1.6148  0.0709
     39       0.3379        1.6298       0.2631        1.6933  0.0707
     40       0.3321        1.6306       0.3209        1.6096  0.0671
     41       0.3376        1.6201       0.2613        1.6806  0.0672
     42       0.3348        1.6230       0.3453        1.6023  0.0719
     43       0.3424        1.6113       0.2667        1.6683  0.0690
     44       0.3381        1.6122       0.3609        1.5889  0.0693
     45       0.3469        1.6014       0.2691        1.6630  0.0689
     46       0.3397        1.6026       0.3799        1.5755  0.0677
     47       0.3528        1.5793       0.2818        1.6184  0.0680
     48       0.3434        1.5876       0.3597        1.5757  0.0669
     49       0.3463        1.5903       0.2845        1.6147  0.0767
     50       0.3454        1.5915       0.4127        1.5700  0.0687
     51       0.3604        1.5747       0.2706        1.6493  0.0703
     52       0.3493        1.5808       0.3949        1.5595  0.1871
     53       0.3583        1.5715       0.2763        1.6440  0.0699
     54       0.3504        1.5798       0.4208        1.5583  0.0708
     55       0.3649        1.5639       0.2818        1.6144  0.0706
     56       0.3517        1.5745       0.3874        1.5487  0.0713
     57       0.3630        1.5604       0.2893        1.6029  0.0696
     58       0.3554        1.5698       0.3934        1.5432  0.0666
     59       0.3659        1.5554       0.2932        1.5938  0.0675
     60       0.3578        1.5632       0.4214        1.5401  0.0663
     61       0.3735        1.5441       0.3016        1.5821  0.0681
     62       0.3587        1.5582       0.3811        1.5386  0.0666
     63       0.3692        1.5489       0.2926        1.5958  0.0683
     64       0.3628        1.5449       0.4133        1.5283  0.0676
     65       0.3733        1.5358       0.2917        1.6094  0.0662
     66       0.3628        1.5487       0.4160        1.5231  0.0660
     67       0.3772        1.5290       0.2998        1.5999  0.0679
     68       0.3652        1.5377       0.3986        1.5209  0.0734
     69       0.3704        1.5327       0.3049        1.6083  0.0765
     70       0.3683        1.5372       0.4275        1.5139  0.0706
     71       0.3755        1.5187       0.3137        1.5886  0.0789
     72       0.3696        1.5309       0.4028        1.5089  0.0795
     73       0.3723        1.5247       0.3116        1.5827  0.0855
     74       0.3683        1.5204       0.4118        1.5052  0.0695
     75       0.3718        1.5251       0.3260        1.5898  0.0668
     76       0.3709        1.5160       0.4064        1.5073  0.0698
     77       0.3560        1.5125       0.3733        1.5419  0.0680
     78       0.3736        1.4965       0.3715        1.5282  0.0672
     79       0.3699        1.4942       0.3874        1.5251  0.0683
     80       0.3789        1.4944       0.4091        1.5076  0.0685
     81       0.3739        1.4875       0.3173        1.5750  0.0679
     82       0.3436        1.5610       0.3691        1.5125  0.0685
     83       0.3408        1.5531       0.3028        1.5761  0.0677
     84       0.3540        1.5189       0.3916        1.5033  0.0692
     85       0.3585        1.5087       0.3570        1.4925  0.0681
     86       0.3619        1.4924       0.4040        1.5049  0.0690
     87       0.3628        1.4797       0.3793        1.5327  0.0676
     88       0.3730        1.4957       0.4094        1.4914  0.0672
     89       0.3662        1.4878       0.3299        1.5312  0.1449
     90       0.3556        1.4996       0.4061        1.4848  0.0750
     91       0.3506        1.5042       0.3251        1.5543  0.0709
     92       0.3511        1.5045       0.4052        1.4879  0.0702
     93       0.3557        1.4893       0.3591        1.4830  0.0688
     94       0.3622        1.4777       0.4067        1.5045  0.0678
     95       0.3727        1.4635       0.3835        1.5098  0.0751
     96       0.3547        1.4810       0.4359        1.4642  0.0674
     97       0.3621        1.5033       0.3140        1.5609  0.0673
     98       0.3491        1.5037       0.4181        1.4820  0.0674
     99       0.3523        1.4760       0.3805        1.4788  0.0671
    100       0.3683        1.4684       0.4115        1.4821  0.0667
    101       0.3633        1.4577       0.3766        1.5016  0.0693
    102       0.3508        1.4764       0.4314        1.4680  0.0703
    103       0.3676        1.4755       0.3528        1.4930  0.0705
    104       0.3631        1.4784       0.4031        1.4530  0.0686
    105       0.3519        1.4879       0.3082        1.4925  0.0676
    106       0.3744        1.4633       0.4232        1.4875  0.0684
    107       0.3799        1.4376       0.3931        1.4888  0.0673
    108       0.3463        1.4716       0.4259        1.4515  0.0679
    109       0.3515        1.4730       0.3260        1.5477  0.0979
    110       0.3520        1.5001       0.4281        1.4354  0.0680
    111       0.3658        1.4565       0.3928        1.4394  0.0675
    112       0.3823        1.4440       0.4223        1.4697  0.0688
    113       0.3840        1.4308       0.3856        1.4552  0.0671
    114       0.3553        1.4711       0.4205        1.4194  0.0677
    115       0.3683        1.4532       0.3338        1.5076  0.0673
    116       0.3479        1.4780       0.4241        1.4466  0.0680
    117       0.3795        1.4317       0.3986        1.4420  0.0775
    118       0.3785        1.4313       0.4214        1.4564  0.0675
    119       0.3739        1.4317       0.3967        1.4759  0.0677
    120       0.3515        1.4548       0.4202        1.4207  0.0687
    121       0.3675        1.4305       0.3606        1.4689  0.0677
    122       0.3627        1.4608       0.4127        1.4196  0.0664
    123       0.3664        1.4445       0.3245        1.5240  0.0682
    124       0.3463        1.4743       0.4278        1.4568  0.0681
    125       0.3930        1.4255       0.3688        1.4043  0.0682
    126       0.3858        1.4151       0.4118        1.4293  0.0709
    127       0.3921        1.3874       0.4070        1.4227  0.1473
    128       0.3816        1.4383       0.4416        1.4056  0.0762
    129       0.3666        1.4338       0.3254        1.5225  0.0709
    130       0.3613        1.4656       0.4199        1.4222  0.0721
    131       0.3701        1.4175       0.4422        1.4099  0.0697
    132       0.3838        1.4098       0.4013        1.4480  0.0704
    133       0.3836        1.3838       0.4067        1.4561  0.0702
    134       0.3706        1.4279       0.4019        1.4377  0.0694
    135       0.4073        1.3657       0.4211        1.3968  0.0694
    136       0.3535        1.4681       0.4253        1.3934  0.0688
    137       0.3692        1.4468       0.3070        1.5635  0.0686
    138       0.3595        1.4641       0.4193        1.4196  0.0681
    139       0.3820        1.3985       0.4467        1.3524  0.0720
    140       0.3960        1.4016       0.3992        1.4253  0.1147
    141       0.3877        1.3705       0.4184        1.4265  0.0798
    142       0.4007        1.4042       0.4205        1.4029  0.0709
    143       0.3803        1.4144       0.3766        1.4336  0.0710
    144       0.3635        1.4435       0.4259        1.4053  0.0699
    145       0.3846        1.4013       0.4250        1.3858  0.0698
    146       0.3643        1.4081       0.4049        1.4263  0.0715
    147       0.3971        1.3558       0.4175        1.3545  0.0697
    148       0.3829        1.3855       0.4214        1.4599  0.0701
    149       0.4026        1.3782       0.3958        1.4332  0.0703
    150       0.3666        1.4325       0.4133        1.4050  0.0693
    151       0.3711        1.4550       0.3636        1.4903  0.0684
    152       0.3531        1.4747       0.4040        1.4488  0.0691
    153       0.4007        1.3743       0.5138        1.3175  0.0673
    154       0.4368        1.3496       0.4169        1.4139  0.0686
    155       0.4406        1.3593       0.4567        1.3251  0.0679
    156       0.4030        1.3586       0.3465        1.4339  0.0676
    157       0.3594        1.4298       0.4479        1.3491  0.0685
    158       0.3947        1.3666       0.4100        1.3895  0.0677
    159       0.3549        1.4573       0.4082        1.3976  0.0742
    160       0.3869        1.3672       0.4545        1.2977  0.0677
    161       0.4071        1.3711       0.4115        1.3810  0.0681
    162       0.4093        1.3234       0.3983        1.3566  0.0684
    163       0.3878        1.4095       0.4437        1.4152  0.0682
    164       0.3699        1.4731       0.3269        1.5608  0.1417
    165       0.3557        1.4567       0.4365        1.4190  0.0679
    166       0.4331        1.3133       0.5141        1.3028  0.0682
    167       0.4583        1.3309       0.3940        1.4411  0.0680
    168       0.4426        1.3636       0.4133        1.3834  0.0689
    169       0.4035        1.3761       0.4410        1.3348  0.0677
    170       0.3989        1.3963       0.4368        1.3513  0.0682
    171       0.4155        1.3574       0.4235        1.4017  0.0679
    172       0.3800        1.4160       0.4329        1.3330  0.0681
    173       0.4284        1.3154       0.3844        1.4098  0.0674
    174       0.3685        1.3952       0.4223        1.3664  0.0680
    175       0.4013        1.3476       0.4401        1.3270  0.0678
    176       0.3880        1.4197       0.4262        1.4163  0.0667
    177       0.3829        1.4239       0.3365        1.5454  0.0689
    178       0.3444        1.4796       0.4347        1.4069  0.0681
    179       0.4289        1.3429       0.4711        1.2882  0.0742
    180       0.4228        1.3481       0.3961        1.4344  0.0672
    181       0.4070        1.3701       0.4217        1.3646  0.0679
    182       0.4046        1.3305       0.4573        1.2697  0.0676
    183       0.3986        1.3502       0.4118        1.3597  0.0680
    184       0.4236        1.2872       0.3853        1.3680  0.0680
    185       0.3840        1.3895       0.4401        1.3876  0.0672
    186       0.3772        1.4543       0.3703        1.5194  0.0669
    187       0.3777        1.4337       0.4329        1.3286  0.0687
    188       0.4073        1.3253       0.4365        1.3026  0.0681
    189       0.3964        1.3524       0.4136        1.3483  0.0749
    190       0.4274        1.3052       0.3669        1.4848  0.0716
    191       0.3843        1.4045       0.4539        1.3406  0.0673
    192       0.4077        1.3443       0.4464        1.3189  0.0679
    193       0.3796        1.3824       0.4290        1.3320  0.0677
    194       0.4341        1.2930       0.4235        1.3508  0.0686
    195       0.4136        1.3531       0.4389        1.3255  0.0682
    196       0.4250        1.3295       0.3841        1.4258  0.0684
    197       0.3721        1.4316       0.3964        1.3279  0.0678
    198       0.3963        1.3590       0.3371        1.4509  0.0669
    199       0.3720        1.4062       0.3904        1.4310  0.0734
    200       0.4054        1.3172       0.4266        1.3753  0.0682
Out[16]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=VanillaMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): ReLU()
      (2): Linear(in_features=32, out_features=7, bias=True)
      (3): Softmax(dim=-1)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=VanillaMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): ReLU()
      (2): Linear(in_features=32, out_features=7, bias=True)
      (3): Softmax(dim=-1)
    )
  ),
)
In [17]:
y_pred = net.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[17]:
0.3754543665724024
In [18]:
net.criterion, net.optimizer
Out[18]:
(torch.nn.modules.loss.NLLLoss, torch.optim.sgd.SGD)
In [19]:
history = net.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image
In [20]:
from torchviz import make_dot


make_dot(
    net.module_(torch.as_tensor(X[:1], dtype=torch.float32).to(net.device)),
    params=dict(net.module_.named_parameters()),
    show_attrs=False,
    # show_saved=True,
)
Out[20]:
No description has been provided for this image

Adam¶

In [21]:
net = NeuralNetClassifier(
    VanillaMLP,
    module__in_features=X_train.shape[1],
    module__out_features=len(le.classes_),
    max_epochs=500,
    
    
    device=DEVICE,
    optimizer=torch.optim.Adam,
    lr=1e-3,

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
            lower_is_better=False
        ),
    ],

    batch_size=2048,
    iterator_train__shuffle=True,
)
net
Out[21]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [22]:
# Training the network
net.fit(X_train, y_train)
  epoch    train_acc    train_loss    valid_acc    valid_loss     dur
-------  -----------  ------------  -----------  ------------  ------
      1       0.1055        2.8612       0.1620        2.2308  0.1646
      2       0.1413        2.0745       0.2203        1.9541  0.0784
      3       0.2041        1.9657       0.2008        1.9668  0.0792
      4       0.2097        1.9434       0.2219        1.9088  0.0757
      5       0.2224        1.8955       0.2375        1.8621  0.0764
      6       0.2520        1.8455       0.2288        1.8194  0.0739
      7       0.2314        1.8114       0.2673        1.7938  0.0741
      8       0.2886        1.7870       0.2655        1.7698  0.0736
      9       0.3005        1.7623       0.2971        1.7436  0.0748
     10       0.2797        1.7372       0.2878        1.7211  0.0738
     11       0.3106        1.7143       0.3359        1.6989  0.0745
     12       0.3443        1.6926       0.3471        1.6771  0.0742
     13       0.3371        1.6707       0.3441        1.6566  0.0754
     14       0.3685        1.6499       0.3904        1.6358  0.0785
     15       0.3984        1.6291       0.4091        1.6159  0.0857
     16       0.4128        1.6089       0.4187        1.5962  0.0831
     17       0.4253        1.5892       0.4259        1.5769  0.0790
     18       0.4429        1.5699       0.4422        1.5578  0.0785
     19       0.4446        1.5509       0.4554        1.5393  0.0834
     20       0.4712        1.5323       0.4606        1.5211  0.1165
     21       0.4771        1.5139       0.4678        1.5031  0.0779
     22       0.4753        1.4960       0.4852        1.4856  0.0787
     23       0.4962        1.4784       0.4934        1.4682  0.0777
     24       0.4944        1.4614       0.4946        1.4515  0.0765
     25       0.5078        1.4446       0.5099        1.4346  0.0760
     26       0.5106        1.4285       0.5154        1.4181  0.0765
     27       0.5105        1.4116       0.5187        1.4023  0.0741
     28       0.5307        1.3957       0.5301        1.3861  0.0750
     29       0.5341        1.3797       0.5424        1.3707  0.0819
     30       0.5483        1.3639       0.5403        1.3549  0.0858
     31       0.5482        1.3483       0.5506        1.3396  0.0825
     32       0.5614        1.3331       0.5563        1.3247  0.0767
     33       0.5574        1.3191       0.5656        1.3098  0.0784
     34       0.5770        1.3039       0.5629        1.2952  0.1628
     35       0.5711        1.2896       0.5713        1.2804  0.0749
     36       0.5742        1.2749       0.5858        1.2663  0.0739
     37       0.5869        1.2605       0.5692        1.2527  0.0746
     38       0.5795        1.2471       0.5957        1.2385  0.0741
     39       0.5934        1.2331       0.5954        1.2257  0.0750
     40       0.5961        1.2194       0.5999        1.2121  0.0753
     41       0.6001        1.2068       0.6039        1.1989  0.0737
     42       0.6070        1.1940       0.5993        1.1854  0.0754
     43       0.6043        1.1808       0.6147        1.1727  0.0751
     44       0.6107        1.1681       0.6017        1.1588  0.0759
     45       0.6092        1.1556       0.6207        1.1461  0.0731
     46       0.6197        1.1425       0.6282        1.1337  0.0741
     47       0.6303        1.1294       0.6315        1.1206  0.0746
     48       0.6312        1.1166       0.6409        1.1072  0.0746
     49       0.6402        1.1033       0.6463        1.0945  0.0753
     50       0.6466        1.0905       0.6499        1.0815  0.0760
     51       0.6454        1.0785       0.6635        1.0697  0.0745
     52       0.6564        1.0664       0.6556        1.0565  0.0750
     53       0.6561        1.0536       0.6613        1.0443  0.0743
     54       0.6589        1.0423       0.6695        1.0333  0.0754
     55       0.6592        1.0318       0.6710        1.0214  0.1047
     56       0.6643        1.0196       0.6743        1.0102  0.0875
     57       0.6713        1.0088       0.6710        1.0000  0.0818
     58       0.6686        0.9980       0.6758        0.9890  0.0774
     59       0.6733        0.9876       0.6806        0.9788  0.0762
     60       0.6740        0.9778       0.6875        0.9686  0.0765
     61       0.6796        0.9680       0.6869        0.9596  0.0744
     62       0.6793        0.9582       0.6857        0.9494  0.0754
     63       0.6832        0.9499       0.6848        0.9407  0.0747
     64       0.6838        0.9397       0.6984        0.9319  0.0777
     65       0.6874        0.9307       0.6927        0.9228  0.0756
     66       0.6926        0.9224       0.6939        0.9138  0.0748
     67       0.6885        0.9144       0.7020        0.9052  0.0770
     68       0.6960        0.9057       0.7023        0.8990  0.0792
     69       0.6950        0.8980       0.7008        0.8892  0.0894
     70       0.7005        0.8893       0.7038        0.8814  0.0840
     71       0.6999        0.8820       0.7095        0.8739  0.0793
     72       0.7045        0.8740       0.7068        0.8664  0.0840
     73       0.7057        0.8668       0.7092        0.8599  0.1580
     74       0.7078        0.8600       0.7071        0.8534  0.0785
     75       0.7106        0.8537       0.7170        0.8461  0.0791
     76       0.7062        0.8476       0.7164        0.8400  0.0812
     77       0.7132        0.8402       0.7143        0.8338  0.0884
     78       0.7122        0.8334       0.7188        0.8263  0.1156
     79       0.7124        0.8271       0.7185        0.8201  0.0790
     80       0.7150        0.8216       0.7149        0.8144  0.0813
     81       0.7172        0.8157       0.7225        0.8084  0.0805
     82       0.7193        0.8104       0.7240        0.8045  0.0821
     83       0.7189        0.8039       0.7252        0.7969  0.0964
     84       0.7214        0.7980       0.7219        0.7911  0.0839
     85       0.7199        0.7924       0.7297        0.7867  0.0870
     86       0.7222        0.7868       0.7321        0.7808  0.0785
     87       0.7240        0.7813       0.7273        0.7758  0.0801
     88       0.7255        0.7766       0.7333        0.7714  0.0849
     89       0.7267        0.7720       0.7327        0.7680  0.0837
     90       0.7289        0.7678       0.7342        0.7617  0.0811
     91       0.7282        0.7627       0.7357        0.7569  0.0793
     92       0.7308        0.7589       0.7354        0.7552  0.0803
     93       0.7318        0.7543       0.7369        0.7482  0.0853
     94       0.7322        0.7499       0.7360        0.7446  0.0962
     95       0.7335        0.7459       0.7378        0.7407  0.0851
     96       0.7319        0.7416       0.7396        0.7389  0.0812
     97       0.7349        0.7390       0.7384        0.7328  0.0805
     98       0.7339        0.7351       0.7408        0.7298  0.0791
     99       0.7372        0.7299       0.7405        0.7282  0.0788
    100       0.7386        0.7270       0.7408        0.7230  0.0868
    101       0.7354        0.7243       0.7444        0.7198  0.0785
    102       0.7395        0.7203       0.7456        0.7169  0.0767
    103       0.7362        0.7172       0.7486        0.7114  0.0776
    104       0.7397        0.7133       0.7489        0.7085  0.0772
    105       0.7398        0.7093       0.7459        0.7054  0.0764
    106       0.7414        0.7058       0.7502        0.7013  0.0780
    107       0.7409        0.7028       0.7505        0.6969  0.0773
    108       0.7448        0.6985       0.7480        0.6944  0.0840
    109       0.7419        0.6954       0.7502        0.6909  0.0872
    110       0.7451        0.6922       0.7502        0.6884  0.0846
    111       0.7450        0.6892       0.7532        0.6855  0.1565
    112       0.7477        0.6867       0.7511        0.6833  0.0812
    113       0.7456        0.6837       0.7559        0.6813  0.0882
    114       0.7480        0.6810       0.7511        0.6789  0.0883
    115       0.7463        0.6784       0.7568        0.6757  0.0875
    116       0.7507        0.6760       0.7520        0.6741  0.0853
    117       0.7453        0.6745       0.7568        0.6710  0.0793
    118       0.7517        0.6719       0.7514        0.6699  0.0793
    119       0.7472        0.6699       0.7559        0.6665  0.0862
    120       0.7516        0.6667       0.7595        0.6652  0.0899
    121       0.7505        0.6650       0.7556        0.6628  0.1087
    122       0.7516        0.6630       0.7586        0.6620  0.0801
    123       0.7516        0.6617       0.7568        0.6625  0.0795
    124       0.7520        0.6615       0.7586        0.6569  0.0804
    125       0.7517        0.6584       0.7571        0.6562  0.0781
    126       0.7556        0.6560       0.7628        0.6534  0.0786
    127       0.7553        0.6531       0.7619        0.6514  0.0785
    128       0.7554        0.6507       0.7634        0.6496  0.0769
    129       0.7558        0.6489       0.7628        0.6472  0.0776
    130       0.7573        0.6472       0.7643        0.6464  0.0773
    131       0.7603        0.6455       0.7646        0.6442  0.0750
    132       0.7557        0.6439       0.7682        0.6422  0.0858
    133       0.7608        0.6421       0.7670        0.6413  0.0769
    134       0.7560        0.6408       0.7703        0.6397  0.0766
    135       0.7613        0.6388       0.7670        0.6376  0.0752
    136       0.7596        0.6373       0.7691        0.6355  0.0773
    137       0.7613        0.6356       0.7691        0.6345  0.0761
    138       0.7612        0.6346       0.7718        0.6340  0.0765
    139       0.7617        0.6329       0.7721        0.6322  0.0775
    140       0.7614        0.6318       0.7700        0.6308  0.0768
    141       0.7628        0.6302       0.7703        0.6285  0.0774
    142       0.7611        0.6290       0.7730        0.6274  0.0754
    143       0.7624        0.6287       0.7694        0.6315  0.0846
    144       0.7651        0.6269       0.7721        0.6234  0.0767
    145       0.7660        0.6248       0.7685        0.6228  0.0763
    146       0.7652        0.6239       0.7715        0.6211  0.0757
    147       0.7651        0.6221       0.7730        0.6208  0.0776
    148       0.7659        0.6212       0.7730        0.6205  0.0761
    149       0.7649        0.6197       0.7727        0.6173  0.1526
    150       0.7678        0.6172       0.7730        0.6164  0.0768
    151       0.7681        0.6165       0.7748        0.6162  0.0788
    152       0.7681        0.6154       0.7745        0.6140  0.0770
    153       0.7690        0.6138       0.7736        0.6127  0.0764
    154       0.7698        0.6130       0.7709        0.6117  0.0785
    155       0.7658        0.6120       0.7757        0.6111  0.0868
    156       0.7711        0.6103       0.7772        0.6096  0.0833
    157       0.7693        0.6093       0.7715        0.6097  0.0879
    158       0.7696        0.6092       0.7778        0.6085  0.0795
    159       0.7714        0.6070       0.7751        0.6059  0.0776
    160       0.7715        0.6062       0.7763        0.6050  0.0804
    161       0.7715        0.6046       0.7763        0.6036  0.0803
    162       0.7727        0.6039       0.7793        0.6031  0.0768
    163       0.7715        0.6023       0.7766        0.6009  0.0768
    164       0.7726        0.6015       0.7772        0.6008  0.0770
    165       0.7727        0.6004       0.7763        0.5996  0.0827
    166       0.7736        0.5995       0.7784        0.5986  0.0861
    167       0.7748        0.5984       0.7790        0.5974  0.0882
    168       0.7743        0.5968       0.7781        0.5964  0.0850
    169       0.7730        0.5965       0.7797        0.5953  0.0863
    170       0.7751        0.5951       0.7778        0.5955  0.1073
    171       0.7748        0.5938       0.7797        0.5938  0.0821
    172       0.7749        0.5939       0.7775        0.5923  0.0827
    173       0.7733        0.5925       0.7821        0.5919  0.0869
    174       0.7773        0.5913       0.7803        0.5918  0.0883
    175       0.7768        0.5909       0.7815        0.5896  0.0880
    176       0.7751        0.5898       0.7800        0.5892  0.0791
    177       0.7782        0.5891       0.7827        0.5898  0.0867
    178       0.7776        0.5888       0.7803        0.5898  0.0854
    179       0.7779        0.5879       0.7842        0.5867  0.0849
    180       0.7790        0.5867       0.7775        0.5873  0.0834
    181       0.7766        0.5867       0.7827        0.5840  0.0788
    182       0.7797        0.5848       0.7824        0.5836  0.0876
    183       0.7784        0.5840       0.7839        0.5848  0.0811
    184       0.7800        0.5832       0.7854        0.5828  0.0776
    185       0.7776        0.5827       0.7842        0.5812  0.0794
    186       0.7775        0.5825       0.7809        0.5820  0.0788
    187       0.7801        0.5812       0.7824        0.5803  0.1517
    188       0.7811        0.5809       0.7860        0.5810  0.0779
    189       0.7770        0.5801       0.7830        0.5818  0.0757
    190       0.7826        0.5795       0.7854        0.5777  0.0761
    191       0.7803        0.5774       0.7848        0.5767  0.0764
    192       0.7835        0.5769       0.7875        0.5774  0.0764
    193       0.7793        0.5758       0.7875        0.5756  0.0771
    194       0.7833        0.5754       0.7881        0.5748  0.0844
    195       0.7817        0.5743       0.7845        0.5747  0.0763
    196       0.7815        0.5744       0.7896        0.5736  0.0774
    197       0.7805        0.5728       0.7857        0.5731  0.0778
    198       0.7826        0.5720       0.7872        0.5733  0.0777
    199       0.7827        0.5719       0.7878        0.5712  0.0781
    200       0.7834        0.5716       0.7884        0.5716  0.0761
    201       0.7804        0.5708       0.7881        0.5707  0.0830
    202       0.7864        0.5698       0.7896        0.5705  0.0858
    203       0.7831        0.5686       0.7899        0.5691  0.0886
    204       0.7852        0.5678       0.7914        0.5681  0.0825
    205       0.7842        0.5673       0.7896        0.5673  0.0841
    206       0.7849        0.5665       0.7905        0.5666  0.0829
    207       0.7861        0.5656       0.7926        0.5668  0.0804
    208       0.7863        0.5649       0.7905        0.5649  0.0975
    209       0.7852        0.5643       0.7911        0.5660  0.1123
    210       0.7867        0.5635       0.7917        0.5635  0.0828
    211       0.7857        0.5629       0.7932        0.5640  0.0793
    212       0.7873        0.5619       0.7935        0.5627  0.0785
    213       0.7873        0.5611       0.7932        0.5617  0.0802
    214       0.7881        0.5605       0.7920        0.5644  0.0795
    215       0.7877        0.5612       0.7944        0.5610  0.0859
    216       0.7888        0.5602       0.7938        0.5611  0.0863
    217       0.7870        0.5590       0.7947        0.5598  0.0848
    218       0.7897        0.5581       0.7938        0.5593  0.0814
    219       0.7882        0.5585       0.7920        0.5582  0.0860
    220       0.7882        0.5574       0.7941        0.5580  0.0871
    221       0.7884        0.5564       0.7950        0.5577  0.0977
    222       0.7901        0.5554       0.7953        0.5571  0.0799
    223       0.7891        0.5551       0.7986        0.5562  0.1165
    224       0.7885        0.5550       0.7959        0.5569  0.0872
    225       0.7897        0.5553       0.7971        0.5565  0.1579
    226       0.7893        0.5545       0.7938        0.5546  0.0796
    227       0.7915        0.5534       0.7971        0.5537  0.0801
    228       0.7897        0.5520       0.7977        0.5530  0.0802
    229       0.7914        0.5517       0.7959        0.5551  0.0812
    230       0.7917        0.5518       0.7950        0.5535  0.0823
    231       0.7928        0.5503       0.7977        0.5514  0.0874
    232       0.7925        0.5494       0.7962        0.5523  0.0803
    233       0.7919        0.5490       0.7983        0.5504  0.0874
    234       0.7926        0.5482       0.7980        0.5497  0.0776
    235       0.7942        0.5480       0.7983        0.5490  0.0783
    236       0.7931        0.5470       0.7983        0.5484  0.0752
    237       0.7945        0.5468       0.7992        0.5488  0.0767
    238       0.7937        0.5468       0.7995        0.5483  0.0767
    239       0.7931        0.5461       0.7998        0.5470  0.0777
    240       0.7923        0.5459       0.7989        0.5485  0.0802
    241       0.7923        0.5449       0.7992        0.5459  0.0769
    242       0.7955        0.5435       0.7995        0.5468  0.0762
    243       0.7960        0.5435       0.7962        0.5464  0.0780
    244       0.7947        0.5431       0.7995        0.5447  0.0769
    245       0.7967        0.5423       0.8001        0.5445  0.0761
    246       0.7947        0.5421       0.7989        0.5477  0.0777
    247       0.7953        0.5420       0.8022        0.5439  0.0767
    248       0.7946        0.5420       0.7998        0.5423  0.0850
    249       0.7963        0.5407       0.8010        0.5425  0.0780
    250       0.7960        0.5398       0.8025        0.5420  0.0791
    251       0.7970        0.5388       0.8004        0.5418  0.0760
    252       0.7964        0.5385       0.8013        0.5400  0.0783
    253       0.7963        0.5388       0.7983        0.5408  0.0799
    254       0.7963        0.5375       0.8034        0.5395  0.0806
    255       0.7970        0.5380       0.8004        0.5419  0.0875
    256       0.7977        0.5373       0.8034        0.5381  0.1217
    257       0.7985        0.5351       0.8028        0.5375  0.0808
    258       0.7987        0.5345       0.7995        0.5401  0.0790
    259       0.7989        0.5342       0.8049        0.5361  0.0803
    260       0.7990        0.5344       0.8019        0.5370  0.0822
    261       0.7964        0.5343       0.8040        0.5354  0.0944
    262       0.8004        0.5344       0.8028        0.5384  0.0850
    263       0.7964        0.5335       0.8019        0.5358  0.1561
    264       0.7998        0.5324       0.8037        0.5347  0.0799
    265       0.8006        0.5315       0.8031        0.5353  0.0793
    266       0.7986        0.5314       0.8001        0.5376  0.0804
    267       0.7998        0.5312       0.8052        0.5319  0.0792
    268       0.8013        0.5298       0.8040        0.5322  0.0810
    269       0.8013        0.5291       0.8040        0.5330  0.0775
    270       0.8023        0.5284       0.8064        0.5307  0.0788
    271       0.8010        0.5290       0.8022        0.5332  0.0767
    272       0.8007        0.5282       0.8019        0.5327  0.0771
    273       0.8004        0.5286       0.8046        0.5310  0.0840
    274       0.8021        0.5275       0.8076        0.5290  0.0775
    275       0.8007        0.5264       0.8046        0.5315  0.0780
    276       0.8022        0.5262       0.8052        0.5285  0.0778
    277       0.8020        0.5256       0.8052        0.5288  0.0774
    278       0.8028        0.5257       0.8052        0.5283  0.0763
    279       0.8015        0.5248       0.8049        0.5292  0.0779
    280       0.8029        0.5248       0.8049        0.5269  0.0782
    281       0.8025        0.5239       0.8070        0.5258  0.0778
    282       0.8041        0.5231       0.8055        0.5273  0.0765
    283       0.8028        0.5234       0.8049        0.5269  0.0767
    284       0.8034        0.5226       0.8092        0.5251  0.0776
    285       0.8056        0.5217       0.8046        0.5244  0.0779
    286       0.8053        0.5207       0.8061        0.5274  0.0781
    287       0.8025        0.5214       0.8098        0.5229  0.0762
    288       0.8034        0.5210       0.8049        0.5252  0.0768
    289       0.8040        0.5204       0.8073        0.5223  0.0856
    290       0.8056        0.5188       0.8085        0.5223  0.0765
    291       0.8035        0.5196       0.8076        0.5222  0.0773
    292       0.8053        0.5182       0.8098        0.5212  0.0765
    293       0.8068        0.5178       0.8070        0.5219  0.0764
    294       0.8056        0.5174       0.8092        0.5209  0.0770
    295       0.8037        0.5173       0.8095        0.5207  0.0789
    296       0.8084        0.5170       0.8101        0.5196  0.0843
    297       0.8044        0.5164       0.8104        0.5185  0.1305
    298       0.8076        0.5160       0.8067        0.5235  0.0965
    299       0.8054        0.5167       0.8079        0.5193  0.0857
    300       0.8070        0.5159       0.8095        0.5185  0.0854
    301       0.8062        0.5149       0.8125        0.5168  0.2071
    302       0.8095        0.5144       0.8089        0.5198  0.0842
    303       0.8057        0.5143       0.8113        0.5174  0.0927
    304       0.8086        0.5141       0.8122        0.5166  0.0870
    305       0.8073        0.5136       0.8131        0.5154  0.0845
    306       0.8081        0.5133       0.8092        0.5170  0.0843
    307       0.8068        0.5119       0.8122        0.5151  0.0910
    308       0.8077        0.5121       0.8122        0.5147  0.1250
    309       0.8080        0.5114       0.8119        0.5154  0.0917
    310       0.8083        0.5114       0.8113        0.5137  0.0897
    311       0.8081        0.5108       0.8128        0.5137  0.0909
    312       0.8092        0.5101       0.8098        0.5146  0.0796
    313       0.8074        0.5102       0.8119        0.5145  0.0797
    314       0.8083        0.5109       0.8116        0.5132  0.0826
    315       0.8087        0.5087       0.8155        0.5119  0.0777
    316       0.8092        0.5091       0.8104        0.5127  0.0774
    317       0.8077        0.5081       0.8125        0.5125  0.0779
    318       0.8104        0.5086       0.8110        0.5124  0.0770
    319       0.8098        0.5079       0.8116        0.5108  0.0893
    320       0.8101        0.5074       0.8125        0.5118  0.1006
    321       0.8114        0.5065       0.8128        0.5102  0.0900
    322       0.8110        0.5060       0.8140        0.5099  0.0808
    323       0.8107        0.5062       0.8116        0.5093  0.0791
    324       0.8096        0.5056       0.8152        0.5089  0.0798
    325       0.8120        0.5048       0.8125        0.5093  0.0790
    326       0.8107        0.5045       0.8125        0.5098  0.0784
    327       0.8109        0.5041       0.8164        0.5080  0.0788
    328       0.8117        0.5044       0.8131        0.5090  0.0780
    329       0.8105        0.5045       0.8164        0.5071  0.0762
    330       0.8111        0.5037       0.8128        0.5102  0.0826
    331       0.8104        0.5030       0.8146        0.5076  0.0983
    332       0.8129        0.5028       0.8134        0.5077  0.1175
    333       0.8107        0.5021       0.8137        0.5064  0.0886
    334       0.8117        0.5021       0.8155        0.5067  0.0923
    335       0.8126        0.5013       0.8143        0.5055  0.1181
    336       0.8131        0.5008       0.8146        0.5062  0.0842
    337       0.8132        0.5007       0.8161        0.5049  0.0896
    338       0.8118        0.5005       0.8167        0.5045  0.0909
    339       0.8126        0.5004       0.8143        0.5058  0.1715
    340       0.8121        0.4998       0.8143        0.5043  0.0863
    341       0.8135        0.4997       0.8137        0.5066  0.0919
    342       0.8090        0.5009       0.8179        0.5029  0.0861
    343       0.8142        0.4989       0.8140        0.5057  0.0927
    344       0.8124        0.4994       0.8155        0.5045  0.0881
    345       0.8138        0.4987       0.8137        0.5043  0.0945
    346       0.8144        0.4979       0.8179        0.5028  0.0848
    347       0.8128        0.4978       0.8185        0.5019  0.0872
    348       0.8143        0.4967       0.8137        0.5034  0.0889
    349       0.8146        0.4969       0.8185        0.5006  0.0866
    350       0.8133        0.4961       0.8146        0.5022  0.0859
    351       0.8147        0.4955       0.8185        0.4998  0.0842
    352       0.8144        0.4954       0.8161        0.5030  0.0842
    353       0.8169        0.4956       0.8167        0.5004  0.0799
    354       0.8141        0.4954       0.8191        0.4989  0.0872
    355       0.8145        0.4954       0.8149        0.5001  0.0875
    356       0.8132        0.4944       0.8173        0.5003  0.0874
    357       0.8161        0.4938       0.8152        0.4995  0.0803
    358       0.8142        0.4944       0.8233        0.4978  0.0799
    359       0.8153        0.4937       0.8146        0.4986  0.0771
    360       0.8154        0.4926       0.8152        0.4990  0.0771
    361       0.8149        0.4921       0.8194        0.4977  0.0786
    362       0.8166        0.4925       0.8200        0.4969  0.0769
    363       0.8153        0.4919       0.8185        0.4974  0.0756
    364       0.8164        0.4918       0.8179        0.4977  0.0775
    365       0.8148        0.4921       0.8197        0.4972  0.0811
    366       0.8144        0.4914       0.8197        0.4951  0.0838
    367       0.8139        0.4909       0.8167        0.4982  0.0826
    368       0.8174        0.4912       0.8188        0.4966  0.0808
    369       0.8163        0.4898       0.8179        0.4966  0.0893
    370       0.8177        0.4902       0.8203        0.4962  0.0916
    371       0.8168        0.4893       0.8215        0.4944  0.0830
    372       0.8162        0.4894       0.8206        0.4962  0.0807
    373       0.8166        0.4885       0.8170        0.4958  0.0855
    374       0.8160        0.4893       0.8188        0.4954  0.0898
    375       0.8163        0.4895       0.8245        0.4927  0.0860
    376       0.8180        0.4886       0.8239        0.4933  0.0814
    377       0.8182        0.4872       0.8227        0.4920  0.1624
    378       0.8171        0.4872       0.8233        0.4915  0.0797
    379       0.8196        0.4879       0.8203        0.4928  0.0790
    380       0.8174        0.4869       0.8200        0.4920  0.0794
    381       0.8204        0.4859       0.8209        0.4916  0.0784
    382       0.8173        0.4850       0.8227        0.4916  0.0767
    383       0.8195        0.4847       0.8230        0.4904  0.0768
    384       0.8191        0.4852       0.8236        0.4910  0.0775
    385       0.8202        0.4850       0.8182        0.4939  0.0787
    386       0.8165        0.4856       0.8206        0.4920  0.0794
    387       0.8199        0.4845       0.8218        0.4916  0.0848
    388       0.8188        0.4856       0.8272        0.4897  0.0873
    389       0.8198        0.4851       0.8191        0.4932  0.0854
    390       0.8201        0.4840       0.8215        0.4910  0.1166
    391       0.8199        0.4829       0.8221        0.4890  0.0789
    392       0.8209        0.4828       0.8230        0.4887  0.0797
    393       0.8203        0.4828       0.8242        0.4885  0.0790
    394       0.8223        0.4823       0.8227        0.4880  0.0799
    395       0.8206        0.4821       0.8203        0.4928  0.0791
    396       0.8207        0.4831       0.8263        0.4870  0.0800
    397       0.8205        0.4834       0.8263        0.4872  0.0812
    398       0.8190        0.4816       0.8230        0.4879  0.0768
    399       0.8229        0.4809       0.8203        0.4925  0.0767
    400       0.8196        0.4825       0.8263        0.4857  0.0825
    401       0.8220        0.4800       0.8269        0.4870  0.0788
    402       0.8201        0.4814       0.8272        0.4845  0.0772
    403       0.8208        0.4806       0.8230        0.4877  0.0825
    404       0.8216        0.4794       0.8245        0.4865  0.0759
    405       0.8217        0.4787       0.8248        0.4843  0.0767
    406       0.8233        0.4786       0.8269        0.4865  0.0782
    407       0.8230        0.4784       0.8272        0.4838  0.0767
    408       0.8217        0.4787       0.8236        0.4843  0.0774
    409       0.8213        0.4777       0.8242        0.4843  0.0764
    410       0.8220        0.4770       0.8275        0.4829  0.0767
    411       0.8217        0.4768       0.8248        0.4846  0.0787
    412       0.8231        0.4765       0.8278        0.4836  0.0784
    413       0.8237        0.4764       0.8260        0.4836  0.0765
    414       0.8229        0.4761       0.8242        0.4856  0.0774
    415       0.8230        0.4772       0.8269        0.4822  0.0773
    416       0.8233        0.4757       0.8287        0.4819  0.1596
    417       0.8215        0.4757       0.8296        0.4806  0.0772
    418       0.8245        0.4756       0.8251        0.4826  0.0768
    419       0.8222        0.4755       0.8272        0.4825  0.0771
    420       0.8229        0.4753       0.8257        0.4833  0.0766
    421       0.8237        0.4754       0.8308        0.4810  0.0769
    422       0.8230        0.4752       0.8284        0.4802  0.0777
    423       0.8247        0.4736       0.8266        0.4818  0.0773
    424       0.8238        0.4730       0.8272        0.4805  0.0760
    425       0.8247        0.4739       0.8269        0.4825  0.0768
    426       0.8232        0.4727       0.8305        0.4784  0.0769
    427       0.8232        0.4734       0.8320        0.4782  0.0777
    428       0.8243        0.4726       0.8257        0.4812  0.0755
    429       0.8247        0.4725       0.8317        0.4779  0.0777
    430       0.8227        0.4719       0.8257        0.4816  0.0848
    431       0.8242        0.4715       0.8290        0.4781  0.0781
    432       0.8246        0.4712       0.8317        0.4769  0.0785
    433       0.8241        0.4705       0.8293        0.4781  0.0762
    434       0.8241        0.4701       0.8338        0.4764  0.0775
    435       0.8254        0.4703       0.8287        0.4782  0.0771
    436       0.8248        0.4700       0.8305        0.4776  0.0770
    437       0.8255        0.4700       0.8290        0.4767  0.1025
    438       0.8244        0.4690       0.8293        0.4769  0.0786
    439       0.8266        0.4693       0.8287        0.4761  0.0783
    440       0.8269        0.4696       0.8266        0.4791  0.0777
    441       0.8245        0.4689       0.8338        0.4754  0.0767
    442       0.8275        0.4688       0.8287        0.4773  0.0778
    443       0.8221        0.4686       0.8299        0.4769  0.0798
    444       0.8289        0.4693       0.8230        0.4789  0.0859
    445       0.8226        0.4693       0.8317        0.4751  0.0773
    446       0.8269        0.4680       0.8284        0.4755  0.0778
    447       0.8254        0.4670       0.8290        0.4749  0.0772
    448       0.8259        0.4674       0.8254        0.4752  0.0794
    449       0.8259        0.4668       0.8299        0.4744  0.0772
    450       0.8267        0.4668       0.8332        0.4727  0.0767
    451       0.8274        0.4660       0.8305        0.4735  0.0779
    452       0.8254        0.4660       0.8326        0.4734  0.0764
    453       0.8268        0.4655       0.8287        0.4762  0.0766
    454       0.8260        0.4661       0.8344        0.4727  0.1515
    455       0.8278        0.4656       0.8299        0.4739  0.0768
    456       0.8254        0.4649       0.8305        0.4727  0.0779
    457       0.8289        0.4655       0.8290        0.4730  0.0846
    458       0.8260        0.4655       0.8329        0.4722  0.0774
    459       0.8282        0.4638       0.8317        0.4720  0.0760
    460       0.8244        0.4649       0.8335        0.4726  0.0773
    461       0.8289        0.4646       0.8293        0.4716  0.0758
    462       0.8275        0.4629       0.8284        0.4733  0.0766
    463       0.8302        0.4637       0.8251        0.4731  0.0781
    464       0.8272        0.4632       0.8374        0.4700  0.0762
    465       0.8303        0.4623       0.8290        0.4717  0.0804
    466       0.8264        0.4624       0.8353        0.4694  0.0762
    467       0.8302        0.4620       0.8314        0.4702  0.0764
    468       0.8281        0.4615       0.8323        0.4703  0.0771
    469       0.8275        0.4617       0.8344        0.4687  0.0778
    470       0.8292        0.4613       0.8314        0.4699  0.0772
    471       0.8295        0.4610       0.8347        0.4691  0.0829
    472       0.8297        0.4608       0.8320        0.4694  0.0776
    473       0.8293        0.4606       0.8326        0.4695  0.0754
    474       0.8318        0.4606       0.8317        0.4700  0.0777
    475       0.8299        0.4602       0.8353        0.4684  0.0778
    476       0.8290        0.4608       0.8356        0.4676  0.0782
    477       0.8284        0.4607       0.8317        0.4717  0.0784
    478       0.8283        0.4598       0.8359        0.4672  0.0763
    479       0.8311        0.4595       0.8317        0.4678  0.0764
    480       0.8302        0.4590       0.8362        0.4680  0.0766
    481       0.8307        0.4596       0.8308        0.4689  0.0766
    482       0.8308        0.4588       0.8323        0.4691  0.0769
    483       0.8290        0.4587       0.8344        0.4656  0.1038
    484       0.8330        0.4584       0.8329        0.4672  0.0765
    485       0.8313        0.4582       0.8365        0.4652  0.0783
    486       0.8324        0.4577       0.8302        0.4693  0.0780
    487       0.8292        0.4572       0.8335        0.4677  0.0767
    488       0.8321        0.4582       0.8317        0.4700  0.0833
    489       0.8293        0.4579       0.8368        0.4643  0.0768
    490       0.8314        0.4573       0.8314        0.4667  0.0755
    491       0.8327        0.4565       0.8341        0.4655  0.0777
    492       0.8326        0.4561       0.8353        0.4662  0.0767
    493       0.8305        0.4569       0.8329        0.4663  0.1526
    494       0.8319        0.4571       0.8326        0.4667  0.0780
    495       0.8305        0.4555       0.8341        0.4640  0.0773
    496       0.8318        0.4547       0.8362        0.4635  0.0766
    497       0.8332        0.4548       0.8329        0.4644  0.0758
    498       0.8326        0.4548       0.8368        0.4633  0.0767
    499       0.8308        0.4551       0.8347        0.4643  0.0777
    500       0.8331        0.4543       0.8356        0.4641  0.0775
Out[22]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=VanillaMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): ReLU()
      (2): Linear(in_features=32, out_features=7, bias=True)
      (3): Softmax(dim=-1)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=VanillaMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): ReLU()
      (2): Linear(in_features=32, out_features=7, bias=True)
      (3): Softmax(dim=-1)
    )
  ),
)
In [23]:
y_pred = net.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[23]:
0.8323364703729181
In [24]:
history = net.history

plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image

Pipeline with normalization¶

In [25]:
net = NeuralNetClassifier(
    VanillaMLP,
    module__in_features=X_train.shape[1],
    module__out_features=len(le.classes_),
    max_epochs=500,
    
    
    device=DEVICE,
    optimizer=torch.optim.Adam,
    lr=1e-2,

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
            lower_is_better=False,
        ),
    ],

    batch_size=2048,
    iterator_train__shuffle=True,
)
net
Out[25]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [26]:
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

pipe = make_pipeline(StandardScaler(), net)
pipe
Out[26]:
Pipeline(steps=[('standardscaler', StandardScaler()),
                ('neuralnetclassifier',
                 NeuralNetClassifier(_params_to_validate={'iterator_train__shuffle', 'module__out_features', 'module__in_features'}, batch_size=2048, callbacks=[<skorch.callbacks.scoring.EpochScoring object at 0x31b2738c0>], compile=False, dataset=<class 'skorch.dataset.Dataset'>, device='mps', iterator...ataLoader'>, iterator_train__shuffle=True, iterator_valid=<class 'torch.utils.data.dataloader.DataLoader'>, lr=0.01, max_epochs=500, module=<class '__main__.VanillaMLP'>, module__in_features=22, module__out_features=7, optimizer=<class 'torch.optim.adam.Adam'>, predict_nonlinearity='auto', torch_load_kwargs=None, use_caching='auto', verbose=1, warm_start=False))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Pipeline(steps=[('standardscaler', StandardScaler()),
                ('neuralnetclassifier',
                 NeuralNetClassifier(_params_to_validate={'iterator_train__shuffle', 'module__out_features', 'module__in_features'}, batch_size=2048, callbacks=[<skorch.callbacks.scoring.EpochScoring object at 0x31b2738c0>], compile=False, dataset=<class 'skorch.dataset.Dataset'>, device='mps', iterator...ataLoader'>, iterator_train__shuffle=True, iterator_valid=<class 'torch.utils.data.dataloader.DataLoader'>, lr=0.01, max_epochs=500, module=<class '__main__.VanillaMLP'>, module__in_features=22, module__out_features=7, optimizer=<class 'torch.optim.adam.Adam'>, predict_nonlinearity='auto', torch_load_kwargs=None, use_caching='auto', verbose=1, warm_start=False))])
StandardScaler()
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.VanillaMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [27]:
# Training the network
pipe.fit(X_train, y_train)
  epoch    train_acc    train_loss    valid_acc    valid_loss     dur
-------  -----------  ------------  -----------  ------------  ------
      1       0.3780        1.7503       0.5406        1.4481  0.0785
      2       0.5583        1.2945       0.5939        1.0888  0.0761
      3       0.6043        1.0283       0.6276        0.9224  0.0770
      4       0.6506        0.8943       0.6749        0.8287  0.0760
      5       0.6817        0.8096       0.7014        0.7643  0.0801
      6       0.7104        0.7471       0.7279        0.7110  0.0961
      7       0.7334        0.6930       0.7486        0.6616  0.0852
      8       0.7594        0.6437       0.7673        0.6199  0.0786
      9       0.7736        0.6025       0.7857        0.5844  0.0784
     10       0.7901        0.5679       0.7977        0.5549  0.0763
     11       0.8007        0.5389       0.8079        0.5319  0.0736
     12       0.8143        0.5128       0.8164        0.5104  0.0741
     13       0.8196        0.4914       0.8221        0.4930  0.0741
     14       0.8270        0.4737       0.8296        0.4765  0.0746
     15       0.8351        0.4576       0.8308        0.4676  0.0739
     16       0.8393        0.4443       0.8380        0.4551  0.0761
     17       0.8465        0.4314       0.8402        0.4471  0.0829
     18       0.8503        0.4223       0.8438        0.4377  0.0830
     19       0.8557        0.4116       0.8462        0.4296  0.1117
     20       0.8587        0.4043       0.8474        0.4252  0.0792
     21       0.8598        0.3966       0.8501        0.4191  0.0783
     22       0.8630        0.3908       0.8525        0.4134  0.0788
     23       0.8651        0.3857       0.8501        0.4108  0.0809
     24       0.8662        0.3812       0.8549        0.4078  0.0856
     25       0.8681        0.3768       0.8546        0.4047  0.0882
     26       0.8701        0.3734       0.8558        0.4042  0.0779
     27       0.8701        0.3696       0.8576        0.3999  0.0789
     28       0.8739        0.3657       0.8576        0.3988  0.1562
     29       0.8737        0.3632       0.8615        0.3973  0.0772
     30       0.8748        0.3608       0.8597        0.3957  0.0763
     31       0.8741        0.3595       0.8576        0.3939  0.0831
     32       0.8776        0.3550       0.8639        0.3925  0.0856
     33       0.8781        0.3530       0.8621        0.3908  0.0846
     34       0.8783        0.3514       0.8627        0.3940  0.0796
     35       0.8799        0.3499       0.8633        0.3905  0.0769
     36       0.8800        0.3483       0.8642        0.3907  0.0759
     37       0.8817        0.3473       0.8648        0.3881  0.0756
     38       0.8811        0.3470       0.8651        0.3890  0.0767
     39       0.8808        0.3468       0.8645        0.3916  0.0743
     40       0.8838        0.3426       0.8663        0.3885  0.0750
     41       0.8820        0.3434       0.8630        0.3914  0.0758
     42       0.8827        0.3422       0.8648        0.3875  0.0744
     43       0.8832        0.3411       0.8669        0.3860  0.0758
     44       0.8844        0.3403       0.8651        0.3878  0.0792
     45       0.8842        0.3390       0.8660        0.3881  0.0846
     46       0.8850        0.3377       0.8679        0.3844  0.0780
     47       0.8858        0.3373       0.8663        0.3865  0.0781
     48       0.8844        0.3366       0.8679        0.3849  0.0753
     49       0.8852        0.3366       0.8642        0.3911  0.0750
     50       0.8854        0.3375       0.8672        0.3848  0.0749
     51       0.8861        0.3342       0.8672        0.3841  0.1094
     52       0.8868        0.3350       0.8666        0.3884  0.0775
     53       0.8869        0.3333       0.8663        0.3839  0.0752
     54       0.8867        0.3325       0.8648        0.3847  0.0758
     55       0.8860        0.3327       0.8703        0.3883  0.0772
     56       0.8854        0.3326       0.8657        0.3886  0.0773
     57       0.8884        0.3313       0.8633        0.3883  0.0823
     58       0.8882        0.3300       0.8697        0.3845  0.0837
     59       0.8872        0.3294       0.8700        0.3848  0.0847
     60       0.8863        0.3307       0.8654        0.3875  0.0796
     61       0.8881        0.3292       0.8685        0.3855  0.0789
     62       0.8866        0.3294       0.8672        0.3846  0.0787
     63       0.8886        0.3282       0.8682        0.3853  0.0759
     64       0.8871        0.3266       0.8636        0.3875  0.0755
     65       0.8885        0.3269       0.8675        0.3813  0.0739
     66       0.8887        0.3260       0.8660        0.3860  0.1527
     67       0.8884        0.3250       0.8679        0.3829  0.0761
     68       0.8897        0.3255       0.8648        0.3907  0.0737
     69       0.8869        0.3266       0.8663        0.3854  0.0766
     70       0.8887        0.3253       0.8660        0.3840  0.0747
     71       0.8878        0.3250       0.8651        0.3922  0.0756
     72       0.8897        0.3248       0.8660        0.3853  0.0740
     73       0.8875        0.3247       0.8694        0.3860  0.0750
     74       0.8889        0.3232       0.8663        0.3841  0.0743
     75       0.8886        0.3232       0.8666        0.3861  0.0757
     76       0.8870        0.3233       0.8651        0.3875  0.0759
     77       0.8902        0.3232       0.8700        0.3842  0.0758
     78       0.8898        0.3216       0.8663        0.3867  0.0787
     79       0.8891        0.3226       0.8657        0.3881  0.0839
     80       0.8896        0.3229       0.8691        0.3854  0.0936
     81       0.8882        0.3233       0.8621        0.3928  0.0891
     82       0.8879        0.3230       0.8675        0.3830  0.0807
     83       0.8889        0.3218       0.8657        0.3931  0.1043
     84       0.8910        0.3196       0.8694        0.3819  0.0803
     85       0.8920        0.3185       0.8663        0.3860  0.0780
     86       0.8904        0.3196       0.8688        0.3831  0.0769
     87       0.8899        0.3191       0.8669        0.3847  0.0860
     88       0.8906        0.3184       0.8685        0.3860  0.0773
     89       0.8905        0.3180       0.8685        0.3840  0.0773
     90       0.8919        0.3193       0.8685        0.3847  0.0771
     91       0.8898        0.3204       0.8685        0.3851  0.0758
     92       0.8902        0.3207       0.8682        0.3893  0.0769
     93       0.8894        0.3193       0.8682        0.3865  0.0770
     94       0.8919        0.3186       0.8691        0.3840  0.0770
     95       0.8913        0.3170       0.8657        0.3856  0.0810
     96       0.8908        0.3168       0.8727        0.3843  0.0848
     97       0.8905        0.3177       0.8727        0.3843  0.0867
     98       0.8912        0.3189       0.8685        0.3886  0.0816
     99       0.8902        0.3183       0.8688        0.3822  0.0795
    100       0.8916        0.3167       0.8685        0.3884  0.0790
    101       0.8917        0.3172       0.8706        0.3815  0.0773
    102       0.8932        0.3157       0.8675        0.3872  0.0775
    103       0.8905        0.3157       0.8688        0.3865  0.0916
    104       0.8918        0.3148       0.8688        0.3835  0.1666
    105       0.8905        0.3160       0.8709        0.3827  0.0801
    106       0.8908        0.3171       0.8694        0.3858  0.0801
    107       0.8899        0.3156       0.8657        0.3930  0.0787
    108       0.8923        0.3163       0.8700        0.3850  0.0804
    109       0.8915        0.3158       0.8694        0.3850  0.0797
    110       0.8916        0.3167       0.8706        0.3826  0.0802
    111       0.8906        0.3148       0.8706        0.3864  0.0775
    112       0.8927        0.3143       0.8694        0.3830  0.0775
    113       0.8907        0.3137       0.8691        0.3826  0.1010
    114       0.8920        0.3138       0.8691        0.3888  0.0782
    115       0.8928        0.3131       0.8712        0.3833  0.0769
    116       0.8921        0.3133       0.8679        0.3831  0.0766
    117       0.8929        0.3128       0.8712        0.3839  0.0774
    118       0.8927        0.3132       0.8679        0.3876  0.0775
    119       0.8936        0.3128       0.8700        0.3840  0.0774
    120       0.8924        0.3124       0.8712        0.3896  0.0849
    121       0.8928        0.3144       0.8712        0.3846  0.0768
    122       0.8939        0.3118       0.8709        0.3857  0.0792
    123       0.8908        0.3136       0.8700        0.3881  0.0816
    124       0.8920        0.3137       0.8700        0.3850  0.0845
    125       0.8926        0.3135       0.8715        0.3882  0.0823
    126       0.8933        0.3113       0.8703        0.3860  0.0818
    127       0.8936        0.3112       0.8694        0.3845  0.0821
    128       0.8945        0.3112       0.8691        0.3878  0.0803
    129       0.8933        0.3107       0.8709        0.3870  0.0794
    130       0.8943        0.3105       0.8733        0.3845  0.0905
    131       0.8922        0.3127       0.8666        0.3933  0.0865
    132       0.8936        0.3111       0.8703        0.3873  0.0858
    133       0.8927        0.3113       0.8715        0.3892  0.0861
    134       0.8933        0.3114       0.8712        0.3864  0.0879
    135       0.8924        0.3110       0.8730        0.3849  0.0865
    136       0.8958        0.3089       0.8703        0.3856  0.0816
    137       0.8948        0.3093       0.8694        0.3867  0.0844
    138       0.8948        0.3089       0.8727        0.3892  0.0817
    139       0.8948        0.3089       0.8691        0.3870  0.0819
    140       0.8948        0.3080       0.8724        0.3876  0.0818
    141       0.8945        0.3086       0.8682        0.3833  0.0798
    142       0.8947        0.3077       0.8700        0.3899  0.1540
    143       0.8931        0.3089       0.8682        0.3873  0.0772
    144       0.8945        0.3091       0.8679        0.3889  0.0764
    145       0.8932        0.3085       0.8715        0.3865  0.0768
    146       0.8942        0.3080       0.8697        0.3871  0.0766
    147       0.8936        0.3078       0.8682        0.3883  0.0766
    148       0.8950        0.3081       0.8682        0.3880  0.0777
    149       0.8953        0.3077       0.8700        0.3899  0.0785
    150       0.8930        0.3089       0.8718        0.3868  0.0862
    151       0.8952        0.3063       0.8682        0.3904  0.0769
    152       0.8963        0.3069       0.8700        0.3851  0.0775
    153       0.8960        0.3066       0.8694        0.3888  0.0763
    154       0.8945        0.3067       0.8694        0.3905  0.0771
    155       0.8940        0.3071       0.8703        0.3934  0.0768
    156       0.8926        0.3091       0.8691        0.3902  0.0778
    157       0.8939        0.3081       0.8663        0.3912  0.0766
    158       0.8944        0.3073       0.8688        0.3879  0.0814
    159       0.8951        0.3063       0.8709        0.3889  0.0851
    160       0.8946        0.3059       0.8718        0.3908  0.1195
    161       0.8957        0.3063       0.8672        0.3882  0.0818
    162       0.8950        0.3056       0.8682        0.3926  0.0835
    163       0.8954        0.3051       0.8694        0.3886  0.0869
    164       0.8950        0.3055       0.8697        0.3921  0.0782
    165       0.8966        0.3053       0.8697        0.3898  0.0780
    166       0.8966        0.3061       0.8682        0.3937  0.0797
    167       0.8959        0.3077       0.8691        0.3888  0.0789
    168       0.8954        0.3054       0.8679        0.3955  0.0800
    169       0.8965        0.3041       0.8639        0.3942  0.0833
    170       0.8958        0.3060       0.8703        0.3904  0.0887
    171       0.8942        0.3057       0.8645        0.4002  0.0878
    172       0.8942        0.3065       0.8694        0.3937  0.0820
    173       0.8952        0.3062       0.8700        0.3920  0.0798
    174       0.8943        0.3065       0.8648        0.3997  0.0803
    175       0.8954        0.3052       0.8706        0.3913  0.0796
    176       0.8951        0.3045       0.8624        0.3981  0.0815
    177       0.8954        0.3051       0.8682        0.3892  0.0863
    178       0.8959        0.3033       0.8685        0.3968  0.0774
    179       0.8966        0.3035       0.8675        0.3942  0.0763
    180       0.8963        0.3032       0.8691        0.3921  0.1556
    181       0.8972        0.3038       0.8709        0.3913  0.0859
    182       0.8948        0.3042       0.8685        0.3958  0.0774
    183       0.8976        0.3040       0.8663        0.3931  0.0781
    184       0.8974        0.3038       0.8654        0.3940  0.0769
    185       0.8980        0.3039       0.8688        0.3921  0.0759
    186       0.8956        0.3051       0.8682        0.3940  0.0770
    187       0.8959        0.3032       0.8675        0.3988  0.0774
    188       0.8975        0.3029       0.8679        0.3923  0.0775
    189       0.8954        0.3038       0.8715        0.3919  0.0774
    190       0.8937        0.3055       0.8651        0.4035  0.0759
    191       0.8951        0.3056       0.8691        0.3938  0.0860
    192       0.8966        0.3030       0.8669        0.3909  0.0782
    193       0.8983        0.3017       0.8675        0.3913  0.0764
    194       0.8966        0.3024       0.8724        0.3945  0.0948
    195       0.8954        0.3027       0.8706        0.3931  0.0764
    196       0.8961        0.3017       0.8672        0.3944  0.0754
    197       0.8962        0.3023       0.8672        0.3932  0.0786
    198       0.8959        0.3038       0.8724        0.3941  0.0773
    199       0.8968        0.3016       0.8666        0.3947  0.0773
    200       0.8969        0.3012       0.8669        0.3917  0.0772
    201       0.8961        0.3005       0.8682        0.3911  0.0779
    202       0.8969        0.3013       0.8706        0.3913  0.0765
    203       0.8985        0.2999       0.8679        0.3936  0.0772
    204       0.8966        0.3005       0.8688        0.3966  0.0779
    205       0.8974        0.3005       0.8682        0.3981  0.0782
    206       0.8979        0.3013       0.8709        0.3931  0.0770
    207       0.8973        0.3008       0.8648        0.3962  0.0852
    208       0.8969        0.3017       0.8679        0.3953  0.0773
    209       0.8984        0.3004       0.8663        0.3913  0.0779
    210       0.8991        0.2992       0.8706        0.3922  0.0772
    211       0.8970        0.3001       0.8666        0.3947  0.0766
    212       0.8970        0.3002       0.8666        0.3949  0.0751
    213       0.8991        0.2993       0.8682        0.3906  0.0776
    214       0.8973        0.2995       0.8709        0.3946  0.0776
    215       0.8968        0.2999       0.8682        0.3930  0.0777
    216       0.8985        0.2991       0.8651        0.3928  0.0766
    217       0.8981        0.2995       0.8663        0.3942  0.0773
    218       0.8971        0.2996       0.8709        0.3948  0.1538
    219       0.8978        0.2984       0.8694        0.3911  0.0773
    220       0.9000        0.2993       0.8657        0.3969  0.0775
    221       0.8958        0.3010       0.8712        0.3920  0.0761
    222       0.8967        0.3008       0.8627        0.3959  0.0786
    223       0.8979        0.3001       0.8675        0.3944  0.0780
    224       0.8972        0.3005       0.8654        0.3944  0.0852
    225       0.8975        0.3013       0.8657        0.3976  0.0773
    226       0.8979        0.2988       0.8718        0.3920  0.0768
    227       0.8983        0.2998       0.8682        0.3924  0.0772
    228       0.8971        0.2989       0.8657        0.3992  0.0769
    229       0.8957        0.3006       0.8700        0.3926  0.0773
    230       0.8963        0.2996       0.8633        0.3956  0.0776
    231       0.8972        0.3001       0.8682        0.3940  0.0775
    232       0.8988        0.2980       0.8645        0.3953  0.0768
    233       0.8991        0.2971       0.8685        0.3930  0.0778
    234       0.8994        0.2980       0.8645        0.3991  0.0772
    235       0.8997        0.2971       0.8706        0.3932  0.0782
    236       0.8987        0.2982       0.8654        0.3980  0.0768
    237       0.8985        0.2983       0.8691        0.3948  0.0768
    238       0.8997        0.2981       0.8657        0.3976  0.0758
    239       0.8988        0.2988       0.8663        0.3918  0.0780
    240       0.8992        0.2970       0.8663        0.3946  0.0774
    241       0.8998        0.2970       0.8663        0.3930  0.0855
    242       0.8973        0.2977       0.8679        0.3948  0.0761
    243       0.8996        0.2962       0.8688        0.3967  0.0767
    244       0.8996        0.2974       0.8688        0.3946  0.0781
    245       0.8986        0.2975       0.8663        0.3942  0.0792
    246       0.8981        0.2977       0.8675        0.3934  0.0770
    247       0.8988        0.2976       0.8688        0.3952  0.0770
    248       0.9006        0.2965       0.8639        0.4001  0.0764
    249       0.8983        0.2973       0.8657        0.3963  0.1016
    250       0.8983        0.2963       0.8666        0.3914  0.0783
    251       0.9000        0.2977       0.8633        0.3990  0.0768
    252       0.8993        0.2973       0.8657        0.3960  0.0775
    253       0.8991        0.2957       0.8697        0.3962  0.0773
    254       0.8983        0.2982       0.8672        0.3955  0.0758
    255       0.8984        0.2959       0.8660        0.3955  0.0767
    256       0.9005        0.2962       0.8651        0.4018  0.0772
    257       0.9009        0.2952       0.8660        0.3930  0.1543
    258       0.8998        0.2957       0.8663        0.3953  0.0828
    259       0.9008        0.2953       0.8675        0.3936  0.0763
    260       0.9002        0.2965       0.8657        0.3997  0.0772
    261       0.8992        0.2961       0.8672        0.3960  0.0771
    262       0.9000        0.2952       0.8672        0.3958  0.0769
    263       0.9003        0.2946       0.8660        0.3935  0.0773
    264       0.9009        0.2944       0.8648        0.3960  0.0764
    265       0.9000        0.2940       0.8660        0.3960  0.0779
    266       0.9000        0.2942       0.8654        0.3997  0.0777
    267       0.9009        0.2968       0.8694        0.3953  0.0768
    268       0.8995        0.2948       0.8672        0.3964  0.0760
    269       0.8997        0.2967       0.8660        0.3976  0.0760
    270       0.8997        0.2951       0.8660        0.4019  0.0776
    271       0.8989        0.2969       0.8682        0.3956  0.0771
    272       0.8982        0.2964       0.8657        0.4006  0.0765
    273       0.8956        0.2981       0.8645        0.4028  0.0839
    274       0.8972        0.2989       0.8648        0.3983  0.0773
    275       0.8987        0.2967       0.8675        0.3986  0.0763
    276       0.8979        0.2948       0.8648        0.3953  0.0777
    277       0.8993        0.2938       0.8651        0.3989  0.0756
    278       0.9003        0.2948       0.8663        0.3963  0.0772
    279       0.9015        0.2929       0.8642        0.3974  0.0768
    280       0.9006        0.2928       0.8651        0.3995  0.0777
    281       0.9005        0.2946       0.8657        0.3992  0.0779
    282       0.9000        0.2947       0.8657        0.3973  0.0772
    283       0.8980        0.2950       0.8660        0.3980  0.0767
    284       0.9015        0.2928       0.8639        0.3980  0.0764
    285       0.9007        0.2934       0.8654        0.3959  0.0768
    286       0.9010        0.2934       0.8669        0.3972  0.0767
    287       0.9024        0.2934       0.8654        0.4025  0.0757
    288       0.8991        0.2958       0.8672        0.3975  0.0779
    289       0.8991        0.2942       0.8666        0.3975  0.0772
    290       0.9006        0.2924       0.8657        0.3996  0.0856
    291       0.9011        0.2933       0.8648        0.3980  0.0780
    292       0.9004        0.2928       0.8663        0.3973  0.0771
    293       0.9012        0.2929       0.8669        0.3968  0.0773
    294       0.9008        0.2929       0.8657        0.3992  0.0774
    295       0.8991        0.2938       0.8675        0.3951  0.1534
    296       0.9010        0.2918       0.8663        0.3952  0.0777
    297       0.9023        0.2920       0.8639        0.3987  0.0767
    298       0.9007        0.2942       0.8648        0.4009  0.0764
    299       0.9007        0.2934       0.8651        0.3960  0.0779
    300       0.9011        0.2927       0.8663        0.3991  0.0775
    301       0.9015        0.2939       0.8651        0.3970  0.0768
    302       0.9006        0.2944       0.8636        0.3984  0.0774
    303       0.8978        0.2936       0.8630        0.4042  0.0776
    304       0.8991        0.2944       0.8685        0.3975  0.0776
    305       0.9007        0.2934       0.8639        0.3994  0.1031
    306       0.8994        0.2946       0.8663        0.3989  0.0787
    307       0.9008        0.2919       0.8654        0.3975  0.0854
    308       0.9021        0.2921       0.8651        0.3982  0.0764
    309       0.9003        0.2922       0.8660        0.3999  0.0759
    310       0.8998        0.2922       0.8660        0.3978  0.0766
    311       0.9003        0.2940       0.8651        0.3990  0.0770
    312       0.9003        0.2930       0.8663        0.4047  0.0777
    313       0.8997        0.2934       0.8657        0.3974  0.0763
    314       0.9002        0.2930       0.8663        0.3994  0.0761
    315       0.9007        0.2923       0.8636        0.4008  0.0771
    316       0.8998        0.2928       0.8648        0.3983  0.0766
    317       0.9010        0.2919       0.8636        0.4011  0.0767
    318       0.9009        0.2908       0.8675        0.3990  0.0769
    319       0.9024        0.2909       0.8651        0.4004  0.0777
    320       0.9020        0.2905       0.8682        0.3975  0.0768
    321       0.9010        0.2915       0.8663        0.3968  0.0765
    322       0.9002        0.2918       0.8666        0.3972  0.0762
    323       0.9012        0.2901       0.8654        0.3996  0.0861
    324       0.9015        0.2904       0.8651        0.3993  0.0760
    325       0.9007        0.2922       0.8639        0.3984  0.0758
    326       0.9011        0.2924       0.8663        0.3973  0.0765
    327       0.9017        0.2913       0.8691        0.3979  0.0789
    328       0.9012        0.2913       0.8627        0.4029  0.0776
    329       0.9009        0.2916       0.8700        0.3996  0.0788
    330       0.9003        0.2927       0.8624        0.4027  0.0759
    331       0.9009        0.2910       0.8645        0.3982  0.0776
    332       0.9000        0.2920       0.8663        0.3997  0.0765
    333       0.9012        0.2896       0.8642        0.3993  0.0768
    334       0.9014        0.2902       0.8663        0.3951  0.1542
    335       0.9015        0.2907       0.8636        0.3990  0.0766
    336       0.9006        0.2915       0.8651        0.3968  0.0779
    337       0.9016        0.2905       0.8651        0.4017  0.0873
    338       0.9008        0.2909       0.8666        0.3998  0.0780
    339       0.9006        0.2919       0.8657        0.3972  0.0769
    340       0.9006        0.2930       0.8645        0.4005  0.0763
    341       0.8985        0.2913       0.8651        0.4029  0.0776
    342       0.9010        0.2903       0.8657        0.3974  0.0778
    343       0.9009        0.2901       0.8651        0.3996  0.0772
    344       0.9004        0.2912       0.8651        0.4016  0.0756
    345       0.9015        0.2922       0.8654        0.3979  0.0766
    346       0.9000        0.2915       0.8688        0.3946  0.0768
    347       0.9016        0.2901       0.8657        0.4002  0.0774
    348       0.9021        0.2907       0.8612        0.4032  0.0781
    349       0.8998        0.2914       0.8654        0.3973  0.0776
    350       0.9012        0.2900       0.8663        0.3992  0.0848
    351       0.9006        0.2920       0.8633        0.4031  0.0779
    352       0.9018        0.2930       0.8657        0.4009  0.0774
    353       0.9003        0.2921       0.8588        0.4055  0.0771
    354       0.9007        0.2924       0.8645        0.3998  0.0771
    355       0.9011        0.2922       0.8660        0.3996  0.0784
    356       0.9000        0.2929       0.8642        0.4025  0.0769
    357       0.8996        0.2932       0.8657        0.4018  0.0767
    358       0.9009        0.2924       0.8666        0.3981  0.0771
    359       0.9006        0.2901       0.8672        0.4050  0.1006
    360       0.9000        0.2921       0.8660        0.3977  0.0777
    361       0.9008        0.2898       0.8675        0.3982  0.0782
    362       0.9011        0.2898       0.8648        0.4046  0.0765
    363       0.9018        0.2917       0.8669        0.3988  0.0768
    364       0.9014        0.2909       0.8675        0.4015  0.0777
    365       0.9024        0.2896       0.8645        0.3987  0.0836
    366       0.9027        0.2903       0.8682        0.4014  0.0759
    367       0.9014        0.2913       0.8666        0.3996  0.0782
    368       0.8997        0.2915       0.8657        0.4010  0.0775
    369       0.9007        0.2902       0.8688        0.3960  0.0762
    370       0.9016        0.2908       0.8642        0.4018  0.0775
    371       0.9021        0.2892       0.8675        0.3951  0.0770
    372       0.9013        0.2903       0.8606        0.4080  0.1530
    373       0.9024        0.2913       0.8672        0.3999  0.0782
    374       0.9015        0.2919       0.8639        0.3983  0.0767
    375       0.8994        0.2908       0.8633        0.4039  0.0774
    376       0.9001        0.2910       0.8663        0.3988  0.0778
    377       0.9014        0.2900       0.8672        0.3991  0.0770
    378       0.9018        0.2890       0.8703        0.3982  0.0764
    379       0.9017        0.2891       0.8648        0.3997  0.0779
    380       0.9032        0.2894       0.8645        0.4004  0.0767
    381       0.9015        0.2901       0.8651        0.4007  0.0843
    382       0.9021        0.2905       0.8672        0.3988  0.0769
    383       0.9018        0.2885       0.8666        0.3967  0.0782
    384       0.9014        0.2890       0.8669        0.3997  0.0770
    385       0.9024        0.2899       0.8672        0.3998  0.0774
    386       0.9025        0.2905       0.8657        0.4056  0.0766
    387       0.9017        0.2901       0.8669        0.3958  0.0775
    388       0.9023        0.2897       0.8666        0.4025  0.0773
    389       0.9019        0.2891       0.8657        0.4026  0.0764
    390       0.9000        0.2910       0.8694        0.3962  0.0762
    391       0.9023        0.2900       0.8642        0.4018  0.0781
    392       0.9021        0.2892       0.8672        0.4000  0.0765
    393       0.9014        0.2895       0.8636        0.3991  0.0772
    394       0.9024        0.2886       0.8633        0.4047  0.0767
    395       0.9027        0.2899       0.8675        0.3982  0.0786
    396       0.9020        0.2904       0.8666        0.4030  0.0770
    397       0.9015        0.2904       0.8663        0.4000  0.0774
    398       0.9012        0.2905       0.8642        0.4057  0.0871
    399       0.9020        0.2917       0.8663        0.4000  0.0787
    400       0.8998        0.2921       0.8657        0.4001  0.0783
    401       0.9028        0.2906       0.8672        0.4014  0.0767
    402       0.8997        0.2922       0.8679        0.3994  0.0773
    403       0.9008        0.2906       0.8669        0.4009  0.0775
    404       0.9013        0.2886       0.8663        0.4032  0.0754
    405       0.9006        0.2890       0.8654        0.4027  0.0776
    406       0.9020        0.2898       0.8648        0.4043  0.0768
    407       0.9021        0.2897       0.8697        0.3979  0.0770
    408       0.9019        0.2898       0.8639        0.4040  0.0765
    409       0.9020        0.2897       0.8679        0.3981  0.0771
    410       0.9010        0.2898       0.8645        0.4024  0.1551
    411       0.9008        0.2920       0.8663        0.4029  0.0767
    412       0.9003        0.2910       0.8691        0.4005  0.1025
    413       0.9027        0.2887       0.8660        0.3998  0.0765
    414       0.9031        0.2881       0.8648        0.3999  0.0771
    415       0.9027        0.2887       0.8657        0.4001  0.0851
    416       0.9019        0.2882       0.8660        0.4013  0.0779
    417       0.9012        0.2886       0.8657        0.4041  0.0782
    418       0.9033        0.2892       0.8679        0.4007  0.0776
    419       0.9027        0.2893       0.8648        0.4004  0.0775
    420       0.9030        0.2881       0.8700        0.3972  0.0771
    421       0.9026        0.2882       0.8672        0.4006  0.0766
    422       0.9030        0.2883       0.8700        0.4014  0.0775
    423       0.9020        0.2894       0.8621        0.4061  0.0760
    424       0.9017        0.2897       0.8691        0.3979  0.0770
    425       0.9012        0.2887       0.8648        0.4062  0.0776
    426       0.9016        0.2892       0.8654        0.4009  0.0782
    427       0.9015        0.2882       0.8672        0.4009  0.0783
    428       0.9009        0.2896       0.8660        0.4033  0.0768
    429       0.8994        0.2914       0.8645        0.4003  0.0780
    430       0.9016        0.2883       0.8651        0.4047  0.0873
    431       0.9012        0.2894       0.8657        0.4018  0.0781
    432       0.9031        0.2891       0.8660        0.4040  0.0764
    433       0.9012        0.2911       0.8639        0.4039  0.0764
    434       0.9004        0.2902       0.8669        0.4011  0.0772
    435       0.9018        0.2892       0.8651        0.4043  0.0778
    436       0.9018        0.2910       0.8672        0.4011  0.0767
    437       0.9003        0.2911       0.8660        0.4010  0.0763
    438       0.9018        0.2881       0.8660        0.3996  0.0780
    439       0.9030        0.2877       0.8645        0.4045  0.0780
    440       0.9020        0.2881       0.8679        0.3990  0.0765
    441       0.9015        0.2877       0.8657        0.4045  0.0780
    442       0.9030        0.2881       0.8688        0.3994  0.0764
    443       0.9027        0.2885       0.8639        0.4039  0.0775
    444       0.9009        0.2890       0.8675        0.4010  0.0888
    445       0.9027        0.2875       0.8675        0.4006  0.0793
    446       0.9025        0.2886       0.8639        0.4068  0.0797
    447       0.9014        0.2900       0.8672        0.3998  0.0777
    448       0.9021        0.2872       0.8654        0.4029  0.1539
    449       0.9024        0.2888       0.8660        0.4011  0.0773
    450       0.9023        0.2877       0.8645        0.4055  0.0766
    451       0.9018        0.2894       0.8648        0.4006  0.0773
    452       0.9016        0.2879       0.8654        0.4004  0.0778
    453       0.9030        0.2875       0.8691        0.4021  0.0780
    454       0.9009        0.2886       0.8694        0.3993  0.0779
    455       0.9024        0.2890       0.8663        0.4020  0.0784
    456       0.9024        0.2870       0.8666        0.4020  0.0772
    457       0.9030        0.2880       0.8663        0.4011  0.0776
    458       0.9032        0.2879       0.8685        0.4008  0.0776
    459       0.9018        0.2878       0.8682        0.4010  0.0853
    460       0.9021        0.2866       0.8682        0.4028  0.0759
    461       0.9025        0.2871       0.8679        0.3985  0.0775
    462       0.9022        0.2870       0.8682        0.4059  0.0767
    463       0.9017        0.2881       0.8672        0.3995  0.1036
    464       0.9016        0.2874       0.8654        0.4007  0.0760
    465       0.9032        0.2871       0.8675        0.4003  0.0795
    466       0.9017        0.2880       0.8657        0.4068  0.0770
    467       0.9037        0.2883       0.8703        0.3984  0.0774
    468       0.9033        0.2867       0.8651        0.4036  0.0763
    469       0.9032        0.2877       0.8682        0.3991  0.0780
    470       0.9015        0.2877       0.8666        0.4053  0.0785
    471       0.9024        0.2884       0.8672        0.4041  0.0832
    472       0.8991        0.2883       0.8685        0.3991  0.0821
    473       0.9016        0.2877       0.8682        0.4047  0.0915
    474       0.9028        0.2858       0.8666        0.4006  0.0775
    475       0.9025        0.2860       0.8694        0.4015  0.0768
    476       0.9037        0.2858       0.8675        0.4028  0.0777
    477       0.9024        0.2861       0.8691        0.4020  0.0774
    478       0.9030        0.2867       0.8685        0.4022  0.0770
    479       0.9020        0.2863       0.8706        0.3996  0.0765
    480       0.9017        0.2863       0.8679        0.4016  0.0781
    481       0.9012        0.2874       0.8651        0.4038  0.0770
    482       0.9031        0.2896       0.8672        0.4011  0.0763
    483       0.9024        0.2874       0.8688        0.3997  0.0761
    484       0.9022        0.2865       0.8651        0.4052  0.0780
    485       0.9029        0.2865       0.8672        0.4020  0.0794
    486       0.9019        0.2866       0.8688        0.4021  0.1723
    487       0.9028        0.2857       0.8660        0.4028  0.0888
    488       0.9024        0.2848       0.8682        0.4005  0.1041
    489       0.9036        0.2859       0.8685        0.4008  0.0820
    490       0.9033        0.2858       0.8654        0.4033  0.0803
    491       0.9025        0.2865       0.8691        0.4015  0.0802
    492       0.9017        0.2871       0.8636        0.4050  0.0810
    493       0.9003        0.2866       0.8694        0.4032  0.0808
    494       0.9027        0.2861       0.8675        0.4014  0.0909
    495       0.9026        0.2855       0.8682        0.4017  0.0946
    496       0.9024        0.2856       0.8679        0.4020  0.0869
    497       0.9021        0.2853       0.8654        0.4040  0.0813
    498       0.9036        0.2857       0.8679        0.4008  0.0895
    499       0.9019        0.2850       0.8682        0.3992  0.1055
    500       0.9017        0.2860       0.8660        0.4048  0.0791
Out[27]:
Pipeline(steps=[('standardscaler', StandardScaler()),
                ('neuralnetclassifier',
                 NeuralNetClassifier(_params_to_validate={'iterator_train__shuffle', 'module__out_features', 'module__in_features'}, batch_size=2048, callbacks=[<skorch.callbacks.scoring.EpochScoring object at 0x31b2738c0>], compile=False, dataset=<class 'skorch.dataset.Dataset'>, device='mps', iterator...ataLoader'>, iterator_train__shuffle=True, iterator_valid=<class 'torch.utils.data.dataloader.DataLoader'>, lr=0.01, max_epochs=500, module=<class '__main__.VanillaMLP'>, module__in_features=22, module__out_features=7, optimizer=<class 'torch.optim.adam.Adam'>, predict_nonlinearity='auto', torch_load_kwargs=None, use_caching='auto', verbose=1, warm_start=False))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Pipeline(steps=[('standardscaler', StandardScaler()),
                ('neuralnetclassifier',
                 NeuralNetClassifier(_params_to_validate={'iterator_train__shuffle', 'module__out_features', 'module__in_features'}, batch_size=2048, callbacks=[<skorch.callbacks.scoring.EpochScoring object at 0x31b2738c0>], compile=False, dataset=<class 'skorch.dataset.Dataset'>, device='mps', iterator...ataLoader'>, iterator_train__shuffle=True, iterator_valid=<class 'torch.utils.data.dataloader.DataLoader'>, lr=0.01, max_epochs=500, module=<class '__main__.VanillaMLP'>, module__in_features=22, module__out_features=7, optimizer=<class 'torch.optim.adam.Adam'>, predict_nonlinearity='auto', torch_load_kwargs=None, use_caching='auto', verbose=1, warm_start=False))])
StandardScaler()
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=VanillaMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): ReLU()
      (2): Linear(in_features=32, out_features=7, bias=True)
      (3): Softmax(dim=-1)
    )
  ),
)
In [28]:
y_pred = pipe.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[28]:
0.8655852188076297
In [29]:
history = pipe[1].history

plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image

Improving the model¶

In [30]:
class BetterMLP(nn.Module):
    def __init__(self, in_features, out_features):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(in_features, 32),
            nn.BatchNorm1d(32),  # batch normalization layer
            nn.ReLU(),
            nn.Dropout(p=0.2),  # dropout layer with 20% dropout rate
            nn.Linear(32, out_features),
            nn.Softmax(dim=-1),
        )

    def forward(self, X, **kwargs):
        return self.model(X)

The better model, monitoring F1 besides Acc¶

In [31]:
net = NeuralNetClassifier(
    BetterMLP,
    module__in_features=X_train.shape[1],
    module__out_features=len(le.classes_),
    max_epochs=500,
    
    lr = 1e-3,
    device=DEVICE,
    optimizer=torch.optim.Adam,

    callbacks=[
        EpochScoring(
            scoring='f1_weighted',
            name='train_f1',
            on_train=True,
            lower_is_better=False,
        ),
        EpochScoring(
            scoring='f1_weighted',
            name='valid_f1',
            on_train=False,
            lower_is_better=False,
        ),
    ],

    batch_size=2048,
    iterator_train__shuffle=True,
)
net
Out[31]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [32]:
# Training the network
net.fit(X_train, y_train)
  epoch    train_f1    train_loss    valid_acc    valid_f1    valid_loss     dur
-------  ----------  ------------  -----------  ----------  ------------  ------
      1      0.0712        1.9186       0.1469      0.0382        2.5864  1.0286
      2      0.0943        1.8163       0.1328      0.0501        1.9488  0.0847
      3      0.1528        1.7282       0.2098      0.1403        1.7196  0.0836
      4      0.2173        1.6527       0.3281      0.2249        1.6047  0.0888
      5      0.2647        1.5878       0.3326      0.2299        1.5355  0.0932
      6      0.3042        1.5298       0.3064      0.2185        1.4855  0.0900
      7      0.3294        1.4790       0.3200      0.2436        1.4443  0.0861
      8      0.3486        1.4336       0.3323      0.2344        1.4102  0.0828
      9      0.3643        1.3945       0.3320      0.2237        1.3842  0.0830
     10      0.3936        1.3611       0.3347      0.2252        1.3619  0.1050
     11      0.4126        1.3320       0.4747      0.4102        1.3504  0.0813
     12      0.4315        1.3053       0.5009      0.4346        1.3345  0.0813
     13      0.4557        1.2771       0.4985      0.4444        1.3171  0.0826
     14      0.4682        1.2542       0.5220      0.4866        1.3050  0.0809
     15      0.4786        1.2305       0.5385      0.5085        1.2910  0.0823
     16      0.5030        1.2034       0.5590      0.5370        1.2734  0.0812
     17      0.5225        1.1722       0.5695      0.5692        1.2896  0.0809
     18      0.5392        1.1466       0.5713      0.5582        1.2856  0.0806
     19      0.5631        1.1149       0.5641      0.5459        1.2802  0.0806
     20      0.5821        1.0827       0.4603      0.4243        1.2996  0.0809
     21      0.5977        1.0529       0.5500      0.5442        1.2446  0.1603
     22      0.6119        1.0215       0.6219      0.6173        1.1837  0.0887
     23      0.6215        0.9950       0.5834      0.5702        1.1501  0.0928
     24      0.6209        0.9686       0.6285      0.6208        1.0946  0.0912
     25      0.6227        0.9507       0.6343      0.6233        1.0672  0.0852
     26      0.6410        0.9262       0.6445      0.6343        1.0064  0.0811
     27      0.6531        0.9077       0.6731      0.6671        0.9535  0.0806
     28      0.6574        0.8922       0.6728      0.6698        0.9340  0.0814
     29      0.6613        0.8756       0.6833      0.6767        0.8976  0.0850
     30      0.6690        0.8610       0.6824      0.6762        0.8837  0.0925
     31      0.6753        0.8452       0.7056      0.6972        0.8494  0.0929
     32      0.6807        0.8303       0.7065      0.6973        0.8240  0.0859
     33      0.6906        0.8160       0.7125      0.7033        0.8228  0.1072
     34      0.6949        0.8090       0.7291      0.7209        0.7874  0.0848
     35      0.6975        0.7970       0.7101      0.7005        0.8011  0.0811
     36      0.7001        0.7859       0.7267      0.7205        0.7730  0.0810
     37      0.7076        0.7743       0.7363      0.7281        0.7564  0.0815
     38      0.7054        0.7666       0.7423      0.7346        0.7403  0.0812
     39      0.7072        0.7589       0.7134      0.7037        0.7646  0.0822
     40      0.7181        0.7498       0.7523      0.7462        0.7148  0.0805
     41      0.7224        0.7402       0.7574      0.7523        0.7191  0.0804
     42      0.7207        0.7321       0.7502      0.7438        0.7136  0.0831
     43      0.7233        0.7233       0.7622      0.7570        0.6978  0.0812
     44      0.7292        0.7164       0.7417      0.7323        0.7011  0.0802
     45      0.7350        0.7074       0.7577      0.7521        0.6789  0.0840
     46      0.7339        0.6997       0.7294      0.7201        0.7124  0.0813
     47      0.7367        0.6935       0.7514      0.7423        0.6874  0.0805
     48      0.7361        0.6888       0.7538      0.7457        0.6762  0.0802
     49      0.7415        0.6860       0.7724      0.7673        0.6590  0.0814
     50      0.7394        0.6784       0.7778      0.7731        0.6470  0.0811
     51      0.7474        0.6681       0.7426      0.7334        0.6808  0.0819
     52      0.7487        0.6629       0.7902      0.7855        0.6277  0.0800
     53      0.7498        0.6603       0.7658      0.7582        0.6508  0.0807
     54      0.7537        0.6546       0.7769      0.7713        0.6302  0.0808
     55      0.7523        0.6544       0.7763      0.7707        0.6365  0.0820
     56      0.7578        0.6446       0.7914      0.7869        0.6220  0.0806
     57      0.7573        0.6378       0.7938      0.7903        0.6125  0.0806
     58      0.7612        0.6376       0.7652      0.7595        0.6545  0.0816
     59      0.7668        0.6323       0.8001      0.7958        0.5906  0.1588
     60      0.7625        0.6313       0.7414      0.7335        0.6735  0.0809
     61      0.7674        0.6218       0.7995      0.7951        0.5917  0.0812
     62      0.7721        0.6172       0.7872      0.7839        0.6191  0.0824
     63      0.7724        0.6131       0.7929      0.7895        0.6042  0.0814
     64      0.7751        0.6081       0.7878      0.7826        0.6015  0.0820
     65      0.7760        0.6032       0.8061      0.8021        0.5815  0.0821
     66      0.7756        0.6032       0.7923      0.7853        0.6069  0.0821
     67      0.7751        0.5963       0.7812      0.7761        0.6047  0.0816
     68      0.7829        0.5932       0.8019      0.8000        0.5889  0.0808
     69      0.7790        0.5911       0.7727      0.7691        0.6193  0.0812
     70      0.7862        0.5850       0.8230      0.8203        0.5487  0.0809
     71      0.7822        0.5836       0.8082      0.8052        0.5704  0.0803
     72      0.7911        0.5785       0.7842      0.7797        0.6094  0.0825
     73      0.7899        0.5758       0.8245      0.8214        0.5420  0.0816
     74      0.7868        0.5770       0.8185      0.8159        0.5604  0.0789
     75      0.7895        0.5697       0.8206      0.8181        0.5480  0.0860
     76      0.7923        0.5706       0.8128      0.8102        0.5634  0.0814
     77      0.7936        0.5661       0.8311      0.8297        0.5346  0.0904
     78      0.7995        0.5589       0.8341      0.8328        0.5186  0.0938
     79      0.7920        0.5597       0.7793      0.7728        0.5891  0.1328
     80      0.7912        0.5604       0.8278      0.8272        0.5256  0.0863
     81      0.7964        0.5566       0.8061      0.8044        0.5661  0.0939
     82      0.7974        0.5536       0.8197      0.8163        0.5275  0.0861
     83      0.7997        0.5518       0.8287      0.8292        0.5318  0.0851
     84      0.8004        0.5459       0.8137      0.8102        0.5372  0.0883
     85      0.8013        0.5461       0.8263      0.8226        0.5266  0.0942
     86      0.7997        0.5411       0.8368      0.8357        0.5000  0.0946
     87      0.8027        0.5379       0.8007      0.7972        0.5602  0.0929
     88      0.8015        0.5430       0.8396      0.8393        0.4954  0.0832
     89      0.8018        0.5373       0.8293      0.8263        0.5091  0.0857
     90      0.8044        0.5366       0.8380      0.8373        0.5040  0.0967
     91      0.8041        0.5349       0.8359      0.8367        0.5184  0.0924
     92      0.8047        0.5367       0.8149      0.8097        0.5239  0.0866
     93      0.8050        0.5318       0.8365      0.8342        0.5006  0.0878
     94      0.8014        0.5276       0.8356      0.8365        0.4984  0.0847
     95      0.8082        0.5313       0.8248      0.8217        0.5127  0.0855
     96      0.8070        0.5255       0.8365      0.8358        0.4949  0.0861
     97      0.8036        0.5283       0.8417      0.8411        0.4951  0.0940
     98      0.8035        0.5278       0.8155      0.8097        0.5187  0.1626
     99      0.8076        0.5202       0.8377      0.8375        0.5017  0.0831
    100      0.8118        0.5163       0.8182      0.8150        0.5329  0.0839
    101      0.8060        0.5221       0.8317      0.8285        0.4920  0.0834
    102      0.8117        0.5167       0.8377      0.8364        0.4969  0.0870
    103      0.8117        0.5134       0.8438      0.8422        0.4739  0.0906
    104      0.8080        0.5142       0.8444      0.8430        0.4788  0.0867
    105      0.8122        0.5132       0.8206      0.8173        0.5046  0.0861
    106      0.8138        0.5119       0.8414      0.8397        0.4830  0.0847
    107      0.8175        0.5063       0.8417      0.8401        0.4845  0.0834
    108      0.8136        0.5093       0.8245      0.8198        0.4978  0.1054
    109      0.8174        0.5054       0.8486      0.8482        0.4658  0.0846
    110      0.8157        0.5019       0.8450      0.8432        0.4791  0.0832
    111      0.8168        0.5035       0.8453      0.8453        0.4771  0.0926
    112      0.8181        0.5059       0.8489      0.8479        0.4683  0.0928
    113      0.8161        0.5023       0.8317      0.8286        0.4819  0.0955
    114      0.8156        0.5005       0.8387      0.8365        0.4771  0.0872
    115      0.8200        0.5004       0.8489      0.8484        0.4759  0.0870
    116      0.8188        0.4971       0.8329      0.8302        0.4866  0.0846
    117      0.8174        0.4985       0.8393      0.8363        0.4831  0.0863
    118      0.8187        0.4941       0.8480      0.8482        0.4801  0.0884
    119      0.8177        0.4959       0.8480      0.8467        0.4670  0.0919
    120      0.8202        0.4925       0.8347      0.8313        0.4888  0.0923
    121      0.8183        0.4944       0.8519      0.8515        0.4618  0.0894
    122      0.8223        0.4914       0.8332      0.8301        0.4882  0.0868
    123      0.8249        0.4910       0.8302      0.8313        0.4865  0.0935
    124      0.8226        0.4923       0.8380      0.8346        0.4672  0.0855
    125      0.8251        0.4897       0.8414      0.8391        0.4798  0.0857
    126      0.8262        0.4874       0.8483      0.8485        0.4606  0.0858
    127      0.8232        0.4853       0.8356      0.8320        0.4805  0.0845
    128      0.8259        0.4853       0.8462      0.8447        0.4610  0.0835
    129      0.8225        0.4858       0.8447      0.8438        0.4669  0.0875
    130      0.8235        0.4828       0.8501      0.8489        0.4543  0.1232
    131      0.8241        0.4809       0.8531      0.8526        0.4560  0.1016
    132      0.8253        0.4813       0.8540      0.8534        0.4696  0.0936
    133      0.8237        0.4817       0.8438      0.8408        0.4528  0.0982
    134      0.8294        0.4823       0.8498      0.8493        0.4511  0.0981
    135      0.8272        0.4770       0.8417      0.8397        0.4671  0.1164
    136      0.8228        0.4830       0.8504      0.8507        0.4600  0.1731
    137      0.8263        0.4797       0.8426      0.8394        0.4648  0.0913
    138      0.8267        0.4766       0.8417      0.8396        0.4617  0.0946
    139      0.8273        0.4762       0.8510      0.8495        0.4587  0.0958
    140      0.8296        0.4730       0.8465      0.8447        0.4582  0.0874
    141      0.8306        0.4731       0.8558      0.8548        0.4440  0.0846
    142      0.8245        0.4775       0.8438      0.8421        0.4639  0.0852
    143      0.8287        0.4718       0.8516      0.8509        0.4571  0.0856
    144      0.8280        0.4699       0.8531      0.8525        0.4371  0.0809
    145      0.8299        0.4703       0.8405      0.8377        0.4629  0.0815
    146      0.8301        0.4706       0.8519      0.8500        0.4408  0.0826
    147      0.8271        0.4689       0.8432      0.8403        0.4527  0.0804
    148      0.8334        0.4673       0.8549      0.8543        0.4486  0.0873
    149      0.8235        0.4742       0.8540      0.8529        0.4395  0.0930
    150      0.8278        0.4715       0.8567      0.8563        0.4288  0.1243
    151      0.8300        0.4690       0.8507      0.8500        0.4365  0.0918
    152      0.8327        0.4676       0.8561      0.8548        0.4364  0.0945
    153      0.8315        0.4667       0.8567      0.8563        0.4367  0.0952
    154      0.8320        0.4622       0.8474      0.8443        0.4511  0.0927
    155      0.8324        0.4636       0.8402      0.8419        0.4629  0.1425
    156      0.8250        0.4656       0.8314      0.8270        0.4809  0.0965
    157      0.8368        0.4593       0.8609      0.8602        0.4368  0.1002
    158      0.8354        0.4606       0.8582      0.8568        0.4381  0.0908
    159      0.8342        0.4615       0.8516      0.8512        0.4258  0.0892
    160      0.8326        0.4587       0.8588      0.8582        0.4376  0.0847
    161      0.8311        0.4607       0.8564      0.8561        0.4283  0.0828
    162      0.8350        0.4585       0.8534      0.8525        0.4414  0.0868
    163      0.8348        0.4584       0.8555      0.8535        0.4432  0.0884
    164      0.8379        0.4555       0.8627      0.8627        0.4345  0.0932
    165      0.8366        0.4542       0.8600      0.8602        0.4302  0.0936
    166      0.8389        0.4550       0.8519      0.8495        0.4420  0.1078
    167      0.8339        0.4590       0.8519      0.8511        0.4261  0.0941
    168      0.8348        0.4588       0.8513      0.8494        0.4443  0.0920
    169      0.8362        0.4534       0.8477      0.8466        0.4382  0.0854
    170      0.8380        0.4545       0.8573      0.8561        0.4319  0.0891
    171      0.8379        0.4501       0.8522      0.8518        0.4267  0.0911
    172      0.8353        0.4550       0.8546      0.8531        0.4286  0.0968
    173      0.8363        0.4524       0.8546      0.8526        0.4418  0.1333
    174      0.8364        0.4525       0.8531      0.8536        0.4327  0.1647
    175      0.8339        0.4542       0.8546      0.8526        0.4372  0.0878
    176      0.8361        0.4494       0.8456      0.8425        0.4509  0.0855
    177      0.8354        0.4598       0.8486      0.8486        0.4366  0.0848
    178      0.8359        0.4512       0.8594      0.8590        0.4147  0.0909
    179      0.8369        0.4532       0.8534      0.8510        0.4288  0.0835
    180      0.8386        0.4489       0.8540      0.8539        0.4257  0.0830
    181      0.8375        0.4494       0.8558      0.8544        0.4346  0.0807
    182      0.8380        0.4530       0.8320      0.8322        0.4577  0.0833
    183      0.8401        0.4497       0.8588      0.8573        0.4325  0.0827
    184      0.8396        0.4449       0.8552      0.8529        0.4309  0.0828
    185      0.8378        0.4492       0.8540      0.8534        0.4327  0.0872
    186      0.8398        0.4467       0.8615      0.8598        0.4356  0.0830
    187      0.8402        0.4449       0.8528      0.8509        0.4375  0.0841
    188      0.8370        0.4523       0.8299      0.8329        0.4643  0.0848
    189      0.8395        0.4458       0.8377      0.8336        0.4762  0.0846
    190      0.8426        0.4440       0.8540      0.8517        0.4324  0.0903
    191      0.8426        0.4403       0.8558      0.8570        0.4270  0.0862
    192      0.8371        0.4423       0.8507      0.8476        0.4372  0.0858
    193      0.8405        0.4468       0.8468      0.8441        0.4496  0.0901
    194      0.8422        0.4439       0.8543      0.8544        0.4308  0.0823
    195      0.8416        0.4447       0.8483      0.8457        0.4384  0.0847
    196      0.8392        0.4471       0.8564      0.8578        0.4288  0.0841
    197      0.8399        0.4408       0.8555      0.8545        0.4257  0.0833
    198      0.8410        0.4439       0.8597      0.8578        0.4215  0.0831
    199      0.8410        0.4442       0.8582      0.8571        0.4176  0.0842
    200      0.8428        0.4417       0.8546      0.8546        0.4225  0.0838
    201      0.8404        0.4414       0.8540      0.8518        0.4275  0.0834
    202      0.8433        0.4432       0.8543      0.8551        0.4235  0.0823
    203      0.8428        0.4372       0.8561      0.8541        0.4314  0.0847
    204      0.8405        0.4399       0.8570      0.8551        0.4253  0.0869
    205      0.8427        0.4375       0.8543      0.8516        0.4245  0.0832
    206      0.8426        0.4345       0.8642      0.8645        0.4138  0.0839
    207      0.8448        0.4354       0.8615      0.8609        0.4102  0.0832
    208      0.8423        0.4372       0.8642      0.8634        0.4215  0.0924
    209      0.8455        0.4377       0.8549      0.8538        0.4230  0.0825
    210      0.8425        0.4388       0.8540      0.8530        0.4152  0.0842
    211      0.8409        0.4392       0.8576      0.8579        0.4167  0.0827
    212      0.8401        0.4370       0.8540      0.8521        0.4218  0.1600
    213      0.8422        0.4368       0.8399      0.8393        0.4339  0.0826
    214      0.8427        0.4343       0.8600      0.8588        0.4182  0.0824
    215      0.8447        0.4346       0.8576      0.8581        0.4142  0.0839
    216      0.8437        0.4338       0.8353      0.8284        0.4727  0.1080
    217      0.8400        0.4366       0.8362      0.8373        0.4496  0.0829
    218      0.8460        0.4340       0.8507      0.8468        0.4464  0.0828
    219      0.8446        0.4331       0.8621      0.8621        0.4116  0.0831
    220      0.8427        0.4372       0.8573      0.8547        0.4336  0.0836
    221      0.8476        0.4333       0.8441      0.8446        0.4298  0.0873
    222      0.8459        0.4333       0.8597      0.8605        0.4328  0.0915
    223      0.8445        0.4334       0.8423      0.8390        0.4524  0.0982
    224      0.8482        0.4346       0.8618      0.8611        0.4089  0.0835
    225      0.8466        0.4352       0.8567      0.8548        0.4342  0.0852
    226      0.8477        0.4307       0.8522      0.8499        0.4281  0.0829
    227      0.8444        0.4306       0.8600      0.8604        0.4097  0.0829
    228      0.8444        0.4297       0.8320      0.8288        0.4804  0.0831
    229      0.8422        0.4368       0.8534      0.8525        0.4203  0.0830
    230      0.8437        0.4352       0.8597      0.8577        0.4290  0.0843
    231      0.8452        0.4306       0.8522      0.8538        0.4243  0.0824
    232      0.8442        0.4301       0.7965      0.7865        0.5295  0.0868
    233      0.8487        0.4319       0.8621      0.8626        0.4179  0.0909
    234      0.8443        0.4307       0.8522      0.8524        0.4129  0.0933
    235      0.8434        0.4321       0.8591      0.8570        0.4147  0.0845
    236      0.8452        0.4280       0.8594      0.8592        0.4146  0.0870
    237      0.8460        0.4290       0.8525      0.8535        0.4169  0.0847
    238      0.8467        0.4311       0.8528      0.8498        0.4311  0.0817
    239      0.8430        0.4335       0.8621      0.8620        0.4134  0.0817
    240      0.8467        0.4271       0.8621      0.8608        0.4109  0.0820
    241      0.8451        0.4252       0.8579      0.8585        0.4223  0.0831
    242      0.8470        0.4266       0.8672      0.8660        0.4098  0.0839
    243      0.8440        0.4309       0.8621      0.8608        0.4073  0.0908
    244      0.8505        0.4262       0.8612      0.8605        0.4064  0.1305
    245      0.8473        0.4288       0.8579      0.8577        0.4202  0.0964
    246      0.8460        0.4277       0.8615      0.8599        0.4105  0.0840
    247      0.8524        0.4263       0.8492      0.8466        0.4271  0.0821
    248      0.8495        0.4241       0.8639      0.8637        0.4014  0.0837
    249      0.8488        0.4277       0.8639      0.8635        0.4043  0.0826
    250      0.8496        0.4269       0.8522      0.8526        0.4141  0.0818
    251      0.8494        0.4251       0.8447      0.8403        0.4503  0.1661
    252      0.8466        0.4294       0.8654      0.8652        0.4065  0.0886
    253      0.8528        0.4225       0.8507      0.8482        0.4374  0.0846
    254      0.8447        0.4283       0.8525      0.8509        0.4184  0.0828
    255      0.8497        0.4228       0.8483      0.8492        0.4177  0.0830
    256      0.8485        0.4264       0.8669      0.8661        0.4065  0.0845
    257      0.8485        0.4272       0.8561      0.8547        0.4089  0.0879
    258      0.8494        0.4215       0.8633      0.8631        0.4051  0.0856
    259      0.8470        0.4233       0.8414      0.8376        0.4590  0.0814
    260      0.8458        0.4301       0.8423      0.8434        0.4251  0.0821
    261      0.8523        0.4190       0.8564      0.8532        0.4227  0.0830
    262      0.8496        0.4204       0.8666      0.8655        0.4030  0.0840
    263      0.8484        0.4211       0.8636      0.8638        0.3999  0.0828
    264      0.8519        0.4205       0.8642      0.8627        0.4245  0.0828
    265      0.8510        0.4254       0.8504      0.8492        0.4244  0.0824
    266      0.8521        0.4253       0.8633      0.8618        0.4188  0.0821
    267      0.8537        0.4240       0.8483      0.8491        0.4225  0.0829
    268      0.8528        0.4226       0.8534      0.8503        0.4358  0.0825
    269      0.8521        0.4228       0.8624      0.8637        0.4096  0.0882
    270      0.8515        0.4225       0.8609      0.8603        0.4087  0.0820
    271      0.8504        0.4249       0.8570      0.8542        0.4244  0.0847
    272      0.8457        0.4219       0.8576      0.8568        0.4111  0.0830
    273      0.8517        0.4186       0.8682      0.8686        0.4000  0.0832
    274      0.8509        0.4206       0.8615      0.8610        0.4061  0.0815
    275      0.8498        0.4182       0.8519      0.8488        0.4221  0.0829
    276      0.8568        0.4169       0.8627      0.8625        0.4078  0.0821
    277      0.8531        0.4180       0.8600      0.8593        0.4075  0.0808
    278      0.8513        0.4191       0.8549      0.8535        0.4148  0.0830
    279      0.8506        0.4216       0.8630      0.8618        0.4007  0.0816
    280      0.8525        0.4185       0.8636      0.8635        0.4002  0.0821
    281      0.8532        0.4193       0.8648      0.8639        0.4046  0.0829
    282      0.8533        0.4145       0.8639      0.8639        0.4119  0.0905
    283      0.8515        0.4212       0.8600      0.8576        0.4202  0.0831
    284      0.8505        0.4195       0.8645      0.8645        0.4058  0.1092
    285      0.8519        0.4198       0.8645      0.8639        0.3999  0.0820
    286      0.8525        0.4189       0.8546      0.8542        0.4075  0.0807
    287      0.8553        0.4172       0.8498      0.8459        0.4434  0.0829
    288      0.8544        0.4164       0.8525      0.8525        0.4226  0.0819
    289      0.8481        0.4233       0.8170      0.8140        0.5024  0.1625
    290      0.8495        0.4188       0.8459      0.8464        0.4227  0.0823
    291      0.8526        0.4202       0.8639      0.8629        0.4045  0.0819
    292      0.8491        0.4235       0.8651      0.8642        0.4037  0.0834
    293      0.8556        0.4177       0.8627      0.8617        0.4079  0.0827
    294      0.8500        0.4216       0.8564      0.8541        0.4245  0.0823
    295      0.8507        0.4144       0.8423      0.8419        0.4247  0.0911
    296      0.8515        0.4222       0.8672      0.8675        0.4100  0.0845
    297      0.8553        0.4112       0.8492      0.8483        0.4199  0.0837
    298      0.8519        0.4158       0.8582      0.8570        0.4089  0.0840
    299      0.8537        0.4117       0.8591      0.8579        0.4344  0.0820
    300      0.8517        0.4190       0.8576      0.8552        0.4331  0.0826
    301      0.8539        0.4154       0.8555      0.8555        0.4076  0.0827
    302      0.8534        0.4144       0.8549      0.8527        0.4271  0.0850
    303      0.8563        0.4136       0.8438      0.8456        0.4192  0.0834
    304      0.8553        0.4147       0.8591      0.8572        0.4143  0.0836
    305      0.8523        0.4138       0.8609      0.8606        0.4092  0.0821
    306      0.8526        0.4158       0.8582      0.8577        0.4056  0.0827
    307      0.8540        0.4145       0.8660      0.8652        0.4075  0.0909
    308      0.8559        0.4110       0.8651      0.8646        0.4031  0.0826
    309      0.8555        0.4117       0.8582      0.8578        0.4025  0.0829
    310      0.8582        0.4117       0.8660      0.8656        0.3964  0.0828
    311      0.8566        0.4121       0.8549      0.8533        0.4177  0.0837
    312      0.8539        0.4138       0.8612      0.8608        0.4041  0.0817
    313      0.8541        0.4100       0.8558      0.8563        0.4162  0.0837
    314      0.8522        0.4132       0.8606      0.8591        0.4138  0.0834
    315      0.8532        0.4123       0.8591      0.8583        0.4036  0.0826
    316      0.8533        0.4166       0.8498      0.8507        0.4123  0.0838
    317      0.8513        0.4159       0.8651      0.8637        0.4058  0.0886
    318      0.8526        0.4146       0.8645      0.8639        0.3942  0.0857
    319      0.8564        0.4111       0.8591      0.8576        0.4082  0.0833
    320      0.8569        0.4094       0.8546      0.8558        0.4061  0.0839
    321      0.8555        0.4105       0.8612      0.8574        0.4259  0.0856
    322      0.8544        0.4133       0.8453      0.8431        0.4383  0.0839
    323      0.8567        0.4149       0.8675      0.8673        0.4034  0.0924
    324      0.8525        0.4129       0.8642      0.8626        0.4079  0.0822
    325      0.8565        0.4081       0.8630      0.8626        0.4035  0.0845
    326      0.8567        0.4068       0.8654      0.8633        0.4154  0.0840
    327      0.8535        0.4162       0.8540      0.8540        0.4129  0.0831
    328      0.8589        0.4089       0.8660      0.8659        0.4039  0.1603
    329      0.8578        0.4115       0.8609      0.8595        0.4071  0.0837
    330      0.8551        0.4095       0.8633      0.8624        0.3972  0.0820
    331      0.8554        0.4092       0.8585      0.8572        0.4092  0.0833
    332      0.8553        0.4141       0.8688      0.8678        0.4013  0.0850
    333      0.8546        0.4099       0.8648      0.8626        0.4108  0.0875
    334      0.8512        0.4165       0.8456      0.8445        0.4257  0.1248
    335      0.8542        0.4112       0.8633      0.8640        0.4142  0.0915
    336      0.8575        0.4091       0.8576      0.8579        0.4091  0.0856
    337      0.8572        0.4066       0.8474      0.8452        0.4287  0.0849
    338      0.8550        0.4130       0.8618      0.8607        0.4003  0.0877
    339      0.8563        0.4098       0.8600      0.8596        0.3976  0.0873
    340      0.8565        0.4098       0.8633      0.8625        0.4002  0.0872
    341      0.8552        0.4107       0.8621      0.8612        0.4018  0.0874
    342      0.8532        0.4142       0.8600      0.8599        0.4157  0.0844
    343      0.8540        0.4155       0.8636      0.8618        0.4073  0.0848
    344      0.8562        0.4135       0.8459      0.8454        0.4246  0.0860
    345      0.8548        0.4140       0.8543      0.8512        0.4246  0.0849
    346      0.8550        0.4122       0.8570      0.8570        0.4107  0.0834
    347      0.8598        0.4085       0.8609      0.8588        0.4146  0.0904
    348      0.8541        0.4089       0.8636      0.8635        0.3911  0.0883
    349      0.8586        0.4073       0.8567      0.8547        0.4175  0.0885
    350      0.8573        0.4078       0.8675      0.8670        0.3998  0.0878
    351      0.8566        0.4086       0.8597      0.8600        0.4011  0.0845
    352      0.8554        0.4104       0.8486      0.8474        0.4186  0.0828
    353      0.8564        0.4084       0.8645      0.8635        0.4156  0.0847
    354      0.8602        0.4034       0.8675      0.8663        0.3965  0.0820
    355      0.8588        0.4027       0.8528      0.8514        0.4131  0.0820
    356      0.8574        0.4106       0.8576      0.8586        0.4101  0.0838
    357      0.8554        0.4075       0.8603      0.8590        0.4106  0.0846
    358      0.8572        0.4091       0.8552      0.8540        0.4259  0.0859
    359      0.8551        0.4103       0.8543      0.8508        0.4234  0.0829
    360      0.8549        0.4102       0.8546      0.8550        0.4153  0.0935
    361      0.8575        0.4058       0.8597      0.8576        0.4268  0.0871
    362      0.8585        0.4050       0.8615      0.8620        0.4032  0.0846
    363      0.8564        0.4055       0.8582      0.8574        0.4039  0.0845
    364      0.8608        0.4062       0.8648      0.8636        0.4003  0.0855
    365      0.8547        0.4130       0.8606      0.8585        0.4135  0.0829
    366      0.8571        0.4069       0.8645      0.8634        0.4030  0.1621
    367      0.8568        0.4102       0.8618      0.8599        0.4051  0.0837
    368      0.8576        0.4080       0.8567      0.8572        0.4062  0.0834
    369      0.8560        0.4065       0.8573      0.8582        0.4015  0.0829
    370      0.8605        0.4037       0.8645      0.8630        0.4108  0.0824
    371      0.8611        0.4040       0.8660      0.8665        0.4002  0.0843
    372      0.8569        0.4065       0.8660      0.8647        0.4127  0.0908
    373      0.8593        0.4030       0.8450      0.8438        0.4191  0.0832
    374      0.8574        0.4105       0.8609      0.8594        0.4158  0.0823
    375      0.8581        0.4036       0.8642      0.8632        0.3964  0.0812
    376      0.8583        0.4041       0.8510      0.8515        0.4161  0.0834
    377      0.8594        0.4069       0.8564      0.8557        0.4048  0.0837
    378      0.8566        0.4111       0.8615      0.8600        0.4077  0.0836
    379      0.8610        0.3995       0.8688      0.8690        0.3972  0.0825
    380      0.8578        0.4060       0.8621      0.8600        0.4130  0.0815
    381      0.8593        0.4052       0.8606      0.8611        0.3950  0.1083
    382      0.8574        0.4043       0.8639      0.8632        0.3952  0.0820
    383      0.8583        0.4061       0.8588      0.8577        0.4104  0.0831
    384      0.8597        0.4065       0.8663      0.8667        0.3927  0.0833
    385      0.8604        0.4037       0.8480      0.8466        0.4144  0.0822
    386      0.8598        0.4013       0.8633      0.8623        0.4139  0.0898
    387      0.8586        0.4058       0.8618      0.8613        0.4112  0.0827
    388      0.8596        0.4054       0.8645      0.8639        0.3947  0.0829
    389      0.8577        0.4025       0.8624      0.8608        0.4041  0.0824
    390      0.8582        0.4024       0.8534      0.8535        0.4011  0.0835
    391      0.8568        0.4031       0.8576      0.8586        0.3973  0.0829
    392      0.8596        0.4020       0.8639      0.8632        0.4002  0.0817
    393      0.8575        0.4063       0.8633      0.8617        0.4064  0.0839
    394      0.8595        0.4007       0.8591      0.8591        0.4075  0.0823
    395      0.8591        0.4018       0.8612      0.8591        0.4157  0.0824
    396      0.8543        0.4099       0.8639      0.8636        0.3914  0.0839
    397      0.8564        0.4102       0.8636      0.8635        0.4001  0.0819
    398      0.8628        0.4023       0.8576      0.8574        0.3985  0.0862
    399      0.8615        0.3993       0.8615      0.8605        0.4022  0.0971
    400      0.8606        0.3990       0.8570      0.8567        0.4049  0.0856
    401      0.8637        0.3972       0.8633      0.8614        0.4075  0.0847
    402      0.8591        0.4022       0.8642      0.8636        0.4089  0.0870
    403      0.8610        0.3986       0.8555      0.8548        0.4068  0.0856
    404      0.8610        0.3983       0.8573      0.8577        0.4037  0.0827
    405      0.8605        0.4017       0.8624      0.8602        0.4178  0.1566
    406      0.8592        0.4028       0.8588      0.8581        0.4165  0.0835
    407      0.8555        0.4088       0.8591      0.8583        0.3936  0.0833
    408      0.8599        0.4029       0.8591      0.8580        0.4033  0.0832
    409      0.8566        0.4016       0.8603      0.8613        0.4040  0.0839
    410      0.8652        0.4007       0.8639      0.8636        0.4064  0.0838
    411      0.8612        0.4008       0.8627      0.8617        0.4084  0.0828
    412      0.8586        0.4024       0.8585      0.8570        0.4028  0.0884
    413      0.8578        0.4067       0.8669      0.8659        0.3970  0.0880
    414      0.8608        0.3994       0.8639      0.8645        0.3920  0.0963
    415      0.8588        0.4010       0.8666      0.8656        0.4027  0.0830
    416      0.8592        0.4033       0.8564      0.8561        0.3994  0.0844
    417      0.8623        0.4041       0.8579      0.8577        0.3965  0.0833
    418      0.8597        0.4014       0.8679      0.8666        0.4004  0.0826
    419      0.8607        0.4025       0.8627      0.8624        0.3901  0.0835
    420      0.8613        0.4011       0.8606      0.8609        0.3959  0.0812
    421      0.8591        0.4017       0.8648      0.8640        0.3888  0.0831
    422      0.8628        0.4006       0.8549      0.8541        0.4095  0.0836
    423      0.8592        0.3988       0.8621      0.8613        0.4089  0.0816
    424      0.8592        0.3989       0.8639      0.8632        0.3931  0.0831
    425      0.8547        0.4058       0.8495      0.8483        0.4174  0.0837
    426      0.8570        0.4062       0.8660      0.8652        0.4018  0.0818
    427      0.8613        0.4059       0.8597      0.8599        0.3979  0.0834
    428      0.8628        0.3975       0.8660      0.8649        0.3945  0.0823
    429      0.8581        0.3995       0.8618      0.8607        0.4012  0.0893
    430      0.8601        0.4056       0.8672      0.8674        0.3944  0.1127
    431      0.8617        0.4005       0.8555      0.8542        0.4125  0.0826
    432      0.8589        0.4011       0.8615      0.8616        0.3981  0.0819
    433      0.8599        0.4006       0.8660      0.8658        0.3882  0.0827
    434      0.8610        0.4012       0.8648      0.8647        0.3926  0.0818
    435      0.8611        0.3999       0.8549      0.8545        0.3989  0.0821
    436      0.8632        0.3910       0.8633      0.8634        0.4096  0.0840
    437      0.8588        0.4012       0.8709      0.8697        0.3945  0.0902
    438      0.8598        0.4027       0.8549      0.8540        0.4049  0.0912
    439      0.8606        0.4001       0.8570      0.8549        0.4179  0.0881
    440      0.8619        0.3943       0.8657      0.8664        0.3995  0.0858
    441      0.8614        0.3976       0.8591      0.8578        0.4091  0.0864
    442      0.8597        0.4032       0.8669      0.8662        0.4058  0.0873
    443      0.8558        0.4044       0.8579      0.8571        0.4033  0.1765
    444      0.8601        0.3971       0.8329      0.8317        0.4373  0.0829
    445      0.8626        0.3983       0.8688      0.8682        0.4061  0.0830
    446      0.8602        0.4001       0.8477      0.8463        0.4385  0.0834
    447      0.8590        0.3973       0.8477      0.8467        0.4211  0.0830
    448      0.8643        0.3960       0.8630      0.8622        0.4104  0.0817
    449      0.8577        0.3992       0.8675      0.8675        0.3981  0.0824
    450      0.8595        0.4043       0.8600      0.8586        0.3984  0.0833
    451      0.8619        0.3963       0.8564      0.8554        0.4036  0.0827
    452      0.8650        0.3961       0.8609      0.8601        0.3999  0.0824
    453      0.8613        0.3947       0.8679      0.8684        0.3945  0.0890
    454      0.8573        0.4024       0.8688      0.8676        0.3983  0.0921
    455      0.8586        0.4037       0.8525      0.8518        0.4110  0.0848
    456      0.8621        0.4030       0.8588      0.8593        0.4021  0.0905
    457      0.8629        0.3964       0.8552      0.8533        0.4331  0.0844
    458      0.8627        0.4058       0.8564      0.8563        0.4103  0.0840
    459      0.8581        0.4029       0.8633      0.8619        0.3994  0.0833
    460      0.8601        0.3951       0.8669      0.8662        0.4063  0.0831
    461      0.8607        0.3957       0.8585      0.8590        0.4062  0.0884
    462      0.8600        0.3974       0.8564      0.8536        0.4181  0.0854
    463      0.8587        0.3996       0.8645      0.8638        0.3979  0.0818
    464      0.8623        0.3950       0.8639      0.8632        0.4005  0.0844
    465      0.8605        0.4026       0.8675      0.8663        0.4015  0.0909
    466      0.8625        0.3945       0.8513      0.8504        0.4088  0.0923
    467      0.8603        0.3958       0.8621      0.8619        0.4090  0.0844
    468      0.8603        0.3933       0.8651      0.8639        0.3982  0.0837
    469      0.8615        0.4005       0.8648      0.8639        0.4035  0.0834
    470      0.8580        0.3994       0.8507      0.8499        0.4084  0.0842
    471      0.8629        0.3992       0.8624      0.8617        0.4036  0.0828
    472      0.8634        0.3968       0.8663      0.8659        0.4034  0.0908
    473      0.8616        0.3934       0.8633      0.8627        0.3934  0.0830
    474      0.8609        0.4007       0.8609      0.8593        0.4002  0.0821
    475      0.8637        0.3946       0.8519      0.8518        0.4162  0.0835
    476      0.8591        0.3968       0.8579      0.8566        0.4150  0.0830
    477      0.8610        0.3968       0.8597      0.8601        0.3942  0.0828
    478      0.8620        0.3961       0.8576      0.8571        0.4016  0.1083
    479      0.8618        0.3967       0.8657      0.8641        0.3966  0.0840
    480      0.8593        0.3966       0.8615      0.8599        0.4043  0.0818
    481      0.8621        0.3968       0.8618      0.8624        0.3978  0.1608
    482      0.8624        0.3962       0.8519      0.8482        0.4484  0.0829
    483      0.8590        0.3996       0.8438      0.8448        0.4273  0.0835
    484      0.8601        0.4056       0.8570      0.8551        0.4066  0.0829
    485      0.8593        0.3985       0.8621      0.8622        0.4066  0.0823
    486      0.8597        0.3949       0.8546      0.8542        0.4085  0.0827
    487      0.8635        0.3957       0.8555      0.8560        0.3968  0.0826
    488      0.8620        0.3967       0.8597      0.8581        0.4069  0.0837
    489      0.8614        0.4029       0.8582      0.8574        0.4126  0.0901
    490      0.8619        0.3925       0.8492      0.8496        0.4094  0.0821
    491      0.8611        0.3962       0.8474      0.8421        0.4516  0.0850
    492      0.8661        0.3927       0.8600      0.8585        0.4130  0.0818
    493      0.8651        0.3951       0.8694      0.8687        0.3893  0.0818
    494      0.8575        0.4083       0.8663      0.8649        0.4131  0.0822
    495      0.8600        0.3959       0.8663      0.8653        0.3989  0.0834
    496      0.8619        0.3941       0.8603      0.8596        0.3972  0.0825
    497      0.8611        0.3939       0.8621      0.8628        0.3981  0.0840
    498      0.8626        0.3950       0.8645      0.8635        0.3916  0.0831
    499      0.8596        0.3948       0.8642      0.8628        0.4055  0.0821
    500      0.8614        0.3976       0.8597      0.8594        0.3929  0.0828
Out[32]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Dropout(p=0.2, inplace=False)
      (4): Linear(in_features=32, out_features=7, bias=True)
      (5): Softmax(dim=-1)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Dropout(p=0.2, inplace=False)
      (4): Linear(in_features=32, out_features=7, bias=True)
      (5): Softmax(dim=-1)
    )
  ),
)
In [33]:
y_pred = net.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[33]:
0.8731897654334311
In [34]:
history = net.history

plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_f1'], label='train_f1')
plt.plot(history[:, 'valid_f1'], label='valid_f1')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('F1')
plt.show()
No description has been provided for this image
No description has been provided for this image

Better Training¶

In [35]:
from skorch.callbacks import LRScheduler, EarlyStopping, Checkpoint
from torch.optim.lr_scheduler import ReduceLROnPlateau
In [36]:
class BetterMLP(nn.Module):
    def __init__(self, in_features, out_features):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(in_features, 32),
            nn.BatchNorm1d(32),  # batch normalization layer
            nn.ReLU(),
            nn.Dropout(p=0.2),  # dropout layer with 20% dropout rate
            nn.Linear(32, 16),
            nn.ReLU(),
            nn.Linear(16, out_features),
            nn.Softmax(dim=-1),
        )

    def forward(self, X, **kwargs):
        return self.model(X)
In [37]:
from skorch.dataset import ValidSplit

net = NeuralNetClassifier(
    BetterMLP,
    module__in_features=X_train.shape[1],
    module__out_features=len(le.classes_),
    max_epochs=500,
    
    lr = 1e-3,
    device=DEVICE,
    optimizer=torch.optim.AdamW,  # using AdamW instead of Adam for better regularization

    callbacks=[
        EpochScoring(
            scoring='f1_weighted',
            name='train_f1',
            on_train=True,
            lower_is_better=False,
        ),
        EpochScoring(
            scoring='f1_weighted',
            name='valid_f1',
            on_train=False,
            lower_is_better=False,
        ),
        EarlyStopping(
            monitor="valid_f1",
            patience=50,
            lower_is_better=False,
        ),
        LRScheduler(
            policy=ReduceLROnPlateau,
            monitor="valid_f1",     
            mode="max",              
            factor=0.5,              
            patience=25,       
            threshold=1e-4,
            min_lr=1e-6,
            verbose=True,
        ),
        Checkpoint(
            monitor="valid_acc_best", 
            load_best=True,
            f_history=None,
            f_optimizer=None,
        )
    ],
    train_split=ValidSplit(cv=5, stratified=True),

    batch_size=2048,
    iterator_train__shuffle=True,
)
net
Out[37]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=22,
  module__out_features=7,
)
In [38]:
# Training the network
net.fit(X_train, y_train)
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/torch/optim/lr_scheduler.py:28: UserWarning: The verbose parameter is deprecated. Please use get_last_lr() to access the learning rate.
  warnings.warn("The verbose parameter is deprecated. Please use get_last_lr() "
  epoch    train_f1    train_loss    valid_acc    valid_f1    valid_loss    cp      lr     dur
-------  ----------  ------------  -----------  ----------  ------------  ----  ------  ------
      1      0.0830        1.9260       0.2914      0.1600        1.9539     +  0.0010  0.4120
      2      0.1850        1.8699       0.2718      0.1739        1.8814        0.0010  0.1013
      3      0.2015        1.8139       0.3104      0.1848        1.8047     +  0.0010  0.1000
      4      0.1937        1.7535       0.3152      0.1703        1.7270     +  0.0010  0.0916
      5      0.1846        1.6877       0.3170      0.1677        1.6488     +  0.0010  0.0918
      6      0.1801        1.6216       0.3209      0.1745        1.5796     +  0.0010  0.0918
      7      0.1797        1.5583       0.3200      0.1732        1.5111        0.0010  0.0880
      8      0.1845        1.5001       0.3104      0.1737        1.4591        0.0010  0.0891
      9      0.1917        1.4470       0.2914      0.1951        1.4184        0.0010  0.0893
     10      0.2063        1.4011       0.2974      0.2017        1.3791        0.0010  0.0994
     11      0.2258        1.3611       0.3046      0.2167        1.3560        0.0010  0.1279
     12      0.2540        1.3213       0.3206      0.2416        1.3406        0.0010  0.0967
     13      0.2821        1.2876       0.3582      0.2753        1.3242     +  0.0010  0.0929
     14      0.3195        1.2537       0.3700      0.2814        1.2863     +  0.0010  0.0903
     15      0.3676        1.2217       0.3934      0.3179        1.2419     +  0.0010  0.0903
     16      0.4058        1.1885       0.4181      0.3584        1.2412     +  0.0010  0.0963
     17      0.4588        1.1557       0.4314      0.3873        1.1864     +  0.0010  0.1811
     18      0.4995        1.1236       0.4561      0.4032        1.1542     +  0.0010  0.0940
     19      0.5262        1.0911       0.5298      0.4898        1.1012     +  0.0010  0.0934
     20      0.5531        1.0560       0.5187      0.4770        1.1129        0.0010  0.0892
     21      0.5907        1.0218       0.5704      0.5430        1.0544     +  0.0010  0.0952
     22      0.6102        0.9857       0.5771      0.5503        1.0365     +  0.0010  0.1190
     23      0.6326        0.9500       0.5825      0.5526        1.0288     +  0.0010  0.0949
     24      0.6500        0.9163       0.6303      0.6050        0.9144     +  0.0010  0.0923
     25      0.6669        0.8851       0.6132      0.5903        0.9737        0.0010  0.0885
     26      0.6807        0.8516       0.6565      0.6349        0.8852     +  0.0010  0.0883
     27      0.6928        0.8224       0.6493      0.6264        0.8810        0.0010  0.0874
     28      0.6984        0.7977       0.7002      0.6863        0.8057     +  0.0010  0.0890
     29      0.7174        0.7704       0.7146      0.7060        0.8089     +  0.0010  0.0906
     30      0.7223        0.7452       0.7306      0.7199        0.7801     +  0.0010  0.0895
     31      0.7318        0.7258       0.7246      0.7122        0.7462        0.0010  0.0878
     32      0.7375        0.7058       0.7547      0.7439        0.6959     +  0.0010  0.0952
     33      0.7403        0.6903       0.7297      0.7183        0.7159        0.0010  0.1047
     34      0.7494        0.6718       0.7787      0.7716        0.6406     +  0.0010  0.0989
     35      0.7526        0.6622       0.7634      0.7556        0.6515        0.0010  0.0908
     36      0.7569        0.6457       0.7896      0.7844        0.6323     +  0.0010  0.0925
     37      0.7607        0.6353       0.7917      0.7888        0.6161     +  0.0010  0.0997
     38      0.7657        0.6256       0.7974      0.7919        0.5893     +  0.0010  0.1053
     39      0.7633        0.6148       0.7926      0.7881        0.5938        0.0010  0.0948
     40      0.7706        0.6056       0.8019      0.7976        0.5914     +  0.0010  0.1021
     41      0.7766        0.5986       0.8131      0.8109        0.5580     +  0.0010  0.0927
     42      0.7727        0.5923       0.7793      0.7747        0.5991        0.0010  0.0928
     43      0.7800        0.5825       0.7778      0.7733        0.5917        0.0010  0.0925
     44      0.7773        0.5797       0.7845      0.7810        0.5832        0.0010  0.0941
     45      0.7840        0.5685       0.7715      0.7664        0.5952        0.0010  0.1024
     46      0.7858        0.5627       0.7848      0.7803        0.5730        0.0010  0.0993
     47      0.7914        0.5573       0.8061      0.8017        0.5371        0.0010  0.0942
     48      0.7882        0.5559       0.7520      0.7470        0.6484        0.0010  0.1010
     49      0.7871        0.5546       0.8082      0.8075        0.5486        0.0010  0.1012
     50      0.7934        0.5453       0.8311      0.8322        0.5053     +  0.0010  0.1323
     51      0.7927        0.5386       0.7920      0.7884        0.5633        0.0010  0.0994
     52      0.7933        0.5359       0.8046      0.8014        0.5390        0.0010  0.1120
     53      0.7969        0.5309       0.8441      0.8426        0.4813     +  0.0010  0.0946
     54      0.7960        0.5315       0.7875      0.7841        0.5811        0.0010  0.0950
     55      0.7992        0.5264       0.8290      0.8270        0.5009        0.0010  0.1714
     56      0.8052        0.5188       0.8396      0.8391        0.4993        0.0010  0.0932
     57      0.8055        0.5106       0.8016      0.7976        0.5277        0.0010  0.0932
     58      0.8041        0.5129       0.7122      0.7087        0.6774        0.0010  0.0923
     59      0.8099        0.5078       0.8402      0.8381        0.4814        0.0010  0.0927
     60      0.8034        0.5089       0.8441      0.8459        0.4929        0.0010  0.0894
     61      0.8103        0.5022       0.8290      0.8272        0.4952        0.0010  0.0902
     62      0.8079        0.5012       0.7565      0.7534        0.6229        0.0010  0.0897
     63      0.8089        0.4989       0.8299      0.8278        0.4925        0.0010  0.0906
     64      0.8132        0.4968       0.8522      0.8518        0.4572     +  0.0010  0.0901
     65      0.8157        0.4932       0.7875      0.7873        0.5667        0.0010  0.0897
     66      0.8143        0.4896       0.8263      0.8228        0.4857        0.0010  0.0904
     67      0.8189        0.4867       0.8152      0.8132        0.5100        0.0010  0.0900
     68      0.8184        0.4797       0.8257      0.8244        0.4988        0.0010  0.0986
     69      0.8185        0.4813       0.8474      0.8477        0.4474        0.0010  0.0915
     70      0.8224        0.4802       0.8441      0.8431        0.4610        0.0010  0.0896
     71      0.8225        0.4768       0.7890      0.7852        0.5392        0.0010  0.0902
     72      0.8194        0.4781       0.8495      0.8483        0.4389        0.0010  0.0910
     73      0.8198        0.4762       0.8558      0.8553        0.4287     +  0.0010  0.0911
     74      0.8202        0.4749       0.8215      0.8181        0.4901        0.0010  0.0899
     75      0.8247        0.4709       0.8396      0.8380        0.4660        0.0010  0.0897
     76      0.8218        0.4716       0.8471      0.8457        0.4425        0.0010  0.0897
     77      0.8236        0.4738       0.8588      0.8590        0.4275     +  0.0010  0.0918
     78      0.8251        0.4711       0.8468      0.8462        0.4365        0.0010  0.1202
     79      0.8193        0.4714       0.7766      0.7731        0.5634        0.0010  0.0926
     80      0.8205        0.4713       0.8480      0.8480        0.4291        0.0010  0.0892
     81      0.8265        0.4646       0.8603      0.8599        0.4206     +  0.0010  0.0900
     82      0.8278        0.4629       0.8453      0.8426        0.4403        0.0010  0.0912
     83      0.8277        0.4628       0.8585      0.8583        0.4342        0.0010  0.0984
     84      0.8237        0.4612       0.8314      0.8272        0.4556        0.0010  0.0898
     85      0.8331        0.4527       0.8362      0.8368        0.4588        0.0010  0.0897
     86      0.8261        0.4591       0.8435      0.8420        0.4496        0.0010  0.0904
     87      0.8260        0.4583       0.8585      0.8570        0.4249        0.0010  0.0911
     88      0.8311        0.4530       0.8555      0.8543        0.4267        0.0010  0.0902
     89      0.8296        0.4567       0.8573      0.8570        0.4319        0.0010  0.0890
     90      0.8297        0.4509       0.8576      0.8571        0.4201        0.0010  0.0878
     91      0.8299        0.4472       0.8570      0.8566        0.4315        0.0010  0.0900
     92      0.8311        0.4556       0.8585      0.8586        0.4141        0.0010  0.0893
     93      0.8306        0.4510       0.8522      0.8505        0.4311        0.0010  0.0901
     94      0.8309        0.4549       0.8374      0.8370        0.4566        0.0010  0.1686
     95      0.8325        0.4499       0.8531      0.8529        0.4177        0.0010  0.0896
     96      0.8320        0.4484       0.8561      0.8553        0.4169        0.0010  0.0885
     97      0.8339        0.4454       0.8543      0.8534        0.4337        0.0010  0.0989
     98      0.8321        0.4483       0.8558      0.8546        0.4236        0.0010  0.0899
     99      0.8366        0.4455       0.8522      0.8494        0.4298        0.0010  0.0882
    100      0.8350        0.4412       0.8444      0.8437        0.4455        0.0010  0.0889
    101      0.8345        0.4431       0.8561      0.8538        0.4197        0.0010  0.0904
    102      0.8397        0.4409       0.8459      0.8457        0.4309        0.0010  0.0896
    103      0.8363        0.4408       0.8561      0.8561        0.4098        0.0010  0.0892
    104      0.8371        0.4391       0.8594      0.8575        0.4159        0.0010  0.0905
    105      0.8357        0.4423       0.8299      0.8278        0.4690        0.0010  0.0922
    106      0.8324        0.4456       0.8525      0.8540        0.4273        0.0010  0.0892
    107      0.8393        0.4440       0.8411      0.8387        0.4411        0.0010  0.0912
    108      0.8416        0.4322       0.8582      0.8577        0.4031        0.0005  0.0895
    109      0.8393        0.4345       0.8591      0.8589        0.4177        0.0005  0.0908
    110      0.8388        0.4356       0.8600      0.8603        0.4036        0.0005  0.0982
    111      0.8365        0.4356       0.8624      0.8618        0.4018     +  0.0005  0.0882
    112      0.8421        0.4347       0.8528      0.8512        0.4127        0.0005  0.0888
    113      0.8385        0.4365       0.8657      0.8650        0.4014     +  0.0005  0.0903
    114      0.8409        0.4335       0.8609      0.8602        0.4042        0.0005  0.0897
    115      0.8379        0.4378       0.8630      0.8626        0.4030        0.0005  0.0915
    116      0.8398        0.4387       0.8642      0.8636        0.4019        0.0005  0.0907
    117      0.8408        0.4303       0.8615      0.8605        0.4056        0.0005  0.0893
    118      0.8430        0.4327       0.8633      0.8626        0.4031        0.0005  0.0907
    119      0.8409        0.4381       0.8627      0.8623        0.4068        0.0005  0.0901
    120      0.8364        0.4372       0.8630      0.8631        0.4007        0.0005  0.0902
    121      0.8443        0.4275       0.8669      0.8659        0.4011     +  0.0005  0.0914
    122      0.8444        0.4261       0.8504      0.8493        0.4140        0.0005  0.0900
    123      0.8398        0.4330       0.8639      0.8634        0.3996        0.0005  0.0903
    124      0.8403        0.4252       0.8639      0.8629        0.4059        0.0005  0.0901
    125      0.8456        0.4279       0.8624      0.8613        0.4053        0.0005  0.0890
    126      0.8398        0.4333       0.8636      0.8632        0.4020        0.0005  0.0984
    127      0.8453        0.4278       0.8624      0.8615        0.4023        0.0005  0.0894
    128      0.8415        0.4291       0.8621      0.8619        0.3996        0.0005  0.0877
    129      0.8429        0.4274       0.8672      0.8669        0.4000     +  0.0005  0.0890
    130      0.8383        0.4308       0.8519      0.8512        0.4082        0.0005  0.0878
    131      0.8391        0.4327       0.8648      0.8646        0.3964        0.0005  0.0895
    132      0.8476        0.4202       0.8639      0.8630        0.4024        0.0005  0.1677
    133      0.8425        0.4304       0.8510      0.8509        0.4122        0.0005  0.0886
    134      0.8423        0.4302       0.8600      0.8582        0.4060        0.0005  0.1195
    135      0.8472        0.4246       0.8540      0.8539        0.4039        0.0005  0.0907
    136      0.8400        0.4318       0.8573      0.8557        0.4089        0.0005  0.0894
    137      0.8444        0.4241       0.8588      0.8585        0.4031        0.0005  0.0905
    138      0.8436        0.4280       0.8633      0.8627        0.4064        0.0005  0.0915
    139      0.8439        0.4249       0.8648      0.8647        0.4040        0.0005  0.0897
    140      0.8427        0.4252       0.8549      0.8550        0.4043        0.0005  0.0970
    141      0.8413        0.4277       0.8685      0.8676        0.3972     +  0.0005  0.0897
    142      0.8441        0.4255       0.8603      0.8594        0.3996        0.0005  0.0896
    143      0.8450        0.4259       0.8600      0.8586        0.4060        0.0005  0.0889
    144      0.8471        0.4225       0.8630      0.8632        0.4018        0.0005  0.0895
    145      0.8437        0.4248       0.8645      0.8639        0.4011        0.0005  0.0890
    146      0.8446        0.4311       0.8564      0.8555        0.4023        0.0005  0.0891
    147      0.8442        0.4215       0.8618      0.8606        0.4000        0.0005  0.0899
    148      0.8434        0.4224       0.8615      0.8602        0.4031        0.0005  0.0895
    149      0.8479        0.4223       0.8633      0.8635        0.3998        0.0005  0.0889
    150      0.8445        0.4247       0.8579      0.8572        0.4042        0.0005  0.0897
    151      0.8474        0.4176       0.8666      0.8665        0.3968        0.0005  0.0896
    152      0.8429        0.4246       0.8651      0.8638        0.4012        0.0005  0.0895
    153      0.8455        0.4218       0.8636      0.8629        0.3969        0.0005  0.0890
    154      0.8474        0.4197       0.8609      0.8608        0.3976        0.0005  0.0900
    155      0.8467        0.4200       0.8585      0.8580        0.3978        0.0005  0.0982
    156      0.8426        0.4227       0.8645      0.8639        0.4025        0.0005  0.0882
    157      0.8452        0.4204       0.8669      0.8664        0.3968        0.0005  0.0905
    158      0.8444        0.4203       0.8648      0.8642        0.3954        0.0005  0.0887
    159      0.8472        0.4176       0.8639      0.8629        0.4029        0.0005  0.0882
    160      0.8449        0.4207       0.8537      0.8542        0.4067        0.0005  0.0895
    161      0.8424        0.4233       0.8609      0.8596        0.4021        0.0005  0.0896
    162      0.8460        0.4202       0.8573      0.8575        0.4034        0.0005  0.0894
    163      0.8463        0.4206       0.8648      0.8636        0.3980        0.0005  0.0898
    164      0.8464        0.4178       0.8642      0.8635        0.3953        0.0005  0.0878
    165      0.8452        0.4212       0.8597      0.8579        0.4102        0.0005  0.0886
    166      0.8448        0.4229       0.8561      0.8563        0.4064        0.0005  0.0903
    167      0.8452        0.4237       0.8645      0.8634        0.3955        0.0005  0.0882
    168      0.8504        0.4173       0.8666      0.8661        0.3953        0.0003  0.0902
    169      0.8449        0.4223       0.8700      0.8692        0.3942     +  0.0003  0.0885
    170      0.8502        0.4193       0.8669      0.8657        0.3999        0.0003  0.0891
    171      0.8455        0.4188       0.8594      0.8589        0.3959        0.0003  0.1734
    172      0.8462        0.4180       0.8694      0.8688        0.3924        0.0003  0.0899
    173      0.8468        0.4198       0.8648      0.8645        0.3937        0.0003  0.0897
    174      0.8475        0.4159       0.8588      0.8586        0.3965        0.0003  0.0896
    175      0.8480        0.4169       0.8588      0.8581        0.3975        0.0003  0.0900
    176      0.8446        0.4200       0.8672      0.8668        0.3921        0.0003  0.0907
    177      0.8493        0.4131       0.8675      0.8670        0.3947        0.0003  0.0931
    178      0.8469        0.4203       0.8657      0.8653        0.3933        0.0003  0.0923
    179      0.8474        0.4154       0.8645      0.8641        0.3937        0.0003  0.0897
    180      0.8502        0.4152       0.8669      0.8660        0.3951        0.0003  0.0909
    181      0.8450        0.4201       0.8639      0.8635        0.3936        0.0003  0.0908
    182      0.8513        0.4137       0.8639      0.8632        0.3928        0.0003  0.0922
    183      0.8483        0.4181       0.8642      0.8636        0.3924        0.0003  0.0913
    184      0.8464        0.4197       0.8682      0.8671        0.3922        0.0003  0.0914
    185      0.8463        0.4178       0.8675      0.8664        0.3966        0.0003  0.0903
    186      0.8463        0.4171       0.8621      0.8615        0.3939        0.0003  0.0898
    187      0.8506        0.4158       0.8669      0.8667        0.3905        0.0003  0.0996
    188      0.8491        0.4147       0.8672      0.8667        0.3914        0.0003  0.1182
    189      0.8458        0.4181       0.8648      0.8649        0.3930        0.0003  0.0896
    190      0.8453        0.4245       0.8663      0.8660        0.3905        0.0003  0.0888
    191      0.8471        0.4208       0.8669      0.8664        0.3924        0.0003  0.0902
    192      0.8531        0.4168       0.8648      0.8646        0.3930        0.0003  0.0902
    193      0.8473        0.4175       0.8642      0.8640        0.3911        0.0003  0.0911
    194      0.8510        0.4142       0.8660      0.8650        0.3933        0.0003  0.0902
    195      0.8469        0.4221       0.8639      0.8628        0.3951        0.0003  0.0881
    196      0.8498        0.4110       0.8642      0.8635        0.3919        0.0001  0.0891
    197      0.8479        0.4191       0.8654      0.8647        0.3916        0.0001  0.0910
    198      0.8472        0.4206       0.8654      0.8646        0.3910        0.0001  0.0918
    199      0.8509        0.4122       0.8688      0.8679        0.3925        0.0001  0.0899
    200      0.8488        0.4134       0.8691      0.8684        0.3914        0.0001  0.0903
    201      0.8479        0.4140       0.8660      0.8653        0.3915        0.0001  0.0982
    202      0.8483        0.4155       0.8624      0.8617        0.3935        0.0001  0.0894
    203      0.8492        0.4140       0.8642      0.8634        0.3917        0.0001  0.0893
    204      0.8526        0.4140       0.8660      0.8650        0.3920        0.0001  0.0914
    205      0.8487        0.4143       0.8648      0.8641        0.3917        0.0001  0.0914
    206      0.8480        0.4141       0.8669      0.8663        0.3902        0.0001  0.0902
    207      0.8524        0.4115       0.8697      0.8690        0.3899        0.0001  0.0904
    208      0.8481        0.4210       0.8685      0.8679        0.3903        0.0001  0.0892
    209      0.8515        0.4084       0.8666      0.8660        0.3913        0.0001  0.1671
    210      0.8504        0.4148       0.8675      0.8669        0.3906        0.0001  0.0909
    211      0.8472        0.4135       0.8682      0.8673        0.3915        0.0001  0.0909
    212      0.8481        0.4152       0.8694      0.8687        0.3904        0.0001  0.0907
    213      0.8484        0.4141       0.8666      0.8659        0.3905        0.0001  0.0892
    214      0.8495        0.4171       0.8657      0.8651        0.3916        0.0001  0.0903
    215      0.8487        0.4166       0.8654      0.8648        0.3917        0.0001  0.0893
    216      0.8476        0.4145       0.8657      0.8650        0.3914        0.0001  0.0973
    217      0.8508        0.4133       0.8682      0.8674        0.3915        0.0001  0.0910
    218      0.8487        0.4156       0.8688      0.8682        0.3900        0.0001  0.0900
Stopping since valid_f1 has not improved in the last 50 epochs.
Out[38]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Dropout(p=0.2, inplace=False)
      (4): Linear(in_features=32, out_features=16, bias=True)
      (5): ReLU()
      (6): Linear(in_features=16, out_features=7, bias=True)
      (7): Softmax(dim=-1)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=22, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Dropout(p=0.2, inplace=False)
      (4): Linear(in_features=32, out_features=16, bias=True)
      (5): ReLU()
      (6): Linear(in_features=16, out_features=7, bias=True)
      (7): Softmax(dim=-1)
    )
  ),
)
In [39]:
y_pred = net.predict(X_test)
f1_score(y_test, y_pred, average="weighted")
Out[39]:
0.8739429187585216
In [40]:
history = net.history

plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
ax1 = plt.gca()  # get current axes
ax2 = plt.gca().twinx()  # create a second y-axis
plt.plot(history[:, "event_lr"], label='learning_rate', color='gray', linestyle='--')
ax2.set_ylabel('Learning Rate')

ax1.set_ylabel('Loss')

for i, checkpoint in enumerate(history[:, "event_cp"][::-1]):
    if checkpoint:
        ax1.axvline(x=len(history[:, "event_cp"])-i-1, color='red', 
                    linestyle='-.', label='Checkpoint', alpha=0.5)
        break

ax1.legend(loc='lower left')

plt.xlabel('Epoch')
plt.show()

plt.plot(history[:, 'train_f1'], label='train_f1')
plt.plot(history[:, 'valid_f1'], label='valid_f1')

plt.xlabel('Epoch')
plt.ylabel('F1')

plt.legend()

plt.show()
No description has been provided for this image
No description has been provided for this image

Regression¶

In [41]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score
import torch
from torch import nn
import torch.nn.functional as F
from skorch import NeuralNetClassifier
from skorch.callbacks import EpochScoring
from sklearn.metrics import mean_absolute_error
from skorch import NeuralNetRegressor
In [42]:
target = "Age"
y = df[target].astype(np.float32).values
X = df.drop(columns=[target, "id"])
In [43]:
# Convert categorical variables to numeric using one-hot encoding

X[X.select_dtypes('object').columns] = X.select_dtypes('object').apply(pd.Categorical)
X = pd.get_dummies(X, drop_first=True).astype(np.float32).values
In [44]:
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
In [45]:
from sklearn.dummy import DummyRegressor

model = DummyRegressor(strategy="mean")
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mean_absolute_error(y_test, y_pred)
Out[45]:
4.175730228424072
In [46]:
from sklearn.neural_network import MLPRegressor

model = MLPRegressor(
    hidden_layer_sizes=(32,),  
    solver="adam",  
    alpha=0.001, 
    max_iter=500,  
    random_state=42,  
    batch_size=2048,  
    shuffle=True,
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mean_absolute_error(y_test, y_pred)
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.
  warnings.warn(
Out[46]:
2.272099018096924
In [47]:
class BetterMLP(nn.Module):
    def __init__(self, in_features, out_features):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(in_features, 32),
            nn.BatchNorm1d(32),  # batch normalization layer
            nn.ReLU(),
            nn.Linear(32, out_features),
            # no activation at the end since this is regression
        )

    def forward(self, X, **kwargs):
        return self.model(X)
In [48]:
from skorch.dataset import ValidSplit
from skorch.callbacks import LRScheduler, EarlyStopping, Checkpoint
from torch.optim.lr_scheduler import ReduceLROnPlateau


net = NeuralNetRegressor(
    BetterMLP,
    module__in_features=X_train.shape[1],
    module__out_features=1,
    max_epochs=500,
    
    lr = 1e-3,
    device=DEVICE,
    optimizer=torch.optim.AdamW,  # using AdamW instead of Adam for better regularization
    criterion=nn.MSELoss,  # using MSE loss for regression

    callbacks=[
        EpochScoring(
            mean_absolute_error,
            name="valid_mae",
            lower_is_better=True,
            on_train=False,
        ),
        EpochScoring(
            mean_absolute_error,
            name="train_mae",
            lower_is_better=True,
            on_train=True,
        ),
        LRScheduler(
            policy=ReduceLROnPlateau,
            monitor="valid_mae",
            mode="min",
            factor=0.5,
            patience=25,
            threshold=1e-4,
            min_lr=1e-6,
            verbose=True,
        ),
        EarlyStopping(
            monitor="valid_mae",
            lower_is_better=True,
            patience=40,
            load_best=True,
        ),
        Checkpoint(
            monitor="valid_mae_best",
            load_best=True,
            f_history=None,
            f_optimizer=None,
        ),
    ],

    batch_size=2048,
    iterator_train__shuffle=True,
)
net
Out[48]:
<class 'skorch.regressor.NeuralNetRegressor'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=27,
  module__out_features=1,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.regressor.NeuralNetRegressor'>[uninitialized](
  module=<class '__main__.BetterMLP'>,
  module__in_features=27,
  module__out_features=1,
)
In [49]:
# Training the network
net.fit(X_train, y_train[:, None])
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/torch/optim/lr_scheduler.py:28: UserWarning: The verbose parameter is deprecated. Please use get_last_lr() to access the learning rate.
  warnings.warn("The verbose parameter is deprecated. Please use get_last_lr() "
  epoch    train_loss    train_mae    valid_loss    valid_mae    cp      lr     dur
-------  ------------  -----------  ------------  -----------  ----  ------  ------
      1      615.6085      24.1479      620.8583      24.2237     +  0.0010  0.7635
      2      609.6344      24.0249      594.3533      23.6981     +  0.0010  0.0959
      3      603.7377      23.9016      584.2501      23.4937     +  0.0010  0.0958
      4      597.9963      23.7810      582.1346      23.4469     +  0.0010  0.0967
      5      592.3411      23.6611      583.7179      23.4732        0.0010  0.1025
      6      586.7383      23.5401      585.3101      23.4981        0.0010  0.1052
      7      581.1118      23.4179      581.9609      23.4199     +  0.0010  0.1372
      8      575.3965      23.2951      577.8797      23.3286     +  0.0010  0.0998
      9      569.5366      23.1720      566.8957      23.0928     +  0.0010  0.1016
     10      563.5585      23.0449      554.6788      22.8320     +  0.0010  0.1034
     11      557.5785      22.9171      557.9930      22.8983        0.0010  0.1123
     12      551.2931      22.7883      555.6525      22.8413        0.0010  0.1007
     13      544.8574      22.6553      544.7441      22.6148     +  0.0010  0.0988
     14      538.0173      22.5182      528.9284      22.2732     +  0.0010  0.1278
     15      531.0705      22.3894      515.9805      21.9958     +  0.0010  0.1047
     16      523.9074      22.2379      502.9861      21.7242     +  0.0010  0.1934
     17      516.4339      22.0936      476.4068      21.1286     +  0.0010  0.1012
     18      508.8763      21.9524      492.5345      21.5311        0.0010  0.1002
     19      501.1751      21.7883      503.3328      21.7922        0.0010  0.1001
     20      493.2712      21.6231      492.8367      21.5734        0.0010  0.1050
     21      485.1305      21.4574      490.8459      21.5350        0.0010  0.1486
     22      476.7567      21.2818      490.0673      21.5339        0.0010  0.1028
     23      468.1747      21.0929      477.2153      21.2477        0.0010  0.1028
     24      459.3939      20.9044      453.6153      20.7284     +  0.0010  0.1011
     25      450.3256      20.7047      440.3734      20.4285     +  0.0010  0.0989
     26      441.1153      20.5007      435.4073      20.3271     +  0.0010  0.0982
     27      431.8324      20.2846      438.4441      20.4027        0.0010  0.0972
     28      422.4637      20.0646      419.2206      19.9451     +  0.0010  0.1118
     29      413.0077      19.8392      418.4272      19.9355     +  0.0010  0.1058
     30      403.4338      19.6067      416.7641      19.8898     +  0.0010  0.1002
     31      393.6827      19.3667      400.5614      19.5050     +  0.0010  0.1018
     32      383.8090      19.1158      394.2751      19.3527     +  0.0010  0.0994
     33      373.7780      18.8614      368.6050      18.6858     +  0.0010  0.1022
     34      363.7001      18.6007      356.5817      18.3663     +  0.0010  0.1127
     35      353.6262      18.3346      357.5906      18.3751        0.0010  0.1061
     36      343.5071      18.0691      347.4042      18.1160     +  0.0010  0.0995
     37      333.4587      17.7955      343.3687      18.0017     +  0.0010  0.1271
     38      323.3497      17.5192      332.8711      17.7251     +  0.0010  0.1073
     39      313.2658      17.2363      338.4815      17.8486        0.0010  0.1129
     40      303.2852      16.9536      311.5698      17.1403     +  0.0010  0.1156
     41      293.3487      16.6645      290.5961      16.5457     +  0.0010  0.0986
     42      283.4980      16.3709      289.1552      16.4901     +  0.0010  0.1816
     43      273.7227      16.0756      278.2753      16.1927     +  0.0010  0.1102
     44      264.0995      15.7783      247.7530      15.2263     +  0.0010  0.1061
     45      254.5279      15.4759      242.6402      15.0534     +  0.0010  0.1025
     46      245.0704      15.1733      249.7249      15.3108        0.0010  0.1076
     47      235.6129      14.8630      221.6600      14.3668     +  0.0010  0.1025
     48      226.3223      14.5489      203.6025      13.7316     +  0.0010  0.0997
     49      217.1373      14.2352      200.6701      13.6246     +  0.0010  0.1244
     50      208.1794      13.9182      205.3880      13.8112        0.0010  0.1063
     51      199.3254      13.6014      193.3978      13.3722     +  0.0010  0.1097
     52      190.5778      13.2789      179.9962      12.8689     +  0.0010  0.1088
     53      182.0794      12.9580      183.8069      13.0041        0.0010  0.1020
     54      173.7490      12.6366      175.0394      12.6347     +  0.0010  0.1019
     55      165.7041      12.3157      173.6547      12.6299     +  0.0010  0.0989
     56      157.8427      11.9969      157.0681      11.9408     +  0.0010  0.0978
     57      150.2190      11.6757      172.7212      12.5955        0.0010  0.0986
     58      142.8233      11.3602      159.1385      12.0206        0.0010  0.0987
     59      135.6361      11.0460      142.8611      11.3420     +  0.0010  0.0980
     60      128.7858      10.7330      140.0285      11.2383     +  0.0010  0.0980
     61      122.0661      10.4216      130.1695      10.7868     +  0.0010  0.0982
     62      115.6664      10.1191      114.6827      10.0530     +  0.0010  0.0971
     63      109.5891       9.8123      161.0770      12.0380        0.0010  0.0997
     64      103.6504       9.5141      132.7724      10.8983        0.0010  0.0983
     65       97.8957       9.2143      112.9393       9.9443     +  0.0010  0.1067
     66       92.4600       8.9230       87.7266       8.6260     +  0.0010  0.0977
     67       87.3025       8.6312       92.8610       8.9661        0.0010  0.0960
     68       82.3165       8.3483       72.5451       7.7481     +  0.0010  0.1762
     69       77.4450       8.0623       63.0638       7.1091     +  0.0010  0.0964
     70       72.9954       7.7904       47.7499       6.0034     +  0.0010  0.0985
     71       68.7289       7.5203       46.2908       5.8458     +  0.0010  0.0980
     72       64.7365       7.2526       48.5953       6.0177        0.0010  0.0979
     73       60.9653       6.9960       71.1719       7.7048        0.0010  0.0989
     74       57.2131       6.7405       74.8591       7.8285        0.0010  0.0968
     75       53.9756       6.5011       83.8327       8.3164        0.0010  0.0988
     76       50.7223       6.2654       53.1948       6.4641        0.0010  0.0986
     77       47.5298       6.0291       42.4083       5.6492     +  0.0010  0.0999
     78       44.4640       5.8015       34.7861       4.9674     +  0.0010  0.1568
     79       41.8125       5.5790       31.2314       4.5917     +  0.0010  0.1015
     80       39.2421       5.3651       37.4587       5.1288        0.0010  0.1008
     81       36.9390       5.1664       21.9634       3.6475     +  0.0010  0.0986
     82       34.6786       4.9686       18.3454       3.1985     +  0.0010  0.0993
     83       32.8448       4.7937       18.3207       3.1803     +  0.0010  0.0974
     84       30.9007       4.6095       20.1405       3.3134        0.0010  0.0968
     85       29.2056       4.4354       21.7719       3.5636        0.0010  0.0972
     86       27.5839       4.2816       20.9368       3.4854        0.0010  0.0968
     87       26.0905       4.1299       28.9042       4.4373        0.0010  0.1009
     88       24.6701       3.9742       27.1625       4.2657        0.0010  0.1055
     89       23.3916       3.8412       39.4685       5.3296        0.0010  0.1012
     90       22.3035       3.7193       27.7205       4.2721        0.0010  0.0972
     91       21.2619       3.6063       29.0469       4.4439        0.0010  0.0979
     92       20.1868       3.4866       24.3693       3.9239        0.0010  0.1057
     93       19.3472       3.3668       15.9236       2.9404     +  0.0010  0.0982
     94       18.5393       3.2793       26.9245       4.1964        0.0010  0.1781
     95       17.8448       3.1980       26.7227       4.1912        0.0010  0.0982
     96       17.1511       3.1040       13.7885       2.6586     +  0.0010  0.0953
     97       16.5009       3.0160       16.5534       3.0281        0.0010  0.0972
     98       15.9473       2.9467       21.0993       3.5798        0.0010  0.0980
     99       15.5190       2.8937       26.9774       4.1806        0.0010  0.0967
    100       15.0258       2.8135       12.6611       2.4495     +  0.0010  0.0972
    101       14.5719       2.7478       20.8927       3.5404        0.0010  0.0961
    102       14.3742       2.7155       29.4341       4.4394        0.0010  0.0969
    103       13.9830       2.6591       47.4778       5.8598        0.0010  0.0975
    104       13.8089       2.6272       22.7402       3.6499        0.0010  0.0981
    105       13.5517       2.6086       15.6046       2.7635        0.0010  0.0981
    106       13.0961       2.5259       12.8392       2.4594        0.0010  0.0984
    107       12.8343       2.4917       11.4291       2.2354     +  0.0010  0.1044
    108       12.4338       2.4204       13.0088       2.5335        0.0010  0.0982
    109       12.2874       2.3946       19.3264       3.3196        0.0010  0.0971
    110       12.1138       2.3691       11.6018       2.2473        0.0010  0.0974
    111       12.0251       2.3638       17.5865       3.1926        0.0010  0.0972
    112       11.8808       2.3140       11.9010       2.3256        0.0010  0.0970
    113       11.7689       2.3023       20.0774       3.4022        0.0010  0.1008
    114       11.6270       2.2998       16.3630       2.8137        0.0010  0.0967
    115       11.4558       2.2411       22.8836       3.7181        0.0010  0.0985
    116       11.4614       2.2522       11.5658       2.3253        0.0010  0.0974
    117       11.3422       2.2258       11.2492       2.1832     +  0.0010  0.0974
    118       11.2104       2.2154       11.0497       2.1661     +  0.0010  0.0986
    119       11.1275       2.1805       11.4183       2.2102        0.0010  0.1220
    120       11.0619       2.1749       10.9135       2.1287     +  0.0010  0.0969
    121       11.0731       2.1914       10.9170       2.1291        0.0010  0.1768
    122       11.0093       2.1494       14.0311       2.7625        0.0010  0.0979
    123       10.9927       2.1789       11.1363       2.2367        0.0010  0.1071
    124       10.9019       2.1455       13.7135       2.4961        0.0010  0.0992
    125       10.8556       2.1473       11.6349       2.2285        0.0010  0.0972
    126       10.8413       2.1387       14.6112       2.7877        0.0010  0.0972
    127       10.8254       2.1354       13.2510       2.6168        0.0010  0.0970
    128       10.9000       2.1733       12.9842       2.3663        0.0010  0.0985
    129       10.8152       2.1253       18.7111       3.1951        0.0010  0.0976
    130       10.7504       2.1261       18.5230       3.2343        0.0010  0.0979
    131       10.7674       2.1355       15.9319       2.9066        0.0010  0.0960
    132       10.7451       2.1299       11.9354       2.2964        0.0010  0.0986
    133       10.6533       2.1341       12.3813       2.3897        0.0010  0.1002
    134       10.9910       2.1734       16.5868       3.1522        0.0010  0.0975
    135       10.7916       2.1382       11.0472       2.2028        0.0010  0.0983
    136       10.5851       2.1182       11.0833       2.1458        0.0010  0.0983
    137       10.6278       2.1097       10.6915       2.1014     +  0.0010  0.1050
    138       10.6092       2.1031       13.5398       2.6567        0.0010  0.0985
    139       10.5511       2.0842       11.3438       2.2877        0.0010  0.0964
    140       10.5511       2.0964       11.7257       2.2922        0.0010  0.0969
    141       10.5219       2.0968       15.4311       2.8124        0.0010  0.0977
    142       10.5396       2.0941       13.7698       2.5947        0.0010  0.0977
    143       10.5058       2.0898       17.9566       3.1381        0.0010  0.0976
    144       10.4883       2.0809       11.2515       2.2494        0.0010  0.0994
    145       10.5158       2.1023       14.7135       2.6793        0.0010  0.0967
    146       10.4078       2.0806       12.2493       2.3467        0.0010  0.0970
    147       10.3714       2.0606       11.9672       2.3063        0.0010  0.1768
    148       10.4436       2.0819       10.8371       2.1445        0.0010  0.0984
    149       10.4166       2.0812       12.1619       2.4191        0.0010  0.0965
    150       10.5072       2.0944       12.3933       2.2661        0.0010  0.0975
    151       10.4302       2.0820       10.6376       2.0709     +  0.0010  0.0978
    152       10.3692       2.0560       13.0043       2.4951        0.0010  0.0960
    153       10.3992       2.0768       11.6122       2.2322        0.0010  0.0979
    154       10.5164       2.0875       15.8421       2.8443        0.0010  0.0965
    155       10.5096       2.0917       16.9917       3.1570        0.0010  0.1072
    156       10.4363       2.0992       11.5760       2.3513        0.0010  0.0976
    157       10.4430       2.0747       13.0481       2.3995        0.0010  0.0967
    158       10.4114       2.0881       11.4282       2.3227        0.0010  0.0972
    159       10.3397       2.0652       13.9916       2.6246        0.0010  0.0968
    160       10.3568       2.0654       10.7009       2.0894        0.0010  0.0972
    161       10.3551       2.0874       11.2556       2.2864        0.0010  0.0962
    162       10.3279       2.0604       11.4774       2.3197        0.0010  0.0978
    163       10.2556       2.0597       10.6614       2.0671     +  0.0010  0.0977
    164       10.3278       2.0691       17.9811       3.1131        0.0010  0.0968
    165       10.3651       2.0652       13.1818       2.6077        0.0010  0.0978
    166       10.2216       2.0542       11.2597       2.2906        0.0010  0.0960
    167       10.3857       2.0945       24.9576       4.0110        0.0010  0.0976
    168       10.4270       2.0713       17.0359       3.1897        0.0010  0.0979
    169       10.3028       2.0664       11.6389       2.3148        0.0010  0.0963
    170       10.2629       2.0591       11.5156       2.3158        0.0010  0.0969
    171       10.2682       2.0553       10.5681       2.0708        0.0010  0.1053
    172       10.2060       2.0289       12.4935       2.5328        0.0010  0.1235
    173       10.2312       2.0472       10.6320       2.0809        0.0010  0.1755
    174       10.2038       2.0410       12.7901       2.3138        0.0010  0.0970
    175       10.2220       2.0375       16.7133       2.9997        0.0010  0.0967
    176       10.2629       2.0649       12.2742       2.4031        0.0010  0.0962
    177       10.2818       2.0660       11.1812       2.2602        0.0010  0.0957
    178       10.1738       2.0388       13.2913       2.6674        0.0010  0.0972
    179       10.1454       2.0332       10.5103       2.0506     +  0.0010  0.0967
    180       10.1075       2.0229       10.9831       2.1108        0.0010  0.0985
    181       10.1103       2.0286       12.9929       2.5500        0.0010  0.0987
    182       10.2273       2.0563       14.4476       2.7819        0.0010  0.0984
    183       10.3006       2.0489       11.1331       2.1658        0.0010  0.0982
    184       10.2019       2.0501       13.2543       2.6810        0.0010  0.0967
    185       10.2433       2.0600       19.0089       3.3647        0.0010  0.0972
    186       10.2751       2.0513       24.7641       3.9840        0.0010  0.0960
    187       10.3142       2.0698       12.5326       2.4930        0.0010  0.1051
    188       10.1317       2.0252       13.8861       2.7852        0.0010  0.0981
    189       10.2103       2.0570       10.9316       2.1724        0.0010  0.0964
    190       10.1055       2.0246       10.9014       2.1548        0.0010  0.0992
    191       10.1341       2.0368       12.3572       2.5084        0.0010  0.0969
    192       10.0761       2.0291       11.1435       2.2622        0.0010  0.0985
    193       10.0840       2.0187       10.7761       2.1510        0.0010  0.0962
    194       10.1373       2.0428       15.5093       2.9806        0.0010  0.0974
    195       10.1754       2.0451       14.3928       2.8143        0.0010  0.0983
    196       10.1540       2.0443       11.6802       2.3649        0.0010  0.0983
    197       10.3802       2.0922       10.7879       2.1506        0.0010  0.0954
    198       10.3887       2.0830       14.1898       2.5277        0.0010  0.0974
    199       10.3347       2.0836       25.6714       3.9037        0.0010  0.1752
    200       10.3367       2.0789       10.5787       2.1010        0.0010  0.0965
    201       10.0463       2.0189       11.2732       2.1099        0.0010  0.0963
    202       10.0429       2.0123       12.6029       2.5336        0.0010  0.0974
    203       10.0345       2.0180       12.3066       2.3730        0.0010  0.1035
    204       10.0725       2.0198       18.0473       3.1458        0.0010  0.0959
    205       10.2650       2.0609       15.9492       2.9051        0.0010  0.0970
    206       10.0590       2.0343       12.7779       2.5979        0.0005  0.0971
    207       10.1405       2.0518       11.1314       2.1864        0.0005  0.0968
    208       10.0398       2.0120       12.6483       2.4137        0.0005  0.0969
    209       10.0545       2.0259       14.7432       2.9243        0.0005  0.0967
    210       10.0426       2.0122       16.5693       2.9820        0.0005  0.1022
    211       10.0309       2.0212       12.4197       2.5236        0.0005  0.1066
    212       10.0322       2.0247       10.3571       2.0170     +  0.0005  0.1052
    213       10.0299       2.0145       10.4687       2.0372        0.0005  0.1016
    214       10.0551       2.0213       11.4687       2.2574        0.0005  0.1071
    215        9.9964       2.0116       10.9590       2.2061        0.0005  0.1101
    216       10.0055       2.0206       10.5415       2.0987        0.0005  0.1383
    217       10.0039       2.0101       10.5187       2.0707        0.0005  0.1073
    218        9.9858       2.0041       10.7047       2.0782        0.0005  0.1079
    219        9.9855       2.0065       10.5367       2.0725        0.0005  0.1008
    220       10.0492       2.0273       13.8437       2.7778        0.0005  0.1014
    221       10.0196       2.0120       16.6357       2.9922        0.0005  0.1132
    222       10.0316       2.0194       11.8387       2.4227        0.0005  0.1078
    223       10.0487       2.0218       10.9232       2.1662        0.0005  0.1083
    224        9.9952       2.0089       11.3090       2.1821        0.0005  0.1089
    225       10.0200       2.0206       11.8820       2.4094        0.0005  0.1841
    226        9.9997       2.0097       11.0449       2.1328        0.0005  0.0993
    227        9.9902       2.0025       10.9042       2.1621        0.0005  0.1005
    228        9.9940       2.0135       10.5860       2.0957        0.0005  0.0993
    229        9.9460       1.9948       13.8859       2.6220        0.0005  0.1006
    230        9.9939       2.0063       11.1663       2.2734        0.0005  0.0989
    231        9.9522       2.0027       10.6127       2.1194        0.0005  0.0959
    232       10.0346       2.0286       10.4554       2.0537        0.0005  0.0973
    233        9.9554       2.0006       10.8173       2.1557        0.0005  0.0972
    234        9.9768       2.0126       13.3576       2.6772        0.0005  0.0959
    235       10.0369       2.0209       11.3771       2.2567        0.0005  0.1049
    236        9.9747       2.0062       10.3786       2.0097     +  0.0005  0.0980
    237        9.9411       1.9977       10.3196       2.0196        0.0005  0.0997
    238        9.9379       2.0035       10.4509       2.0644        0.0005  0.0985
    239        9.9418       1.9980       10.6072       2.1213        0.0005  0.1211
    240        9.9556       1.9989       11.2521       2.1921        0.0005  0.0986
    241        9.9709       2.0029       10.8331       2.1209        0.0005  0.0979
    242        9.9977       2.0206       11.6271       2.3703        0.0005  0.0961
    243        9.9816       2.0051       11.6028       2.2476        0.0005  0.0959
    244        9.9832       2.0114       10.6162       2.0818        0.0005  0.0969
    245        9.9652       2.0129       10.4408       2.0606        0.0005  0.0969
    246        9.9912       2.0222       14.7667       2.9274        0.0005  0.0983
    247        9.9915       2.0144       12.7221       2.4374        0.0005  0.0969
    248       10.0175       2.0363       10.8693       2.1885        0.0005  0.0985
    249        9.9691       2.0083       11.6561       2.3021        0.0005  0.0990
    250       10.0141       2.0202       10.4279       2.0265        0.0005  0.1063
    251        9.9495       2.0056       10.3374       2.0439        0.0005  0.1774
    252       10.0045       2.0258       13.8486       2.7867        0.0005  0.0976
    253       10.0667       2.0253       15.0957       2.7715        0.0005  0.0982
    254       10.0643       2.0397       11.3925       2.3188        0.0005  0.0974
    255        9.9743       2.0147       13.3847       2.5553        0.0005  0.0979
    256        9.9358       1.9938       10.3613       2.0266        0.0005  0.0974
    257        9.9704       2.0093       10.9700       2.2140        0.0005  0.0972
    258        9.9994       2.0231       10.8503       2.1709        0.0005  0.0973
    259        9.9250       2.0016       10.9251       2.0872        0.0005  0.0969
    260        9.9697       2.0112       11.6003       2.3092        0.0005  0.0968
    261       10.0126       2.0142       12.0197       2.2593        0.0005  0.0977
    262        9.9637       2.0096       10.6802       2.1333        0.0005  0.0973
    263        9.9887       2.0218       10.4781       2.0542        0.0003  0.0979
    264        9.9253       2.0017       10.6417       2.1334        0.0003  0.0967
    265        9.9038       1.9868       10.5920       2.1144        0.0003  0.0967
    266        9.9451       2.0056       10.3308       2.0236        0.0003  0.1053
    267        9.8958       1.9990       10.3029       2.0108        0.0003  0.0988
    268        9.9277       2.0042       10.3527       2.0301        0.0003  0.0974
    269        9.9342       2.0036       10.2913       2.0003     +  0.0003  0.0972
    270        9.9623       2.0051       10.4179       2.0402        0.0003  0.0967
    271        9.9495       2.0155       10.3165       2.0165        0.0003  0.0976
    272        9.9215       2.0046       10.2776       2.0001     +  0.0003  0.0975
    273        9.8989       1.9960       10.4992       2.0451        0.0003  0.0980
    274        9.9241       2.0078       10.4035       2.0469        0.0003  0.0975
    275        9.9012       1.9974       10.3583       2.0218        0.0003  0.0978
    276        9.9218       2.0024       10.4039       2.0556        0.0003  0.0977
    277        9.9308       2.0046       10.8659       2.1386        0.0003  0.0954
    278        9.8990       1.9961       10.2940       1.9984     +  0.0003  0.1760
    279        9.9578       2.0150       10.2932       2.0050        0.0003  0.0990
    280        9.8998       1.9930       10.3219       2.0323        0.0003  0.0969
    281        9.8907       1.9893       10.2723       2.0066        0.0003  0.0965
    282        9.8944       1.9931       10.3314       2.0269        0.0003  0.1031
    283        9.9090       2.0011       10.2808       2.0005        0.0003  0.0976
    284        9.9638       2.0212       10.7474       2.1588        0.0003  0.0984
    285        9.9082       2.0054       10.3627       2.0594        0.0003  0.0988
    286        9.8999       1.9983       10.4395       2.0342        0.0003  0.0999
    287        9.9051       1.9996       10.4167       2.0270        0.0003  0.0972
    288        9.9367       2.0029       11.0638       2.2175        0.0003  0.0974
    289        9.9510       2.0201       10.4041       2.0410        0.0003  0.0984
    290        9.8862       1.9973       10.2883       1.9912     +  0.0003  0.0972
    291        9.9335       2.0058       10.4414       2.0597        0.0003  0.0988
    292        9.8801       1.9896       11.2034       2.1817        0.0003  0.1210
    293        9.9612       2.0084       10.2912       1.9978        0.0003  0.0974
    294        9.9149       2.0004       10.4739       2.0721        0.0003  0.0975
    295        9.9201       2.0009       10.3878       2.0552        0.0003  0.0971
    296        9.8749       1.9893       10.3235       2.0346        0.0003  0.0975
    297        9.8962       2.0044       10.3851       2.0348        0.0003  0.1053
    298        9.8853       1.9896       10.9170       2.1318        0.0003  0.0979
    299        9.9470       2.0191       10.6505       2.1118        0.0003  0.0961
    300        9.9054       2.0016       10.3083       2.0103        0.0003  0.0967
    301        9.8884       1.9979       10.3231       2.0380        0.0003  0.0975
    302        9.8951       2.0063       10.2794       2.0001        0.0003  0.0958
    303        9.9217       2.0057       10.3672       2.0450        0.0003  0.0958
    304        9.8987       2.0062       10.4386       2.0577        0.0003  0.1781
    305        9.8900       1.9948       10.4546       2.0307        0.0003  0.0991
    306        9.9696       2.0328       11.2850       2.3061        0.0003  0.0983
    307        9.9196       1.9999       10.8129       2.1385        0.0003  0.0971
    308        9.9653       2.0233       11.3015       2.2968        0.0003  0.0972
    309        9.9826       2.0090       11.2653       2.1935        0.0003  0.0970
    310        9.9207       2.0054       10.4656       2.0680        0.0003  0.0964
    311        9.9065       1.9921       10.6278       2.0744        0.0003  0.0986
    312        9.9461       2.0164       10.7961       2.1606        0.0003  0.0984
    313        9.8878       1.9958       10.4509       2.0730        0.0003  0.0968
    314        9.8795       1.9931       10.3534       2.0164        0.0003  0.0976
    315        9.8701       1.9904       10.2612       2.0008        0.0003  0.0953
    316        9.8853       1.9932       10.2719       2.0080        0.0003  0.1041
    317        9.8927       1.9913       10.2841       2.0177        0.0001  0.0977
    318        9.8730       1.9928       10.2693       2.0031        0.0001  0.0966
    319        9.8797       1.9944       10.2654       2.0090        0.0001  0.0977
    320        9.8604       1.9903       10.2589       2.0022        0.0001  0.0963
    321        9.8712       1.9901       10.2800       2.0177        0.0001  0.0971
    322        9.8859       1.9965       10.3494       2.0452        0.0001  0.0969
    323        9.9518       2.0256       10.2760       2.0057        0.0001  0.0966
    324        9.8958       1.9982       10.3993       2.0551        0.0001  0.0990
    325        9.8887       1.9944       10.3061       2.0065        0.0001  0.1006
    326        9.8660       1.9876       10.3734       2.0388        0.0001  0.1000
    327        9.8825       1.9938       10.2654       2.0101        0.0001  0.0977
    328        9.8930       1.9934       10.2830       1.9940        0.0001  0.0987
    329        9.8861       2.0004       10.4599       2.0542        0.0001  0.0968
Stopping since valid_mae has not improved in the last 40 epochs.
Restoring best model from epoch 290.
Out[49]:
<class 'skorch.regressor.NeuralNetRegressor'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=27, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Linear(in_features=32, out_features=1, bias=True)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.regressor.NeuralNetRegressor'>[initialized](
  module_=BetterMLP(
    (model): Sequential(
      (0): Linear(in_features=27, out_features=32, bias=True)
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Linear(in_features=32, out_features=1, bias=True)
    )
  ),
)
In [50]:
y_pred = net.predict(X_test)
mean_absolute_error(y_test, y_pred)
Out[50]:
2.0315136909484863
In [51]:
history = net.history

plt.plot(history[:, 'train_mae'], label='train_mae')
plt.plot(history[:, 'valid_mae'], label='valid_mae')
ax1 = plt.gca()  # get current axes
ax2 = plt.gca().twinx()  # create a second y-axis
plt.plot(history[:, "event_lr"], label='learning_rate', color='gray', linestyle='--')
ax2.set_ylabel('Learning Rate')

ax1.set_ylabel('MAE')

for i, checkpoint in enumerate(history[:, "event_cp"][::-1]):
    if checkpoint:
        ax1.axvline(x=len(history[:, "event_cp"])-i-1, color='red', 
                    linestyle='-.', label='Checkpoint', alpha=0.5)
        break

ax1.legend(loc='lower left')

plt.xlabel('Epoch')
plt.show()
No description has been provided for this image

Exercise¶

Build an MLP to predict whether a patient has an in-hospital complication, based on features like BMI, age, etc.

  • use a dummy baseline that always predicts 1
  • use an sklearn MLP
  • use a torch MLP

optimize for F1-score w.r.t. the positive class:

f1_score(y_pred, y_test)  # no "weighted"

Plot the loss curves for the torch model

In [52]:
df = pd.read_csv("./Data/surgical_complications.csv")
df
Out[52]:
bmi Age asa_status baseline_cancer baseline_charlson baseline_cvd baseline_dementia baseline_diabetes baseline_digestive baseline_osteoart ... complication_rsi dow gender hour month moonphase mort30 mortality_rsi race complication
0 19.31 59.2 1 1 0 0 0 0 0 0 ... -0.57 3 0 7.63 6 1 0 -0.43 1 0
1 18.73 59.1 0 0 0 0 0 0 0 0 ... 0.21 0 0 12.93 0 1 0 -0.41 1 0
2 21.85 59.0 0 0 0 0 0 0 0 0 ... 0.00 2 0 7.68 5 3 0 0.08 1 0
3 18.49 59.0 1 0 1 0 0 1 1 0 ... -0.65 2 1 7.58 4 3 0 -0.32 1 0
4 19.70 59.0 1 0 0 0 0 0 0 0 ... 0.00 0 0 7.88 11 0 0 0.00 1 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
14630 18.79 14.1 1 0 1 0 0 0 0 0 ... -0.54 1 0 7.78 0 1 0 -0.16 1 1
14631 19.65 12.6 0 0 0 0 0 0 0 0 ... -1.42 4 0 8.40 6 1 0 -0.77 1 1
14632 14.84 12.6 1 0 0 0 0 0 0 0 ... 0.65 0 0 13.25 3 3 0 0.99 1 1
14633 17.75 8.9 0 0 1 0 0 0 1 0 ... -0.50 0 1 8.30 5 0 0 0.17 1 1
14634 14.40 6.1 1 0 1 0 0 0 1 0 ... 0.78 2 0 7.65 4 1 0 1.06 0 1

14635 rows × 25 columns

In [53]:
target = "complication"
X = df.drop(columns=[target]).values.astype(np.float32)
y = df[target].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)