如何使用`set_hyperparameters`将lambda超参数传递给Sagemaker XGBoost估算器

huangapple go评论66阅读模式
英文:

How to pass lambda hypterparameter to Sagemaker XGboost estimator with set_hyperparameters

问题

xgb = sagemaker.estimator.Estimator(**training_dict, sagemaker_session=sagemaker_session)

xgb.set_hyperparameters(
num_round = 2000,
objective = 'binary:logistic',
tree_method = 'hist',
eval_metric = 'auc',
.
.
.
alpha = 1
)
xgb.fit({'train': s3_input_train, 'validation': s3_input_validation})

英文:
xgb = sagemaker.estimator.Estimator(**training_dict, sagemaker_session=sagemaker_session)

xgb.set_hyperparameters(    num_round = 2000,
                            objective = 'binary:logistic', 
                            tree_method = 'hist', 
                            eval_metric = 'auc', 
                           .
                           .
                           .
                            lambda = 0.5,
                            alpha = 1
                       )
xgb.fit({'train': s3_input_train, 'validation': s3_input_validation}) 

The documentation here lists lambda for L2 Regularization but when I pass this to the set_parameter method for sagemaker estimator I get a syntax error because lambda is keyword.

https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost_hyperparameters.html

    lambda = 0.5, 
           ^
SyntaxError: invalid syntax

答案1

得分: 1

"lambda" 是 Python 中的一个保留关键字,用于 lambda 表达式

要解决这个问题,您可以将函数参数放入字典中,然后将字典“解包”为函数参数:

xgb.set_hyperparameters(**{ "num_round": 2000,
                           .
                           .
                           .
                            "lambda": 0.5,
                            "alpha": 1
                       })
英文:

lambda is a reserved keyword in Python for lambda expressions.

The way you can get around it is to rather put your function arguments in a dict and then "unpack" the dict into function arguments:

xgb.set_hyperparameters(**{ "num_round": 2000,
                           .
                           .
                           .
                            "lambda": 0.5,
                            "alpha": 1
                       })

huangapple
  • 本文由 发表于 2023年2月24日 14:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定