【问题标题】:LightGBMRegressor Custom eval loss fuction return as list error for single outputLightGBM Regressor Custom feval loss function return as list error for single output
【发布时间】:2020-11-24 15:25:47
【问题描述】:

我想在我的 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: too many values to unpack (expected 3)。当我在没有 eval_set 和 early_stopping_rounds 的情况下运行它时,它工作正常。你能帮帮我吗?

谢谢

【问题讨论】:

    标签: python eval lightgbm custom-function


    【解决方案1】:

    只需将返回值更改为元组

    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)
    

    根据docs,这是因为它期望单个元素的元组或多个元素的元组列表

    返回 (eval_name, eval_result, is_higher_better) 或列表 (eval_name, eval_result, is_higher_better)

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2022-08-12
      • 2014-05-18
      相关资源
      最近更新 更多