Skip to main content

Amazon SageMaker

Amazon SageMaker คือแพลตฟอร์ม Machine Learning แบบครบวงจร (end-to-end) ที่ AWS ออกแบบมาเพื่อให้นักวิทยาศาสตร์ข้อมูล (Data Scientists) และนักพัฒนาสามารถสร้าง ฝึกสอน ปรับแต่ง และ deploy โมเดล ML ได้อย่างรวดเร็วในระดับ production โดยไม่ต้องจัดการโครงสร้างพื้นฐานเอง SageMaker รองรับทุกขั้นตอนของ ML lifecycle ตั้งแต่การเตรียมข้อมูล การ label ข้อมูล การฝึกสอนแบบ distributed จนถึงการ monitor โมเดลหลัง deploy

SageMaker เหมาะสำหรับทั้งองค์กรขนาดเล็กที่ต้องการ AutoML แบบไม่ต้องเขียนโค้ด และองค์กรขนาดใหญ่ที่ต้องการ MLOps pipeline เต็มรูปแบบ ครอบคลุมตั้งแต่ IDE สำหรับ ML, การสร้าง Feature Store, Model Registry ไปจนถึงการตรวจจับ bias และการ monitor drift ของโมเดลในระยะยาว

AWS Docs: https://docs.aws.amazon.com/sagemaker/latest/dg/whatis.html

สถาปัตยกรรม


ฟีเจอร์หลัก

SageMaker Studio

IDE แบบ web-based ที่ครบครันสำหรับงาน ML รองรับ JupyterLab notebooks, terminal, file browser และ Git integration ในที่เดียว ทีม Data Science สามารถทำงานร่วมกันได้ในสภาพแวดล้อมเดียวกัน พร้อม visual tools สำหรับทุกขั้นตอน

SageMaker Autopilot

AutoML ที่สร้างโมเดล ML อัตโนมัติจากข้อมูล CSV หรือ tabular data โดยไม่ต้องเขียนโค้ด ระบบวิเคราะห์ข้อมูล เลือก algorithm ที่เหมาะสม ทำ feature engineering และ hyperparameter tuning ให้อัตโนมัติ พร้อมอธิบาย leaderboard ของโมเดลที่ดีที่สุด

SageMaker Canvas

เครื่องมือ no-code ML สำหรับนักธุรกิจ ไม่ต้องมีความรู้ด้านโปรแกรมมิ่ง สามารถลาก-วางข้อมูลและสร้างโมเดลพยากรณ์ได้ด้วยตนเอง รองรับ time series forecasting, classification, regression และ image analysis

SageMaker Training Jobs

ระบบฝึกสอนโมเดลแบบ distributed บน managed infrastructure รองรับ framework หลัก เช่น TensorFlow, PyTorch, MXNet, Hugging Face และ custom containers สามารถ scale ไปยัง multi-GPU และ multi-node training ได้อัตโนมัติ

SageMaker Endpoints (Real-time Inference)

บริการ deploy โมเดลสำหรับ real-time inference พร้อม auto-scaling รองรับ A/B testing ระหว่างโมเดล multi-model endpoints เพื่อประหยัดค่าใช้จ่าย และ serverless inference สำหรับ workload ที่ไม่สม่ำเสมอ

SageMaker Batch Transform

ระบบ inference แบบ batch สำหรับ predict ข้อมูลจำนวนมากพร้อมกัน ไม่ต้องมี endpoint ที่ทำงานตลอดเวลา ประหยัดค่าใช้จ่ายสำหรับงานที่ไม่ต้องการ real-time response

SageMaker Pipelines (MLOps CI/CD)

เครื่องมือสร้าง automated ML pipeline ครอบคลุมขั้นตอน data processing, training, evaluation และ deployment สามารถ version control pipeline ได้ และ integrate กับ CI/CD tools ภายนอกเช่น GitHub Actions, Jenkins

SageMaker Feature Store

ที่เก็บ feature สำหรับ ML ที่รองรับทั้ง online store (latency ต่ำ สำหรับ real-time inference) และ offline store (สำหรับ training) ช่วยให้ทีม share feature ระหว่างกันและไม่ต้องคำนวณ feature ซ้ำ

SageMaker Model Registry

ระบบบริหารจัดการโมเดล ML แบบ centralized รองรับ versioning, metadata, approval workflow และ lineage tracking ทีม MLOps สามารถอนุมัติโมเดลก่อน deploy ไปยัง production ได้

SageMaker Model Monitor

ระบบ monitor โมเดลที่ deploy แล้ว ตรวจจับ data drift, model drift, feature attribution drift และ bias drift โดยอัตโนมัติ แจ้งเตือนเมื่อ performance ของโมเดลลดลง

SageMaker Clarify

