ClassiSage: Terraform IaC 自動化された AWS SageMaker ベースの HDFS ログ分類モデル

Barbara Streisand
リリース: 2024-10-26 05:04:30
オリジナル
405 人が閲覧しました

クラシセージ

インフラストラクチャ設定の自動化に Terraform を使用して HDFS ログを分類するための AWS SageMaker とその Python SDK で作成された機械学習モデル。

リンク: GitHub
言語: HCL (terraform)、Python

コンテンツ

  • 概要: プロジェクトの概要
  • システムアーキテクチャ: システムアーキテクチャ図
  • ML モデル: モデルの概要
  • はじめに: プロジェクトの実行方法
  • コンソールの観察: プロジェクトの実行中に観察できるインスタンスとインフラストラクチャの変更。
  • 終了とクリーンアップ: 追加料金が発生しないようにします。
  • 自動作成オブジェクト: 実行プロセス中に作成されるファイルとフォルダー。

  • プロジェクトをより適切にセットアップするには、まずディレクトリ構造に従います。
  • 理解を深めるために、GitHub にアップロードされた ClassiSage のプロジェクト リポジトリから主要なリファレンスを取得してください。

概要

  • モデルは、データセット、ノートブック ファイル (SageMaker インスタンスのコードを含む) およびモデル出力を保存するための S3 とともに、HDFS ログの分類のために AWS SageMaker で作成されています。
  • インフラストラクチャのセットアップは、HashiCorp によって作成されたコードとしてのインフラストラクチャを提供するツール Terraform を使用して自動化されます。
  • 使用されるデータセットは HDFS_v1 です。
  • プロジェクトは、モデル XGBoost バージョン 1.2 を使用して SageMaker Python SDK を実装します。

システムアーキテクチャ

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

ML モデル

  • 画像URI
  # Looks for the XGBoost image URI and builds an XGBoost container. Specify the repo_version depending on preference.
  container = get_image_uri(boto3.Session().region_name,
                            'xgboost', 
                            repo_version='1.0-1')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • コンテナに対するハイパーパラメータとエスティメーターの呼び出しを初期化しています
  hyperparameters = {
        "max_depth":"5",                ## Maximum depth of a tree. Higher means more complex models but risk of overfitting.
        "eta":"0.2",                    ## Learning rate. Lower values make the learning process slower but more precise.
        "gamma":"4",                    ## Minimum loss reduction required to make a further partition on a leaf node. Controls the model’s complexity.
        "min_child_weight":"6",         ## Minimum sum of instance weight (hessian) needed in a child. Higher values prevent overfitting.
        "subsample":"0.7",              ## Fraction of training data used. Reduces overfitting by sampling part of the data. 
        "objective":"binary:logistic",  ## Specifies the learning task and corresponding objective. binary:logistic is for binary classification.
        "num_round":50                  ## Number of boosting rounds, essentially how many times the model is trained.
        }
  # A SageMaker estimator that calls the xgboost-container
  estimator = sagemaker.estimator.Estimator(image_uri=container,                  # Points to the XGBoost container we previously set up. This tells SageMaker which algorithm container to use.
                                          hyperparameters=hyperparameters,      # Passes the defined hyperparameters to the estimator. These are the settings that guide the training process.
                                          role=sagemaker.get_execution_role(),  # Specifies the IAM role that SageMaker assumes during the training job. This role allows access to AWS resources like S3.
                                          train_instance_count=1,               # Sets the number of training instances. Here, it’s using a single instance.
                                          train_instance_type='ml.m5.large',    # Specifies the type of instance to use for training. ml.m5.2xlarge is a general-purpose instance with a balance of compute, memory, and network resources.
                                          train_volume_size=5, # 5GB            # Sets the size of the storage volume attached to the training instance, in GB. Here, it’s 5 GB.
                                          output_path=output_path,              # Defines where the model artifacts and output of the training job will be saved in S3.
                                          train_use_spot_instances=True,        # Utilizes spot instances for training, which can be significantly cheaper than on-demand instances. Spot instances are spare EC2 capacity offered at a lower price.
                                          train_max_run=300,                    # Specifies the maximum runtime for the training job in seconds. Here, it's 300 seconds (5 minutes).
                                          train_max_wait=600)                   # Sets the maximum time to wait for the job to complete, including the time waiting for spot instances, in seconds. Here, it's 600 seconds (10 minutes).
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • トレーニングジョブ
  estimator.fit({'train': s3_input_train,'validation': s3_input_test})
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • 展開
  xgb_predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.m5.large')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • 検証
  # Looks for the XGBoost image URI and builds an XGBoost container. Specify the repo_version depending on preference.
  container = get_image_uri(boto3.Session().region_name,
                            'xgboost', 
                            repo_version='1.0-1')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

