

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SageMaker AI CatBoost の使用方法
<a name="catboost-modes"></a>

CatBoost は Amazon SageMaker AI に組み込まれているアルゴリズムとして使用できます。次のセクションでは、SageMaker Python SDK で CatBoost を使用する方法について説明します。Amazon SageMaker Studio Classic UI から CatBoost を使用する方法については、「[SageMaker JumpStart の事前トレーニング済みモデル](studio-jumpstart.md)」を参照してください。
+ **CatBoost を組み込みアルゴリズムとして使用する**

  次のコード例に示すように、CatBoost 組み込みアルゴリズムを使用して、CatBoost トレーニングコンテナを構築します。SageMaker AI `image_uris.retrieve` API を使用して、CatBoost 組み込みアルゴリズムイメージ URI を自動的に見つけることができます。

  CatBoost イメージ URI を指定したら、CatBoost コンテナを使用して SageMaker AI ModelTrainer API を使用して ModelTrainer を構築し、トレーニングジョブを開始できます。CatBoost の組み込みアルゴリズムはスクリプトモードで実行されますが、トレーニングスクリプトが提供されているので、置き換える必要はありません。スクリプトモードを使用して SageMaker トレーニングジョブを作成した経験が豊富な場合は、独自の CatBoost トレーニングスクリプトを組み込むことができます。

  ```
  from sagemaker.core import image_uris
  from sagemaker.core import model_uris, script_uris
  
  train_model_id, train_model_version, train_scope = "catboost-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[
      "iterations"
  ] = "500"
  print(hyperparameters)
  
  from sagemaker.train import ModelTrainer
  from sagemaker.train.configs import InputData
  from sagemaker.train.configs import SourceCode, Compute, StoppingCondition, OutputDataConfig
  from sagemaker.utils import name_from_base
  
  training_job_name = name_from_base(f"built-in-algo-{train_model_id}-training")
  
  # Create SageMaker ModelTrainer instance
  tabular_model_trainer = ModelTrainer(
      role=aws_role,
      training_image=train_image_uri,
      source_code=SourceCode(source_dir=train_source_uri, entry_script="transfer_learning.py"),
      # In V3, pre-trained model artifacts are passed via input_data_config
      compute=Compute(instance_type=training_instance_type, instance_count=1),
      stopping_condition=StoppingCondition(max_runtime_in_seconds=360000),
      hyperparameters=hyperparameters,
      output_data_config=OutputDataConfig(s3_output_path=s3_output_location)
  )
  
  # Launch a SageMaker Training job by passing the S3 path of the training data
  tabular_model_trainer.train(
      input_data_config=[
          InputData(channel_name="training", data_source=training_dataset_s3_path),
          InputData(channel_name="validation", data_source=validation_dataset_s3_path),
          InputData(channel_name="model", data_source=train_model_uri),
      ]
  )
  ```

  CatBoost を組み込みアルゴリズムとして設定する方法の詳細については、次のノートブックの例を参照してください。
  + [Tabular classification with Amazon SageMaker AI LightGBM and CatBoost algorithm](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/lightgbm_catboost_tabular/Amazon_Tabular_Classification_LightGBM_CatBoost.ipynb)
  + [Tabular regression with Amazon SageMaker AI LightGBM and CatBoost algorithm](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/lightgbm_catboost_tabular/Amazon_Tabular_Regression_LightGBM_CatBoost.ipynb)