码迷,mamicode.com
首页 > 其他好文 > 详细

K折验证

时间:2019-01-04 00:18:30      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:tor   amp   ada   axis   pen   model   val   partial   res   

"""K折验证"""

#K validation

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)
    
    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)

all_scores

np.mean(all_scores)

"""保存每折验证的结果"""

#save k-viladation results
num_epochs = 500
all_mae_histories=  []
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)
    partial_train_targets = np.concatenate([train_targets[: i * num_val_samples], train_targets[(i + 1) * num_val_samples :]], axis = 0)
    
    model = build_model()
    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[val_mean_absolute_error]
    all_mae_histories.append(mae_history)

 

K折验证

标签:tor   amp   ada   axis   pen   model   val   partial   res   

原文地址:https://www.cnblogs.com/wbloger/p/10217535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!