LightGBMRegressor自定义评估丢失函数返回为单个输出的列表错误



我想在我的lightGBM模型中使用自定义eval函数。我的代码如下:

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
)

不幸的是,在运行这段代码时,我得到了错误ValueError:太多的值无法解压缩(应为3(。当我在没有eval_set和early_stopping_rounds的情况下运行它时,它运行得很好。你能帮帮我吗?

感谢

只需将返回值更改为元组

def mle_loss(y_true, y_pred):
....

return ("mle_loss", total_log_prob.numpy(), True)

以列表形式返回会抛出错误,例如

feval_ret = ['mle_loss', -1.4523773, True]
if isinstance(feval_ret, list):
for eval_name, val, is_higher_better in feval_ret:
pass

会抛出你提到的错误

ValueError: too many values to unpack (expected 3)

根据文档,发生这种情况是因为它期望单个元素的元组或多个元素的元组列表

返回(eval_name、eval_result、is_higher_better(或(eval_name、eval_sult、is_higher_beter(列表

最新更新