1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
def get_k_fold_data(k, i, X, y):
assert k > 1
fold_size = X.shape[0] // k
X_train, y_train = None, None
for j in range(k):
idx = slice(j*fold_size, (j+1)*fold_size)
X_part, y_part = X[idx, :], y[idx]
if j == i:
X_valid, y_valid = X_part, y_part
elif X_train is None:
X_train, y_train = X_part, y_part
else:
X_train = torch.cat([X_train, X_part], 0)
y_train = torch.cat([y_train, y_part], 0)
return X_train, y_train, X_valid, y_valid
def k_fold(k, X_train, y_train,
num_epochs, learning_rate, weight_decay, batch_size,
in_feats, hidden_feats, dropout):
train_l_sum, valid_l_sum = 0,0
for i in range(k):
data = get_k_fold_data(k, i, X_train, y_train)
model = MLP(in_feats, hidden_feats, dropout)
train_ls, valid_ls = train(model, *data, num_epochs, learning_rate, weight_decay, batch_size)
#将最后一轮的性能作为该模型的最终性能
train_l_sum += train_ls[-1]
valid_l_sum += valid_ls[-1]
# print(f'Fold-{i+1}, train log rmse {float(train_ls[-1]):f},'
# f'valid log rmse {float(valid_ls[-1]):f}')
return train_l_sum / k, valid_l_sum / k
# k, num_epochs, learning_rate, weight_decay, batch_size = 10, 100, 5, 0, 64
# in_feats, hidden_feats, dropout = train_feats.shape[1], 64, 0.5
# train_l, valid_l = k_fold(k, train_feats, train_labels,
# num_epochs, learning_rate, weight_decay, batch_size,
# in_feats, hidden_feats, dropout)
|