Integrate with LightGBM¶
Comet can automatically log your model graph, logging metrics, and parameters from your LightGBM code. You need only add the following lines of code to your LightGBM script:
from comet_ml import start, login
import lightgbm as lgbm
login()
experiment = start()
# Your code here...
gbm = lgbm.train()
Log automatically¶
The Comet LightGBM auto-logger can automatically log:
- Model/graph description
- Metrics (such as loss and accuracy)
- Hyperparameters
- Command-line arguments
Configure Comet for LightGBM¶
Item | Experiment Parameter | Environment Setting | Configuration File Setting |
---|---|---|---|
Model/graph description | log_graph | COMET_AUTO_LOG_GRAPH | comet.auto_log.graph |
Metrics | auto_metric_logging | COMET_AUTO_LOG_METRICS | comet.auto_log.metrics |
Hyperparameters | auto_param_logging | COMET_AUTO_LOG_PARAMETERS | comet.auto_log.parameters |
Command-line arguments | parse_args | COMET_AUTO_LOG_CLI_ARGUMENTS | comet.auto_log.cli_arguments |
End-to-end example¶
Here is a simple end-to-end LightGBM example.
# Get the data for this script:
# wget https://raw.githubusercontent.com/microsoft/LightGBM/master/examples/regression/regression.train -qq
# wget https://raw.githubusercontent.com/microsoft/LightGBM/master/examples/regression/regression.test -qq
import comet_ml
import lightgbm as lgb
import os
import pandas as pd
from sklearn.metrics import mean_squared_error
experiment = comet_ml.start()
dirname = os.path.dirname(__file__)
df_train = pd.read_csv(os.path.join(dirname, "regression.train"), header=None, sep="\t")
df_test = pd.read_csv(os.path.join(dirname, "regression.test"), header=None, sep="\t")
y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
params = {
"boosting_type": "gbdt",
"objective": "regression",
"metric": {"rmse", "l2", "l1", "huber"},
"num_leaves": 31,
"learning_rate": 0.05,
"feature_fraction": 0.9,
"bagging_fraction": 0.8,
"bagging_freq": 5,
"verbosity": -1,
}
gbm = lgb.train(
params,
lgb_train,
num_boost_round=20,
valid_sets=lgb_eval,
valid_names=("validation"),
early_stopping_rounds=5,
)
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
print("The rmse of prediction is:", mean_squared_error(y_test, y_pred) ** 0.5)
Note
There are alternatives to setting the API key programatically. See more here.
Try it out!¶
Here's an example for using Comet with LightGBM.
Nov. 5, 2024