本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
如何使用 SageMaker LightGBM
您可以使用 LightGBM 作為 SageMaker Amazon 內建演算法。下一節說明如何將 LightGBM 與 SageMaker Python 搭配使用SDK。如需如何從 Amazon SageMaker Studio Classic UI 使用 LightGBM 的詳細資訊,請參閱 SageMaker JumpStart 預先訓練的模型。
-
使用 LightGBM 作為內建演算法
使用 LightGBM 內建演算法來建置 LightGBM 訓練容器,如下列程式碼範例所示。您可以使用 URI SageMaker
image_uris.retrieve
API(如果使用 Amazon SageMaker Python SDK第 2 版 get_image_uri
API) 自動識別 LightGBM 內建演算法映像。指定 LightGBM Image 之後URI,您可以使用 LightGBM 容器,使用估算器建構 SageMaker 估算器API並啟動訓練任務。LightGBM 內建演算法會以指令碼模式執行,但訓練指令碼會提供給您,不需要加以取代。如果您有使用指令碼模式建立 SageMaker 訓練任務的豐富經驗,則可以整合自己的 LightGBM 訓練指令碼。
from sagemaker import image_uris, model_uris, script_uris train_model_id, train_model_version, train_scope = "lightgbm-classification-model", "*", "training" training_instance_type = "ml.m5.xlarge" # Retrieve the docker image train_image_uri = image_uris.retrieve( region=None, framework=None, model_id=train_model_id, model_version=train_model_version, image_scope=train_scope, instance_type=training_instance_type ) # Retrieve the training script train_source_uri = script_uris.retrieve( model_id=train_model_id, model_version=train_model_version, script_scope=train_scope ) train_model_uri = model_uris.retrieve( model_id=train_model_id, model_version=train_model_version, model_scope=train_scope ) # Sample training data is available in this bucket training_data_bucket = f"jumpstart-cache-prod-{aws_region}" training_data_prefix = "training-datasets/tabular_multiclass/" training_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}/train" validation_dataset_s3_path = f"s3://{training_data_bucket}/{training_data_prefix}/validation" output_bucket = sess.default_bucket() output_prefix = "jumpstart-example-tabular-training" s3_output_location = f"s3://{output_bucket}/{output_prefix}/output" from sagemaker import hyperparameters # Retrieve the default hyperparameters for training the model hyperparameters = hyperparameters.retrieve_default( model_id=train_model_id, model_version=train_model_version ) # [Optional] Override default hyperparameters with custom values hyperparameters[ "num_boost_round" ] = "500" print(hyperparameters) from sagemaker.estimator import Estimator from sagemaker.utils import name_from_base training_job_name = name_from_base(f"built-in-algo-{train_model_id}-training") # Create SageMaker Estimator instance tabular_estimator = Estimator( role=aws_role, image_uri=train_image_uri, source_dir=train_source_uri, model_uri=train_model_uri, entry_point="transfer_learning.py", instance_count=1, # for distributed training, specify an instance_count greater than 1 instance_type=training_instance_type, max_run=360000, hyperparameters=hyperparameters, output_path=s3_output_location ) # Launch a SageMaker Training job by passing the S3 path of the training data tabular_estimator.fit( { "train": training_dataset_s3_path, "validation": validation_dataset_s3_path, }, logs=True, job_name=training_job_name )
如需如何將 LightGBM 設定為內建演算法的詳細資訊,請參閱下列筆記本範例。