3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JAWS-UG CLI] Amazon MachineLearning 入門 (2) 学習モデルの作成

Last updated at Posted at 2016-10-23

この記事について

JAWS-UG CLI専門支部 #67 MachineLearning入門で実施するハンズオン用の手順書です。

前提条件

必要な権限

作業にあたっては、以下の権限を有したIAMユーザもしくはIAMロールを利用してください。

  • MachineLearningのフルコントロール権限

0. 準備

0.1. リージョンを指定

コマンド
export AWS_DEFAULT_REGION="eu-west-1"

0.2. 資格情報を確認

コマンド
aws configure list

インスタンスプロファイルを設定したEC2インスタンスでアクセスキーを設定せずに実行した場合、以下のようになります。

結果
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************GMWA         iam-role
secret_key     ****************obFC         iam-role
    region                eu-west-1              env    AWS_DEFAULT_REGION

0.3. バージョン確認

コマンド
aws --version
結果
aws-cli/1.11.5 Python/2.7.12 Linux/4.4.19-29.55.amzn1.x86_64 botocore/1.4.62

0.4. バージョンアップ(必要に応じて)

コマンド
sudo pip install -U awscli

0.5. 変数の確認

コマンド
cat << ETX

    AWS_ID: ${AWS_ID}
    TRAINING_DATA_SOURCE_ID: ${TRAINING_DATA_SOURCE_ID}

ETX
結果

    AWS_ID: ************
    TRAINING_DATA_SOURCE_ID: training-cli-************

1. 学習モデルの作成

1.1. 学習モデルの作成

学習モデルIDの指定

コマンド
ML_MODEL_ID="ml-model-cli-${AWS_ID}-$(date +%Y%m%d%H)"

学習モデルの種類の指定

今回は二項分類を行います。

コマンド
ML_MODEL_TYPE="BINARY"

変数の確認

コマンド
cat << ETX

    ML_MODEL_ID: ${ML_MODEL_ID}
    ML_MODEL_TYPE: ${ML_MODEL_TYPE}
    TRAINING_DATA_SOURCE_ID: ${TRAINING_DATA_SOURCE_ID}

ETX
結果

    ML_MODEL_ID: ml-model-cli-************
    ML_MODEL_TYPE: BINARY
    TRAINING_DATA_SOURCE_ID: training-cli-************

学習モデルの作成

コマンド
ML_MODEL_ID=$(aws machinelearning create-ml-model \
    --ml-model-id ${ML_MODEL_ID} \
    --ml-model-type ${ML_MODEL_TYPE} \
    --training-data-source-id ${TRAINING_DATA_SOURCE_ID} \
    ) \
    echo ${ML_MODEL_ID}
結果
ml-model-cli-************

学習モデルの確認(1)

コマンド
aws machinelearning get-ml-model \
    --ml-model-id ${ML_MODEL_ID}

完了前には以下のような結果になります。

結果
{
    "Status": "INPROGRESS",
    "TrainingParameters": {
        "sgd.maxPasses": "10",
        "algorithm": "sgd",
        "sgd.l2RegularizationAmount": "1E-6",
        "sgd.shuffleType": "none",
        "sgd.maxMLModelSizeInBytes": "100000000",
        "sgd.l1RegularizationAmount": "0.0"
    },
    "MLModelType": "BINARY",
    "CreatedByIamUser": "arn:aws:iam::************:user/user01",
    "EndpointInfo": {
        "PeakRequestsPerSecond": 0,
        "EndpointStatus": "NONE"
    },
    "MLModelId": "ml-model-cli-************",
    "InputDataLocationS3": "s3://jawsug-cli-ml-************/banking.csv",
    "LastUpdatedAt": 1474609005.35,
    "TrainingDataSourceId": "training-cli-************",
    "StartedAt": 1474608877.295,
    "Message": "Current Step: TRAINING (1/1) Current Iteration: (10/10) 100%",
    "CreatedAt": 1474608873.692
}

完了後には以下のような結果になります。
作成時のログを確認することができます。

