刚开始学李宏毅老师的深度学习课程,记录一下完成实验的过程
做法尽量从课程作业的 hints 出发,不用一些 tricky 的技巧,尽量使用通法
分别需要达到的指标
-
private
-
public
Simple#
只需要运行代码即可
提交结果
Medium#
特征选择
观察训练数据可以看出来第 0 列是 id,没什么用,只需把它筛掉就行
feat_idx = list(range(raw_x_train.shape[1]))
feat_idx = feat_idx[1:]
Result
居然连 strong 的指标都过了
Strong#
Medium 将 Strong 的指标也刷了,那就不特别进行模型架构和优化器调整了,沿用 Medium 作法
Boss#
不希望使用太 tricky 的技巧,只做了以下调整
-
batch size 从 256→128(合适的 batch size 相当于给 loss 加了一个小噪声,类似于退火,可以有机会收敛到全局泛化更好的地方)
-
模型架构改为:加入了 leakyrelu 和 dropout,各种激活函数学习过程参考:一文搞懂激活函数 (Sigmoid/ReLU/LeakyReLU/PReLU/ELU) - 知乎
self.layers = nn.Sequential( nn.Linear(input_dim, 32), nn.LeakyReLU(), nn.Linear(32, 64), nn.LeakyReLU(), nn.Dropout(0.1), nn.Linear(64, 1) )
-
优化器为
optimizer = torch.optim.SGD(model.parameters(), lr=config['learning_rate'], momentum=0.9, weight_decay=config['weight_decay'])
-
特征选择模块
from sklearn.feature_selection import SelectKBest from sklearn.feature_selection import f_regression features = pd.read_csv('./covid.train.csv') x_data, y_data = features.iloc[:, 0:117], features.iloc[:, 117] #try choose your k best features k = 24 selector = SelectKBest(score_func=f_regression, k=k) result = selector.fit(x_data, y_data) #result.scores_ inclues scores for each features #np.argsort sort scores in ascending order by index, we reverse it to make it descending. idx = np.argsort(result.scores_)[::-1] print(f'Top {k} Best feature score ') print(result.scores_[idx[:k]]) print(f'\nTop {k} Best feature index ') print(idx[:k]) print(f'\nTop {k} Best feature name') print(x_data.columns[idx[:k]]) selected_idx = list(np.sort(idx[:k])) print(selected_idx) print(x_data.columns[selected_idx])
由于使用 KBest,前 24 个特征远大于后面的特征的特征得分,所以选 24 个
-
数据预处理:最大最小值归一化,参考:如何理解归一化(normalization)? - 知乎
# Normalization x_min, x_max = x_train.min(axis=0), x_train.max(axis=0) x_train = (x_train - x_min) / (x_max - x_min) x_valid = (x_valid - x_min) / (x_max - x_min) x_test = (x_test - x_min) / (x_max - x_min)
-
参数设置
config = { 'seed': 5201314, # Your seed number, you can pick your lucky number. :) 'select_all': False, # Whether to use all features. 'valid_ratio': 0.2, # validation_size = train_size * valid_ratio 'n_epochs': 3000, # Number of epochs. 'batch_size': 128, 'learning_rate': 1e-4, 'weight_decay': 1e-4, 'early_stop': 600, # If model has not improved for this many consecutive epochs, stop training. 'save_path': './models/model.ckpt' # Your model will be saved here. }
最后在 private 上指标还差一点,但是时间花的太久了,暂且先这样吧,希望在做这个过程中学到一些通法。
想要达到 boss baseline 可参考机器学习手艺人 - CSDN 博客