I want to use custom eval function in my lightGBM model. My code is as follow:
X = df.loc[:, "VALUE_1":"TVALUE_33"]
y_true = df.loc[:, "VALUE_34"]
X_train, X_test, y_train, y_test = train_test_split(X, y_true, test_size= 0.30, random_state = 333)
def mle_loss(y_true, y_pred):
y_true = tf.cast(y_true, dtype= tf.float32)
# get params for probability distributions
mu = y_pred.mean()
sigma = y_pred.std()
mu = tf.cast(mu, tf.float32)
sigma = tf.cast(sigma, tf.float32)
total_dist = tfp.distributions.Normal(loc=mu, scale=sigma)
total_log_prob = total_dist.log_prob(y_true)[0]
return ["mle_loss", total_log_prob.numpy(), True]
hyper_params_11 = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': "custom",
'subsample_freq': 250,
'subsample': 0.8,
'num_leaves': 16,
'min_split_gain': 50,
'min_child_samples': 25,
'max_depth': 5,
'max_bin': 1024,
'learning_rate': 0.001,
'colsample_bytree': 0.5,
"num_iterations": 10000,
"lambda_l1" : 1,
"lambda_l2" : 1
}
gbm_model_1 = lgb.LGBMRegressor(**hyper_params_1)
gbm_model_1.fit(X_train, y_train,
eval_set=[(X_test, y_test), (X_train,y_train)],
eval_metric = mle_loss,
early_stopping_rounds=100
)
Unfortunatelu running the piece of code I got error ValueError: too many values to unpack (expected 3). WHen I run it without eval_set and early_stopping_rounds, it works fine. Can you please help me?
Thanks
Just change your return values as tuples
def mle_loss(y_true, y_pred):
....
return ("mle_loss", total_log_prob.numpy(), True)
Returning as list throws an error, for example,
feval_ret = ['mle_loss', -1.4523773, True]
if isinstance(feval_ret, list):
for eval_name, val, is_higher_better in feval_ret:
pass
will throw the error you mentioned
ValueError: too many values to unpack (expected 3)
According to docs this happens because it is expecting a tuple for a single element or a list of tuples for multiples elements
returns (eval_name, eval_result, is_higher_better) or list of (eval_name, eval_result, is_higher_better)