shenweichen / DeepCTR-Torch

【PyTorch】Easy-to-use,Modular and Extendible package of deep-learning based CTR models.
https://deepctr-torch.readthedocs.io/en/latest/index.html
Apache License 2.0
2.95k stars 696 forks source link

How to do hyperparameter tuning with DeepCTR? #274

Open CFZhai opened 1 year ago

CFZhai commented 1 year ago

e.g, Could you show an example of how to do hyperparameter tuning with DeepFM? thank you!

alibugra commented 1 year ago

I suggest you to use Hyperopt. http://hyperopt.github.io/hyperopt/

CFZhai commented 1 year ago

Do you have any example of using Hyperopt and DeepCTR together? Thank you, Alibugra!

alibugra commented 1 year ago

I prepared an example via the file "examples/run_classification_criteo.py". In this file, you can delete the code where the model part is defined (from Line 54 to Line 66) and then add the following code.

    def objective_function(param_space):
        dnn_hidden_units = param_space["dnn_hidden_units"]
        dnn_dropout = param_space["dnn_dropout"]

        model = DeepFM(linear_feature_columns=linear_feature_columns, dnn_feature_columns=dnn_feature_columns,
                       task='binary',
                       dnn_hidden_units=dnn_hidden_units, dnn_dropout=dnn_dropout,
                       l2_reg_embedding=1e-5, device=device)

        model.compile("adagrad", "binary_crossentropy",
                      metrics=["binary_crossentropy", "auc"], )

        history = model.fit(train_model_input, train[target].values, batch_size=32, epochs=10, verbose=2,
                            validation_split=0.2)
        pred_ans = model.predict(test_model_input, 256)
        print("")
        print("test LogLoss", round(log_loss(test[target].values, pred_ans), 4))
        auc = round(roc_auc_score(test[target].values, pred_ans), 4)
        print("test AUC", auc)

        return {
            "loss": -auc,
            "status": STATUS_OK,
            "dnn_hidden_units": dnn_hidden_units,
            "dnn_dropout": dnn_dropout
        }

    trials = Trials()
    param_space = {
        "dnn_hidden_units": hp.choice("dnn_hidden_units",
                                      [(128, 128), (256, 256)]),
        "dnn_dropout": hp.choice("dnn_dropout", [0, 0.1])
    }
    best = fmin(fn=objective_function, space=param_space,
                algo=tpe.suggest, max_evals=20, trials=trials)
    print("best parameter is:", str(best))

Do not forget to install Hyperopt and related libraries, also add them to the code.

import hyperopt
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
CFZhai commented 1 year ago

That is great! Thank you so much!