"""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)
原文地址:https://www.cnblogs.com/wbloger/p/10217535.html
时间: 2024-11-06 03:37:26