Skip to content

Commit

Permalink
estimator training job practice bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gracechung-sw committed Jul 3, 2024
1 parent 891d9c8 commit 6dc7d54
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,8 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import boto3\n",
"import json\n",
"import sagemaker\n",
"from sagemaker.session import Session\n",
"from sagemaker import get_execution_role\n",
"from sagemaker.experiments.run import Run\n",
"from sagemaker.utils import unique_name_from_base"
"import sagemaker"
]
},
{
Expand Down Expand Up @@ -121,8 +115,8 @@
"default_bucket = sagemaker_session.default_bucket()\n",
"\n",
"# sagemaker를 사용\n",
"sm = sagemaker_session.client(\"sagemaker\")\n",
"region = sagemaker_session.region_name"
"sm = boto_session.client(\"sagemaker\")\n",
"region = boto_session.region_name"
]
},
{
Expand All @@ -141,82 +135,18 @@
},
{
"cell_type": "markdown",
"id": "8063c278",
"id": "2f7b1a5e",
"metadata": {},
"source": [
"## Experiment을 생성하고 training job을 실행하기\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0303fbea-268b-4766-abf7-05a3c6679722",
"metadata": {},
"outputs": [],
"source": [
"### Prepare the training script\n",
"\n",
"from sagemaker.tensorflow.estimator import TensorFlow\n",
"from sagemaker.experiments.run import Run"
"Here we use a SageMaker Training job to train the model on a remote instance.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b94f650f-30b1-4c96-a321-a2a4cdb8be9c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"experiment_name = \"tensorflow-estimator-experiment-practice\"\n",
"\n",
"batch_size = 256\n",
"epochs = 5\n",
"dropout = 0.1\n",
"\n",
"with Run(\n",
" experiment_name=experiment_name,\n",
" sagemaker_session=sagemaker_session,\n",
") as run:\n",
" run.log_parameter(\"batch_size\", batch_size)\n",
" run.log_parameter(\"epochs\", epochs)\n",
" run.log_parameter(\"dropout\", dropout)\n",
"\n",
" # Estimator를 사용하지 않는다면, 여기에 ../2.experiment_training/keras_experiment.ipynb의 def get_model(dropout=0.5): 처럼 model training 코드를 작성하겠지만\n",
" # 여기서는 Estimator의 from sagemaker.tensorflow.estimator import TensorFlow를 사용해서\n",
" # 추상화된 컨데이너를 사용하도록 코드를 작성합니다.\n",
" est = TensorFlow( # TensorFlow Estimator. https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator\n",
" entry_point=\"./script/train.py\",\n",
" role=role,\n",
" model_dir=False,\n",
" # 주의: hyperparameters가 entry_point의 script(\"./script/train.py\")에서 사용하는 인자와 일치해야 합니다.\n",
" # Estimator가 \"./script/train.py\" 에서 이 hyperparameters를 인자로 자신의 script에 넣어주기 때문입니다.\n",
" # def parse_args():\n",
" # parser = argparse.ArgumentParser()\n",
"\n",
" # parser.add_argument(\"--epochs\", type=int, default=1)\n",
" # parser.add_argument(\"--batch_size\", type=int, default=64)\n",
" # parser.add_argument(\"--dropout\", type=float, default=0.01)\n",
"\n",
" # return parser.parse_known_args()\n",
" hyperparameters={\"epochs\": epochs,\n",
" \"batch_size\": batch_size, \"dropout\": dropout},\n",
" framework_version=\"2.8\", # tensorflow framework version\n",
" py_version=\"py39\", # python version\n",
" instance_type=\"ml.m5.xlarge\",\n",
" instance_count=1,\n",
" keep_alive_period_in_seconds=3600,\n",
" environment={\"REGION\": region},\n",
" )\n",
"\n",
" # Training Job 시작\n",
" # .fit을 호출하면, AWS SageMaker는 자동으로 Estimator를 기반으로 필요한 인프라를 provisioning하고 training job이 생성되어 학습을 실행시킨다.\n",
" est.fit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce9b36e1-9d66-456d-9fe1-7bcc2d8c19e2",
"id": "7ecee201",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -226,14 +156,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bb9ad785-58cc-4adb-b258-fdd76fc40cff",
"id": "9c8a285a",
"metadata": {},
"outputs": [],
"source": [
"# 이렇게 하면 script/train.py 파일이 생성되고, 이 아래에 있는 코드들이 이 파일에 작성된다.\n",
"\n",
"%%writefile ./script/train.py\n",
"\n",
"# 이렇게 하면 script/train.py 파일이 생성되고, 이 아래에 있는 코드들이 이 파일에 작성된다.\n",
"\n",
"import os\n",
"\n",
"os.system(\"pip install -U sagemaker\")\n",
Expand Down Expand Up @@ -264,18 +194,17 @@
" return parser.parse_known_args()\n",
"\n",
"\n",
"# Define the Keras callback to log metrics to the run\n",
"# The Keras Callback class provides a method on_epoch_end which emits metrics at the end of each epoch.\n",
"# All emitted metrics will be logged in the run passed to the callback.\n",
"class ExperimentCallback(keras.callbacks.Callback):\n",
" \"\"\" \"\"\"\n",
"\n",
" def __init__(self, run, model, x_test, y_test):\n",
" \"\"\"Save params in constructor\"\"\"\n",
" def __init__(self, run, trained_model, x_test, y_test):\n",
" self.run = run\n",
" self.model = model\n",
" self.trained_model = trained_model\n",
" self.x_test = x_test\n",
" self.y_test = y_test\n",
"\n",
" def on_epoch_end(self, epoch, logs=None):\n",
" \"\"\" \"\"\"\n",
" keys = list(logs.keys())\n",
" for key in keys:\n",
" self.run.log_metric(name=key, value=logs[key], step=epoch)\n",
Expand Down Expand Up @@ -393,6 +322,80 @@
" main()"
]
},
{
"cell_type": "markdown",
"id": "8063c278",
"metadata": {},
"source": [
"## Experiment을 생성하고 training job을 실행하기\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0303fbea-268b-4766-abf7-05a3c6679722",
"metadata": {},
"outputs": [],
"source": [
"\n",
"from sagemaker.tensorflow.estimator import TensorFlow\n",
"from sagemaker.experiments.run import Run"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b94f650f-30b1-4c96-a321-a2a4cdb8be9c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"experiment_name = \"tensorflow-estimator-experiment-practice\"\n",
"\n",
"batch_size = 256\n",
"epochs = 5\n",
"dropout = 0.1\n",
"\n",
"with Run(\n",
" experiment_name=experiment_name,\n",
" sagemaker_session=sagemaker_session,\n",
") as run:\n",
" run.log_parameter(\"batch_size\", batch_size)\n",
" run.log_parameter(\"epochs\", epochs)\n",
" run.log_parameter(\"dropout\", dropout)\n",
"\n",
" # Estimator를 사용하지 않는다면, 여기에 ../2.experiment_training/keras_experiment.ipynb의 def get_model(dropout=0.5): 처럼 model training 코드를 작성하겠지만\n",
" # 여기서는 Estimator의 from sagemaker.tensorflow.estimator import TensorFlow를 사용해서\n",
" # 추상화된 컨데이너를 사용하도록 코드를 작성합니다.\n",
" est = TensorFlow( # TensorFlow Estimator. https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-estimator\n",
" entry_point=\"./script/train.py\",\n",
" role=role,\n",
" model_dir=False,\n",
" # 주의: hyperparameters가 entry_point의 script(\"./script/train.py\")에서 사용하는 인자와 일치해야 합니다.\n",
" # Estimator가 \"./script/train.py\" 에서 이 hyperparameters를 인자로 자신의 script에 넣어주기 때문입니다.\n",
" # def parse_args():\n",
" # parser = argparse.ArgumentParser()\n",
"\n",
" # parser.add_argument(\"--epochs\", type=int, default=1)\n",
" # parser.add_argument(\"--batch_size\", type=int, default=64)\n",
" # parser.add_argument(\"--dropout\", type=float, default=0.01)\n",
"\n",
" # return parser.parse_known_args()\n",
" hyperparameters={\"epochs\": epochs,\n",
" \"batch_size\": batch_size, \"dropout\": dropout},\n",
" framework_version=\"2.8\", # tensorflow framework version\n",
" py_version=\"py39\", # python version\n",
" instance_type=\"ml.m5.xlarge\",\n",
" instance_count=1,\n",
" keep_alive_period_in_seconds=3600,\n",
" environment={\"REGION\": region},\n",
" )\n",
"\n",
" # Training Job 시작\n",
" # .fit을 호출하면, AWS SageMaker는 자동으로 Estimator를 기반으로 필요한 인프라를 provisioning하고 training job이 생성되어 학습을 실행시킨다.\n",
" est.fit()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

1 comment on commit 6dc7d54

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MODEL METRICS

Training variance explained: 33.0%
Test variance explained: 32.0%

Data viz

feature_importance
residuals

Please sign in to comment.