結果
{
    "Status": "COMPLETED",
    "SizeInBytes": 467150,
    "ComputeTime": 102000,
    "TrainingParameters": {
        "sgd.maxPasses": "10",
        "algorithm": "sgd",
        "sgd.l2RegularizationAmount": "1E-6",
        "sgd.shuffleType": "none",
        "sgd.maxMLModelSizeInBytes": "100000000",
        "sgd.l1RegularizationAmount": "0.0"
    },
    "MLModelType": "BINARY",
    "CreatedByIamUser": "arn:aws:iam::************:user/user01",
    "EndpointInfo": {
        "PeakRequestsPerSecond": 0,
        "EndpointStatus": "NONE"
    },
    "MLModelId": "ml-model-cli-************",
    "InputDataLocationS3": "s3://jawsug-cli-ml-************/banking.csv",
    "LastUpdatedAt": 1474609062.644,
    "TrainingDataSourceId": "training-cli-************",
    "StartedAt": 1474608877.295,
    "LogUri": "https://eml-prod-dub-emr.s3.amazonaws.com/************-pr-ml-model-cli/userlog/************-pr-ml-model-cli?AWSAccessKeyId=********************&Expires=1475213899&Signature=************************************",
    "CreatedAt": 1474608873.692,
    "FinishedAt": 1474609062.644
}

学習モデルの確認(2)

TrainingParametersは以下の公式ドキュメントを参照してください。

Training Parameters

毎度おなじみのDevelopers.IOでも解説されています。

AWS再入門 Amazon Machine Learning編

予測の精度や計算量に影響するパラメータです。

コマンド
aws machinelearning describe-ml-models
結果
{
    "Results": [
        {
            "Status": "COMPLETED",
            "SizeInBytes": 467150,
            "ComputeTime": 102000,
            "Algorithm": "sgd",
            "TrainingParameters": {
                "sgd.maxPasses": "10",
                "algorithm": "sgd",
                "sgd.l2RegularizationAmount": "1E-6",
                "sgd.shuffleType": "none",
                "sgd.maxMLModelSizeInBytes": "100000000",
                "sgd.l1RegularizationAmount": "0.0"
            },
            "MLModelType": "BINARY",
            "CreatedByIamUser": "arn:aws:iam::************:user/user01",
            "EndpointInfo": {
                "PeakRequestsPerSecond": 0,
                "EndpointStatus": "NONE"
            },
            "MLModelId": "ml-model-cli",
            "InputDataLocationS3": "s3://jawsug-cli-ml-************/banking.csv",
            "LastUpdatedAt": 1474609062.644,
            "TrainingDataSourceId": "training-cli",
            "StartedAt": 1474608877.295,
            "CreatedAt": 1474608873.692,
            "FinishedAt": 1474609062.644
        }
    ]
}

ログの確認

コマンド
ML_MODEL_LOG_URI=$(aws machinelearning get-ml-model \
    --ml-model-id ${ML_MODEL_ID} \
    --query LogUri \
    --output text) \
    && curl ${ML_MODEL_LOG_URI}
結果
(省略)

Recipeの確認

Recipeは、データソースを学習モデル作成時にどのような前処理をするか表現したものです。
AMLがモデルの作成時に推奨Recipeを作成してくれます。
自身でRecipeを作成することも可能です。

Data Transformations for Machine Learning

こちらもDevelopers.IOにまとめられてました。

Amazon Machine Learning Recipeまとめ

コマンド
aws machinelearning get-ml-model \
    --ml-model-id ${ML_MODEL_ID} \
    --verbose \
    --query "Recipe"
結果
"{\n  \"groups\" : {\n    \"NUMERIC_VARS_QB_50\" : \"group('cons_price_idx')\",\n    \"NUMERIC_VARS_QB_20\" : \"group('previous')\",\n    \"NUMERIC_VARS_QB_500\" : \"group('age','emp_var_rate','campaign')\",\n    \"NUMERIC_VARS_QB_10\" : \"group('duration','euribor3m','cons_conf_idx','pdays','nr_employed')\"\n  },\n  \"assignments\" : { },\n  \"outputs\" : [ \"ALL_BINARY\", \"ALL_CATEGORICAL\", \"quantile_bin(NUMERIC_VARS_QB_50,50)\", \"quantile_bin(NUMERIC_VARS_QB_20,20)\", \"quantile_bin(NUMERIC_VARS_QB_500,500)\", \"quantile_bin(NUMERIC_VARS_QB_10,10)\" ]\n}"

以上です。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?