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

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

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})

英文:
  1. xgb = sagemaker.estimator.Estimator(**training_dict, sagemaker_session=sagemaker_session)
  2. xgb.set_hyperparameters( num_round = 2000,
  3. objective = 'binary:logistic',
  4. tree_method = 'hist',
  5. eval_metric = 'auc',
  6. .
  7. .
  8. .
  9. lambda = 0.5,
  10. alpha = 1
  11. )
  12. 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

  1. lambda = 0.5,
  2. ^
  3. SyntaxError: invalid syntax

答案1

得分: 1

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

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

  1. xgb.set_hyperparameters(**{ "num_round": 2000,
  2. .
  3. .
  4. .
  5. "lambda": 0.5,
  6. "alpha": 1
  7. })
英文:

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:

  1. xgb.set_hyperparameters(**{ "num_round": 2000,
  2. .
  3. .
  4. .
  5. "lambda": 0.5,
  6. "alpha": 1
  7. })

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:

确定