はじめる

  • Git Bash を使用してリポジトリのクローンを作成する / .zip ファイルをダウンロードする / リポジトリをフォークします。
  • AWS マネジメント コンソールに移動し、右上隅にあるアカウント プロファイルをクリックし、ドロップダウンから [セキュリティ認証情報] を選択します。
  • アクセス キーの作成: [アクセス キー] セクションで、[新しいアクセス キーの作成] をクリックすると、アクセス キー ID とシークレット アクセス キーを示すダイアログが表示されます。
  • キーをダウンロードまたはコピーします: (重要) .csv ファイルをダウンロードするか、キーを安全な場所にコピーします。シークレット アクセス キーを表示できるのはこのときだけです。
  • クローンされたリポジトリを開きます。 VS コード内
  • ClassiSage の下に terraform.tfvars としてファイルを作成し、その内容を次のようにします。
  hyperparameters = {
        "max_depth":"5",                ## Maximum depth of a tree. Higher means more complex models but risk of overfitting.
        "eta":"0.2",                    ## Learning rate. Lower values make the learning process slower but more precise.
        "gamma":"4",                    ## Minimum loss reduction required to make a further partition on a leaf node. Controls the model’s complexity.
        "min_child_weight":"6",         ## Minimum sum of instance weight (hessian) needed in a child. Higher values prevent overfitting.
        "subsample":"0.7",              ## Fraction of training data used. Reduces overfitting by sampling part of the data. 
        "objective":"binary:logistic",  ## Specifies the learning task and corresponding objective. binary:logistic is for binary classification.
        "num_round":50                  ## Number of boosting rounds, essentially how many times the model is trained.
        }
  # A SageMaker estimator that calls the xgboost-container
  estimator = sagemaker.estimator.Estimator(image_uri=container,                  # Points to the XGBoost container we previously set up. This tells SageMaker which algorithm container to use.
                                          hyperparameters=hyperparameters,      # Passes the defined hyperparameters to the estimator. These are the settings that guide the training process.
                                          role=sagemaker.get_execution_role(),  # Specifies the IAM role that SageMaker assumes during the training job. This role allows access to AWS resources like S3.
                                          train_instance_count=1,               # Sets the number of training instances. Here, it’s using a single instance.
                                          train_instance_type='ml.m5.large',    # Specifies the type of instance to use for training. ml.m5.2xlarge is a general-purpose instance with a balance of compute, memory, and network resources.
                                          train_volume_size=5, # 5GB            # Sets the size of the storage volume attached to the training instance, in GB. Here, it’s 5 GB.
                                          output_path=output_path,              # Defines where the model artifacts and output of the training job will be saved in S3.
                                          train_use_spot_instances=True,        # Utilizes spot instances for training, which can be significantly cheaper than on-demand instances. Spot instances are spare EC2 capacity offered at a lower price.
                                          train_max_run=300,                    # Specifies the maximum runtime for the training job in seconds. Here, it's 300 seconds (5 minutes).
                                          train_max_wait=600)                   # Sets the maximum time to wait for the job to complete, including the time waiting for spot instances, in seconds. Here, it's 600 seconds (10 minutes).
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • Terraform と Python を使用するためのすべての依存関係をダウンロードしてインストールします。
  • ターミナルに「terraform init」と入力/貼り付けて、バックエンドを初期化します。

  • 次に、「terraform Plan」と入力/貼り付けてプランを表示するか、単純に terraform validate を実行してエラーがないことを確認します。

  • 最後にターミナルに「terraform apply --auto-approve」と入力/貼り付けます

  • これにより、2 つの出力が表示されます。1 つはbucket_name、もう 1 つは pretrained_ml_instance_name です (3 番目のリソースは、グローバル リソースであるため、バケットに与えられた変数名です)。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • コマンドの完了がターミナルに表示されたら、ClassiSage/ml_ops/function.py に移動し、コードが含まれるファイルの 11 行目に移動します。
  estimator.fit({'train': s3_input_train,'validation': s3_input_test})
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

プロジェクト ディレクトリが存在するパスに変更して保存します。

  • 次に、ClassiSageml_opsdata_upload.ipynb で、次のコードを使用してセル番号 25 までのすべてのコード セルを実行します。
  xgb_predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.m5.large')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

データセットを S3 バケットにアップロードします。

  • コードセル実行の出力

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • ノートブックの実行後、AWS マネジメントコンソールを再度開きます。
  • S3 サービスと Sagemaker サービスを検索すると、開始された各サービスのインスタンス (S3 バケットと SageMaker ノートブック) が表示されます

