示例#1
0
def train_DeepFM():
    X, y, sparse_list, dense_list = feature_engineering.get_NN_data(
        use_over_sampler=True)

    data = pd.DataFrame(y)
    dnn_feature_columns = linear_feature_columns = sparse_list + dense_list
    feature_names = inputs.get_feature_names(linear_feature_columns +
                                             dnn_feature_columns)

    # kFold cv
    models = []
    scores = []

    kf = StratifiedKFold(n_splits=5, random_state=42)
    for i, (tdx, vdx) in enumerate(kf.split(X, y)):
        print(f'Fold : {i}')
        X_train, X_val, y_train, y_val = X.loc[tdx], X.loc[vdx], y.loc[
            tdx], y.loc[vdx]
        y_true = y_val

        X_train = {name: X_train[name] for name in feature_names}
        X_val = {name: X_val[name] for name in feature_names}

        model = DeepFM(linear_feature_columns,
                       dnn_feature_columns,
                       dnn_hidden_units=(128, 64),
                       dnn_use_bn=True,
                       task='binary',
                       dnn_dropout=0.5)

        best_param_path = '/Users/a_piao/PycharmProjects/BankMarketing/workspace/DeepFM/best_param_DeepFM.py_%d.h5' % i
        # best_param_path = 'best_param_%s_%d.h5' % (os.path.basename(__file__), i)

        if os.path.exists(best_param_path):
            model.load_weights(best_param_path)
        else:
            model.compile("adam",
                          "binary_crossentropy",
                          metrics=['binary_crossentropy'])
            # tb = TensorBoard(log_dir="/Users/a_piao/PycharmProjects/NightLife_recommend/workspace/DeepFM/log", write_images=1, histogram_freq=1)
            es = EarlyStopping(monitor='val_binary_crossentropy',
                               mode='min',
                               patience=20)
            mc = ModelCheckpoint(best_param_path,
                                 monitor='val_binary_crossentropy',
                                 mode='min',
                                 save_best_only=False,
                                 verbose=False,
                                 save_weights_only=True)
            # model.fit(X_train, y_train, batch_size=512, epochs=1000, verbose=2, validation_split=0.2, callbacks=[es, mc])
            model.fit(X_train,
                      y_train,
                      validation_data=(X_val, y_val),
                      batch_size=1024,
                      epochs=1000,
                      verbose=2,
                      callbacks=[es, mc])
            # model.fit(X_train, y_train, batch_size=1024, epochs=100, verbose=2, callbacks=[es, mc])
            model.load_weights(best_param_path)

        y_pred = model.predict(X_val, batch_size=64).flatten()
        auc = roc_auc_score(y_true, y_pred)
        print("AUC score at %d floder: %f" % (i, auc))
        scores.append(auc)
        models.append(model)
        data.loc[vdx, 'y_pred'] = y_pred
        # print(data['y_pred'].value_counts())
        # exit()

    mean_score = np.mean(scores)
    oof = roc_auc_score(data['y'], data['y_pred'])
    print("5-floder total mean_score:", mean_score)
    print("5-floder oof auc score:", oof)
    print("----train %s finish!----" % 'DeepFM')
    cal_roc_curve(data['y'], data['y_pred'], 'DeepFM')

    return data['y_pred']
示例#2
0
    test_model_input = {name: test[name].values for name in sparse_features}

    # Instantiate a FeatureMetas object, add your features' meta information to it
    feature_metas = FeatureMetas()
    for feat in sparse_features:
        feature_metas.add_sparse_feature(name=feat,
                                         one_hot_dim=data[feat].nunique(),
                                         embedding_dim=32)
    # a warning need to be fixed see https://stackoverflow.com/questions/35892412/tensorflow-dense-gradient-explanation
    # Instantiate a model and compile it
    model = DeepFM(feature_metas=feature_metas,
                   linear_slots=sparse_features,
                   fm_slots=sparse_features,
                   dnn_slots=sparse_features)
    model.compile(optimizer="adam",
                  loss="binary_crossentropy",
                  metrics=['binary_crossentropy'])

    # Train the model
    history = model.fit(x=train_model_input,
                        y=train[target].values,
                        batch_size=128,
                        epochs=3,
                        verbose=2,
                        validation_split=0.2)

    # Testing
    pred_ans = model.predict(test_model_input, batch_size=256)
    print("test LogLoss", round(log_loss(test[target].values, pred_ans), 4))
    print("test AUC", round(roc_auc_score(test[target].values, pred_ans), 4))