调节神经网络参数的时候需要对网络进行评估,一般来说把数据集分为训练集和验证集。如果数据集不够大时,评估可能会产生误差,此时可以选择k折交叉验证。
这种方法将可用数据划分为 K个分区(K 通常取 4 或 5),实例化 K 个相同的模型,将每个模型在 K-1 个分区上训练,并在剩下的一个分区上进行评估。模型的验证分数等于 K 个验证分数的平均值。
![](https://i-blog.csdnimg.cn/blog_migrate/9378f8799b50e31f1cc9c770ee9d5069.png)
import numpy as np
k = 4
num_val_samples = len(train_data) // k#验证集长度
num_epochs = 100
all_scores = []
for i in range(k):
print('processing fold #', i)
val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples] #验证集
val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]
partial_train_data = np.concatenate(
[train_data[:i * num_val_samples],
train_data[(i + 1) * num_val_samples:]],
axis=0)#沿0轴连接剩余数据作为训练集
partial_train_targets = np.concatenate(
[train_targets[:i * num_val_samples],
train_targets[(i + 1) * num_val_samples:]],
axis=0)
model = build_model() #构建模型
model.fit(partial_train_data, partial_train_targets,
epochs=num_epochs, batch_size=1, verbose=0)#训练模型
val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)#在验证集上评估模型
all_scores.append(val_mae)#记录每一折的mae
'''
#在验证的时候也可以选择使用history来记录,可读取的参数有'loss', 'mae', 'val_loss', 'val_mae'
history = model.fit(partial_train_data, partial_train_targets,
validation_data=(val_data, val_targets),
epochs=num_epochs, batch_size=1, verbose=0)
mae_history = history.history['mae']
'''
此外还学到了另外一种k折的写法,利用传统的sklearn来写,感觉会更方便
from sklearn.model_selection import StratifiedKFold,KFold
kfold = KFold(n_splits=4, shuffle=True, random_state=1)#定义,注意StratifiedKFold支持分类问题,回归问题用KFold
for train_index,test_index in kfold.split(train_data,train_targets):#进行交叉验证
x_train,x_test=train_data[train_index],train_data[test_index]#获取训练集和测试集
y_train,y_test=train_targets[train_index],train_targets[test_index]
model = build_model()
model.fit(x_train,y_train,epochs=100, batch_size=1, verbose=0)
val_mse, val_mae = model.evaluate(x_test,y_test, verbose=0)
all_scores.append(val_mae)
print(all_scores)