เครื่องมือตรวจจับ bias ในข้อมูลและโมเดล และสร้าง explainability report (SHAP values) อธิบายว่า feature ใดมีผลต่อการตัดสินใจของโมเดลมากที่สุด รองรับทั้ง training time และ inference time

SageMaker Ground Truth

บริการ data labeling สำหรับสร้าง training dataset คุณภาพสูง ใช้ active learning ช่วยลดจำนวน label ที่ต้องทำด้วยคน สามารถส่งงาน labeling ให้กับ crowd workforce ของ Amazon Mechanical Turk หรือ private workforce ขององค์กร

SageMaker JumpStart

คลัง pre-trained model และ solution template สำหรับ Computer Vision, NLP, Tabular data และ Foundation Models เช่น Llama, Mistral, Falcon สามารถ deploy หรือ fine-tune ได้ด้วยคลิกเดียว

SageMaker HyperPod

ระบบ cluster สำหรับ distributed training ขนาดใหญ่ที่มี resilience สูง หากโหนดล้มเหลวระหว่างการ training ระบบจะรีสตาร์ทจาก checkpoint อัตโนมัติ เหมาะสำหรับการฝึกสอน LLM และ Foundation Models

SageMaker Experiments

ระบบ track และ compare ML experiments บันทึก parameters, metrics, artifacts ของแต่ละ run เพื่อวิเคราะห์ว่า configuration ใดให้ผลดีที่สุด

SageMaker Debugger

เครื่องมือ debug training jobs แบบ real-time ตรวจจับปัญหาเช่น vanishing gradients, overfitting, class imbalance และ resource bottleneck พร้อม built-in rules และ alerts


การติดตั้งและการตั้งค่า

เปิดใช้งานผ่าน AWS Console

  1. ไปที่ AWS Console > SageMaker
  2. คลิก "Open Studio" เพื่อสร้าง Studio Domain (ครั้งแรก)
  3. เลือก VPC, subnet และ execution role
  4. รอประมาณ 5-10 นาทีสำหรับการสร้าง domain

IAM Permissions ที่จำเป็น

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sagemaker:*",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}

ติดตั้ง SDK

pip install boto3 sagemaker

วิธีใช้งาน

ตัวอย่าง: สร้าง Training Job ด้วย Python SDK

import sagemaker
from sagemaker.sklearn.estimator import SKLearn

session = sagemaker.Session()
role = sagemaker.get_execution_role()

# กำหนด estimator
estimator = SKLearn(
entry_point="train.py",
role=role,
instance_type="ml.m5.xlarge",
framework_version="1.0-1",
output_path=f"s3://my-bucket/model-output/"
)

# เริ่ม training job
estimator.fit({"train": "s3://my-bucket/data/train.csv"})

ตัวอย่าง: Deploy โมเดลและ Real-time Inference

# Deploy endpoint
predictor = estimator.deploy(
initial_instance_count=1,
instance_type="ml.m5.large",
endpoint_name="my-model-endpoint"
)

# ส่ง request
import numpy as np
result = predictor.predict([[1.5, 2.3, 0.8, 1.1]])
print(result)

ตัวอย่าง: สร้าง ML Pipeline

from sagemaker.workflow.pipeline import Pipeline
from sagemaker.workflow.steps import TrainingStep, ProcessingStep

# สร้าง processing step
processing_step = ProcessingStep(
name="DataPreprocessing",
processor=processor,
inputs=[...],
outputs=[...]
)

# สร้าง training step
training_step = TrainingStep(
name="ModelTraining",
estimator=estimator,
inputs={"train": training_data}
)

# สร้าง pipeline
pipeline = Pipeline(
name="MyMLPipeline",
steps=[processing_step, training_step]
)
pipeline.upsert(role_arn=role)
pipeline.start()

ตัวอย่าง: Batch Transform

from sagemaker.transformer import Transformer

transformer = Transformer(
model_name="my-model",
instance_count=1,
instance_type="ml.m5.xlarge",
output_path="s3://my-bucket/batch-output/"
)

transformer.transform(
data="s3://my-bucket/batch-input/",
data_type="S3Prefix",
content_type="text/csv"
)
transformer.wait()

ราคา (ประมาณการในบาท)

ราคาคำนวณจาก 1 USD = 35 บาท (ราคา US East region โดยประมาณ)

Instance Types สำหรับ Studio/Training/Inference

InstancevCPURAMUSD/ชม.บาท/ชม.
ml.t3.medium24 GB$0.0464~1.62
ml.m5.large28 GB$0.134~4.69
ml.m5.xlarge416 GB$0.269~9.42
ml.m5.2xlarge832 GB$0.538~18.83
ml.g4dn.xlarge (GPU)416 GB$0.736~25.76
ml.p3.2xlarge (V100 GPU)861 GB$3.825~133.88
ml.p4d.24xlarge (A100x8)961152 GB$32.77~1,146.95