2 つのオブジェクト、データセット、モデル コードを含む pretrained_sm.ipynb ファイルがアップロードされた「data-bucket-」という名前の S3 バケット。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model


  • AWS SageMaker のノートブック インスタンスに移動し、作成したインスタンスをクリックして、[Jupyter を開く] をクリックします。
  • その後、ウィンドウの右上にある [新規] をクリックし、ターミナルを選択します。
  • これにより、新しいターミナルが作成されます。

  • ターミナルに以下を貼り付けます (VS Code のターミナル出力に表示されるbucket_name 出力に置き換えます)。
  # Looks for the XGBoost image URI and builds an XGBoost container. Specify the repo_version depending on preference.
  container = get_image_uri(boto3.Session().region_name,
                            'xgboost', 
                            repo_version='1.0-1')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

S3 から Notebook の Jupyter 環境に pretrained_sm.ipynb をアップロードするターミナル コマンド

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model


  • 開いた Jupyter インスタンスに戻り、pretrained_sm.ipynb ファイルをクリックして開き、conda_python3 カーネルを割り当てます。
  • 4 番目のセルまで下にスクロールし、変数bucket_nameの値を、bucket_name = ""に対するVS Codeのターミナル出力に置き換えます。
  hyperparameters = {
        "max_depth":"5",                ## Maximum depth of a tree. Higher means more complex models but risk of overfitting.
        "eta":"0.2",                    ## Learning rate. Lower values make the learning process slower but more precise.
        "gamma":"4",                    ## Minimum loss reduction required to make a further partition on a leaf node. Controls the model’s complexity.
        "min_child_weight":"6",         ## Minimum sum of instance weight (hessian) needed in a child. Higher values prevent overfitting.
        "subsample":"0.7",              ## Fraction of training data used. Reduces overfitting by sampling part of the data. 
        "objective":"binary:logistic",  ## Specifies the learning task and corresponding objective. binary:logistic is for binary classification.
        "num_round":50                  ## Number of boosting rounds, essentially how many times the model is trained.
        }
  # A SageMaker estimator that calls the xgboost-container
  estimator = sagemaker.estimator.Estimator(image_uri=container,                  # Points to the XGBoost container we previously set up. This tells SageMaker which algorithm container to use.
                                          hyperparameters=hyperparameters,      # Passes the defined hyperparameters to the estimator. These are the settings that guide the training process.
                                          role=sagemaker.get_execution_role(),  # Specifies the IAM role that SageMaker assumes during the training job. This role allows access to AWS resources like S3.
                                          train_instance_count=1,               # Sets the number of training instances. Here, it’s using a single instance.
                                          train_instance_type='ml.m5.large',    # Specifies the type of instance to use for training. ml.m5.2xlarge is a general-purpose instance with a balance of compute, memory, and network resources.
                                          train_volume_size=5, # 5GB            # Sets the size of the storage volume attached to the training instance, in GB. Here, it’s 5 GB.
                                          output_path=output_path,              # Defines where the model artifacts and output of the training job will be saved in S3.
                                          train_use_spot_instances=True,        # Utilizes spot instances for training, which can be significantly cheaper than on-demand instances. Spot instances are spare EC2 capacity offered at a lower price.
                                          train_max_run=300,                    # Specifies the maximum runtime for the training job in seconds. Here, it's 300 seconds (5 minutes).
                                          train_max_wait=600)                   # Sets the maximum time to wait for the job to complete, including the time waiting for spot instances, in seconds. Here, it's 600 seconds (10 minutes).
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

コードセル実行の出力

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model


  • ファイルの先頭で、[カーネル] タブに移動して再起動を実行します。
  • 次のコードを使用して、コードセル番号 27 までノートブックを実行します。
  estimator.fit({'train': s3_input_train,'validation': s3_input_test})
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • 意図した結果が得られます。 データはフェッチされ、定義された出力パスを使用してラベルと機能に合わせて調整された後、トレーニング セットとテスト セットに分割されます。その後、SageMaker の Python SDK を使用するモデルがトレーニングされ、エンドポイントとしてデプロイされ、検証されてさまざまなメトリクスが提供されます。

コンソール観察メモ

8 番目のセルの実行

  xgb_predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.m5.large')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • モデル データを保存するために、S3 に出力パスが設定されます。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

23 番目のセルの実行

  # Looks for the XGBoost image URI and builds an XGBoost container. Specify the repo_version depending on preference.
  container = get_image_uri(boto3.Session().region_name,
                            'xgboost', 
                            repo_version='1.0-1')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • トレーニング ジョブが開始されます。トレーニング タブで確認できます。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • しばらくすると (推定 3 分) 完了し、同じように表示されます。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

