使用神经网络解决Titanic问题,主要用两种实现,一种是纯pytorch实现神经网络的搭建,另一种是使用skorch包装好的以pytorch为后端的API实现。
|
|
0.4.1
读入数据文件,这里省去了特征工程的部分,特征采用之前特征工程提取出的特征。
|
|
|
|
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 13 columns):
PassengerId 891 non-null int64
Pclass 891 non-null int64
Sex 891 non-null int64
Title 891 non-null int64
Family_size 891 non-null int64
Family_Survival 891 non-null float64
FareBin_Code 891 non-null int64
AgeBin_Code 891 non-null int64
Embarked_C 891 non-null int64
Embarked_None 891 non-null int64
Embarked_Q 891 non-null int64
Embarked_S 891 non-null int64
Survived 891 non-null float64
dtypes: float64(2), int64(11)
memory usage: 90.6 KB
|
|
|
|
使用Pytorch搭建模型
数据载入和处理
|
|
|
|
|
|
|
|
|
|
|
|
Epoch 0/99
----------
train loss: 0.6819 Acc: 0.5674
val loss: 0.6576 Acc: 0.6425
Epoch 1/99
----------
train loss: 0.6624 Acc: 0.6096
val loss: 0.6418 Acc: 0.6425
Epoch 2/99
----------
...
Epoch 97/99
----------
train loss: 0.4965 Acc: 0.7598
val loss: 0.3836 Acc: 0.8603
Epoch 98/99
----------
train loss: 0.4819 Acc: 0.7556
val loss: 0.3836 Acc: 0.8603
Epoch 99/99
----------
train loss: 0.4981 Acc: 0.7626
val loss: 0.3836 Acc: 0.8603
Trainning complete in 0m 9s
Best val Acc: 0.877095
|
|
使用skorch搭建模型
|
|
0.3.0
epoch train_loss valid_acc valid_loss dur
------- ------------ ----------- ------------ ------
1 0.7777 0.3596 0.7712 0.0215
...
epoch train_loss valid_acc valid_loss dur
------- ------------ ----------- ------------ ------
1 0.6797 0.6704 0.6408 0.0250
2 0.6756 0.6704 0.6409 0.0250
3 0.6767 0.6704 0.6409 0.0250
...
200 0.5370 0.7709 0.4915 0.0200
0.7845117845117845 {'lr': 0.02, 'max_epochs': 200, 'module__num_units': 20}
epoch train_loss valid_acc valid_loss dur
------- ------------ ----------- ------------ ------
1 0.7113 0.6704 0.6715 0.0190
2 0.7007 0.6704 0.6704 0.0264
...
199 0.5825 0.7989 0.5358 0.0220
200 0.5838 0.7989 0.5350 0.0240
参考
1、Pytorch data_loading tutorial
2、Pytorch_transfer_learning_tutorial
4、notebook