深度学习模型stacking模型融合python代码,看了你就会使
话不多说,直接上代码
1 def stacking_first(train, train_y, test): 2 savepath = './stack_op{}_dt{}_tfidf{}/'.format(args.option, args.data_type, args.tfidf) 3 os.makedirs(savepath, exist_ok=True) 4 5 count_kflod = 0 6 num_folds = 6 7 kf = KFold(n_splits=num_folds, shuffle=True, random_state=10) 8 # 测试集上的预测结果 9 predict = np.zeros((test.shape[0], config.n_class)) 10 # k折交叉验证集的预测结果 11 oof_predict = np.zeros((train.shape[0], config.n_class)) 12 scores = [] 13 f1s = [] 14 15 for train_index, test_index in kf.split(train): 16 # 训练集划分为6折,每一折都要走一遍。那么第一个是5份的训练集索引,第二个是1份的测试集,此处为验证集是索引 17 18 kfold_X_train = {} 19 kfold_X_valid = {} 20 21 # 取数据的标签 22 y_train, y_test = train_y[train_index], train_y[test_index] 23 # 取数据 24 kfold_X_train, kfold_X_valid = train[train_index], train[test_index] 25 26 # 模型的前缀 27 model_prefix = savepath + 'DNN' + str(count_kflod) 28 if not os.path.exists(model_prefix): 29 os.mkdir(model_prefix) 30 31 M = 4 # number of snapshots 32 alpha_zero = 1e-3 # initial learning rate 33 snap_epoch = 16 34 snapshot = SnapshotCallbackBuilder(snap_epoch, M, alpha_zero) 35 36 # 使用训练集的size设定维度,fit一个模型出来 37 res_model = get_model(train) 38 res_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 39 # res_model.fit(train_x, train_y, batch_size=BATCH_SIZE, epochs=EPOCH, verbose=1, class_weight=class_weight) 40 res_model.fit(kfold_X_train, y_train, batch_size=BATCH_SIZE, epochs=snap_epoch, verbose=1, 41 validation_data=(kfold_X_valid, y_test), 42 callbacks=snapshot.get_callbacks(model_save_place=model_prefix)) 43 44 # 找到这个目录下所有已经训练好的深度学习模型,通过".h5" 45 evaluations = [] 46 for i in os.listdir(model_prefix): 47 if '.h5' in i: 48 evaluations.append(i) 49 50 # 给测试集和当前的验证集开辟空间,就是当前折的数据预测结果构建出这么多的数据集[数据个数,类别] 51 preds1 = np.zeros((test.shape[0], config.n_class)) 52 preds2 = np.zeros((len(kfold_X_valid), config.n_class)) 53 # 遍历每一个模型,用他们分别预测当前折数的验证集和测试集,N个模型的结果求平均 54 for run, i in enumerate(evaluations): 55 res_model.load_weights(os.path.join(model_prefix, i)) 56 preds1 += res_model.predict(test, verbose=1) / len(evaluations) 57 preds2 += res_model.predict(kfold_X_valid, batch_size=128) / len(evaluations) 58 59 # 测试集上预测结果的加权平均 60 predict += preds1 / num_folds 61 # 每一折的预测结果放到对应折上的测试集中,用来最后构建训练集 62 oof_predict[test_index] = preds2 63 64 # 计算精度和F1 65 accuracy = mb.cal_acc(oof_predict[test_index], np.argmax(y_test, axis=1)) 66 f1 = mb.cal_f_alpha(oof_predict[test_index], np.argmax(y_test, axis=1), n_out=config.n_class) 67 print('the kflod cv is : ', str(accuracy)) 68 print('the kflod f1 is : ', str(f1)) 69 count_kflod += 1 70 71 # 模型融合的预测结果,存起来,用以以后求平均值 72 scores.append(accuracy) 73 f1s.append(f1) 74 # 指标均值,最为最后的预测结果 75 print('total scores is ', np.mean(scores)) 76 print('total f1 is ', np.mean(f1s)) 77 return predict
时刻记着自己要成为什么样的人!