24 番目のコードセルの実行

  hyperparameters = {
        "max_depth":"5",                ## Maximum depth of a tree. Higher means more complex models but risk of overfitting.
        "eta":"0.2",                    ## Learning rate. Lower values make the learning process slower but more precise.
        "gamma":"4",                    ## Minimum loss reduction required to make a further partition on a leaf node. Controls the model’s complexity.
        "min_child_weight":"6",         ## Minimum sum of instance weight (hessian) needed in a child. Higher values prevent overfitting.
        "subsample":"0.7",              ## Fraction of training data used. Reduces overfitting by sampling part of the data. 
        "objective":"binary:logistic",  ## Specifies the learning task and corresponding objective. binary:logistic is for binary classification.
        "num_round":50                  ## Number of boosting rounds, essentially how many times the model is trained.
        }
  # A SageMaker estimator that calls the xgboost-container
  estimator = sagemaker.estimator.Estimator(image_uri=container,                  # Points to the XGBoost container we previously set up. This tells SageMaker which algorithm container to use.
                                          hyperparameters=hyperparameters,      # Passes the defined hyperparameters to the estimator. These are the settings that guide the training process.
                                          role=sagemaker.get_execution_role(),  # Specifies the IAM role that SageMaker assumes during the training job. This role allows access to AWS resources like S3.
                                          train_instance_count=1,               # Sets the number of training instances. Here, it’s using a single instance.
                                          train_instance_type='ml.m5.large',    # Specifies the type of instance to use for training. ml.m5.2xlarge is a general-purpose instance with a balance of compute, memory, and network resources.
                                          train_volume_size=5, # 5GB            # Sets the size of the storage volume attached to the training instance, in GB. Here, it’s 5 GB.
                                          output_path=output_path,              # Defines where the model artifacts and output of the training job will be saved in S3.
                                          train_use_spot_instances=True,        # Utilizes spot instances for training, which can be significantly cheaper than on-demand instances. Spot instances are spare EC2 capacity offered at a lower price.
                                          train_max_run=300,                    # Specifies the maximum runtime for the training job in seconds. Here, it's 300 seconds (5 minutes).
                                          train_max_wait=600)                   # Sets the maximum time to wait for the job to complete, including the time waiting for spot instances, in seconds. Here, it's 600 seconds (10 minutes).
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • エンドポイントは [推論] タブにデプロイされます。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

追加のコンソール観察:

  • 「推論」タブでのエンドポイント構成の作成。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • モデルの作成も [推論] タブで行われます。

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model


エンディングとクリーンアップ

  • VS Code で data_upload.ipynb に戻り、最後の 2 つのコード セルを実行して、S3 バケットのデータをローカル システムにダウンロードします。
  • フォルダーの名前は、download_bucket_content になります。 ダウンロードしたフォルダーのディレクトリ構造

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • ダウンロードしたファイルのログが出力セルに表示されます。これには、生の pretrained_sm.ipynb、final_dataset.csv、および sagemaker コード ファイルの実行データを含む「pretrained-algo」という名前のモデル出力フォルダーが含まれます。
  • 最後に、SageMaker インスタンス内に存在する pretrained_sm.ipynb に移動し、最後の 2 つのコード セルを実行します。 追加料金が発生しないように、S3 バケット内のエンドポイントとリソースは削除されます。
  • エンドポイントの削除
  estimator.fit({'train': s3_input_train,'validation': s3_input_test})
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

ClassiSage: Terraform IaC Automated AWS SageMaker based HDFS Log classification Model

  • S3 のクリア: (インスタンスを破棄するために必要)
  # Looks for the XGBoost image URI and builds an XGBoost container. Specify the repo_version depending on preference.
  container = get_image_uri(boto3.Session().region_name,
                            'xgboost', 
                            repo_version='1.0-1')
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー
  • プロジェクト ファイルの VS Code ターミナルに戻り、「terraform destroy --auto-approve」と入力/貼り付けます
  • 作成されたリソース インスタンスはすべて削除されます。

自動作成されたオブジェクト

ClassiSage/downloaded_bucket_content
ClassiSage/.terraform
ClassiSage/ml_ops/pycache
ClassiSage/.terraform.lock.hcl
ClassiSage/terraform.tfstate
ClassiSage/terraform.tfstate.backup

注:
AWS クラウドの S3 と HDFS ログ分類に SageMaker を使用し、IaC (インフラストラクチャ セットアップ自動化) に Terraform を使用したこの機械学習プロジェクトのアイデアと実装が気に入っていただけた場合は、GitHub でプロジェクト リポジトリをチェックアウトした後、この投稿に「いいね!」を付けてスターを付けることを検討してください。 .

以上がClassiSage: Terraform IaC 自動化された AWS SageMaker ベースの HDFS ログ分類モデルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!