SageMaker Studio

  • IDE เองไม่มีค่าใช้จ่าย จ่ายเฉพาะค่า compute instance ที่ใช้งาน
  • Free tier: 250 ชั่วโมง ml.t3.medium (2 เดือนแรก)

Ground Truth Labeling

  • $0.08/object (~2.80 บาท/ชิ้น) สำหรับ public workforce
  • Private workforce: จ่ายแยกตาม Amazon Mechanical Turk

ตัวอย่างค่าใช้จ่ายรายเดือน

  • ทีมเล็ก (Studio 8 ชม./วัน + 1 endpoint ml.m5.large): ~3,400 บาท/เดือน
  • Training GPU (ml.p3.2xlarge 40 ชม./เดือน): ~5,355 บาท/เดือน
  • Production endpoint (ml.m5.xlarge 24/7): ~6,769 บาท/เดือน

เหมาะสำหรับ

  • ทีม Data Science ที่ต้องการสภาพแวดล้อม ML แบบครบวงจรในที่เดียว
  • องค์กรที่ต้องการ MLOps pipeline มาตรฐานพร้อม versioning และ approval workflow
  • บริษัทที่ต้องการ AutoML โดยไม่ต้องมีผู้เชี่ยวชาญด้าน ML (SageMaker Autopilot / Canvas)
  • ทีมที่ต้องการ fine-tune Foundation Models หรือ LLM บน private data
  • องค์กรที่มีข้อกำหนด compliance และต้องการ monitor bias ของโมเดล
  • นักพัฒนาที่ต้องการ deploy ML model โดยไม่ต้องจัดการ Kubernetes หรือ server

ใช้ร่วมกับ AWS Services

  • Amazon S3 — เก็บ training data, model artifacts และ output
  • AWS Lambda — post-processing หลัง inference หรือ trigger pipeline
  • Amazon ECR — เก็บ custom Docker containers สำหรับ training/serving
  • AWS Glue — ETL pipeline สำหรับเตรียมข้อมูลก่อน training
  • Amazon Redshift — ดึงข้อมูลขนาดใหญ่มา train โมเดล
  • Amazon CloudWatch — monitor metrics, logs และ alerts
  • AWS Step Functions — orchestrate ML workflow ที่ซับซ้อน
  • Amazon Bedrock — ใช้ร่วมกับ Foundation Models บน Bedrock
  • AWS IAM — จัดการ permissions และ roles
  • Amazon VPC — isolate training และ inference workloads

Use Case ตัวอย่าง

1. ระบบแนะนำสินค้าสำหรับ E-Commerce ไทย

บริษัท e-commerce ขนาดใหญ่ใช้ SageMaker ฝึกสอน recommendation engine จากข้อมูลพฤติกรรมลูกค้ากว่า 10 ล้านรายการ ทีมใช้ SageMaker Feature Store เก็บ feature เช่น purchase history, browsing pattern และ seasonal preference SageMaker Pipelines สร้าง automated workflow ที่ retrain โมเดลใหม่ทุกสัปดาห์โดยอัตโนมัติ โมเดลถูก deploy บน SageMaker Endpoint พร้อม auto-scaling รับ traffic สูงสุด 50,000 request/วินาทีช่วง 11.11 sale ผลลัพธ์คือ CTR (Click-through rate) เพิ่มขึ้น 35% และยอดขายเพิ่ม 25%

2. ระบบตรวจจับการทุจริตทางการเงินในธนาคารไทย

ธนาคารพาณิชย์ใช้ SageMaker ฝึกสอนโมเดล fraud detection จากธุรกรรมกว่า 100 ล้านรายการต่อปี ใช้ ml.p3.2xlarge สำหรับ distributed training ด้วย XGBoost และ Deep Learning SageMaker Clarify ช่วยตรวจสอบ bias และ SageMaker Model Monitor ตรวจจับ data drift เมื่อพฤติกรรมการใช้บัตรเปลี่ยนไป ระบบตรวจจับการทุจริตได้ real-time ภายใน 50ms ลดความเสียหายจาก fraud ได้ 60%

3. Quality Control ด้วย Computer Vision ในโรงงานอุตสาหกรรม

โรงงานผลิตชิ้นส่วนอิเล็กทรอนิกส์ใช้ SageMaker Ground Truth สร้าง labeled dataset จากภาพชิ้นส่วนที่มีข้อบกพร่องกว่า 50,000 ภาพ จากนั้นใช้ SageMaker JumpStart โหลด pre-trained ResNet model มา fine-tune บน custom dataset ด้วย ml.g4dn.xlarge โมเดลถูก deploy บน edge device ที่ไลน์การผลิตผ่าน SageMaker Edge Manager ตรวจสอบชิ้นส่วนได้ 200 ชิ้น/นาที ลดของเสียที่ผ่านไปยังลูกค้าได้ 95%