Hyperparameter Tuning ด้วย DVC

CI/CD สำหรับ Machine Learning

Ravi Bhadauria

Machine Learning Engineer

ขั้นตอนการทำ Hyperparameter Tuning

  • Hyperparameter tuning

    • Input: ช่วงค่าพารามิเตอร์
    • Output: พารามิเตอร์ที่ดีที่สุด
  • การเทรน

    • Input: พารามิเตอร์ที่ดีที่สุด
    • Output: เมตริกและกราฟ (ครอบคลุมแล้ว)
  • การเชื่อมต่อแบบหลวมเพื่อเทรนอิสระจากกัน

    • Hyperparameter tuning เป็นเงื่อนไขเพียงพอแต่ไม่จำเป็น
  • ทั้งสอง job ขึ้นอยู่กับชุดข้อมูล

# Contents of hp configuration
{
    "n_estimators": [2, 4, 5],
    "max_depth": [10, 20, 50],
    "random_state": [1993]
}
# Contents of best parameters
{
    "n_estimators": 5,
    "max_depth": 20,
    "random_state": 1993
}
CI/CD สำหรับ Machine Learning

การเปลี่ยนแปลงโค้ดสำหรับการเทรน

การเปลี่ยนแปลงในสคริปต์ Python สำหรับ Hyperparameter Tuning
# Load hyperparameters from the JSON file
with open("rfc_best_params.json", "r") as params_file:
  rfc_params = json.load(params_file)

# Define and train model model = RandomForestClassifier(**rfc_params) model.fit(X_train, y_train)
CI/CD สำหรับ Machine Learning

Hyperparameter Tuning ด้วย GridSearch

# Define the model and hyperparameter search space
model = RandomForestClassifier()
param_grid = json.load(open("hp_config.json", "r"))

# Perform GridSearch with five fold CV grid_search = GridSearchCV(model, param_grid, cv=5) grid_search.fit(X_train, y_train)
# Get the best hyperparameters best_params = grid_search.best_params_ with open("rfc_best_params.json", "w") as outfile: json.dump(best_params, outfile)
CI/CD สำหรับ Machine Learning

การเปลี่ยนแปลงใน DVC YAML

Hyperparameter Tuning
stages:
  preprocess: ...
  train: ...
  hp_tune:
    cmd: python hp_tuning.py
    deps:
    - processed_dataset/weather.csv
    - hp_config.json
    - hp_tuning.py
    outs: # Not tracking best parameters
      - hp_tuning_results.md:
          cache: false
การเทรน
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # Best parameters
    - train.py
    metrics:
      - metrics.json:
          cache: false
CI/CD สำหรับ Machine Learning

การสั่งรัน Stage แยกเดี่ยว

  • สั่งรัน stage ได้อิสระด้วย dvc repro <stage_name>

  • บังคับรัน stage ของ hyperparameter tuning ด้วย dvc repro -f hp_tune

    • รับประกันว่าไฟล์พารามิเตอร์ที่ดีที่สุดจะอัปเดต
  • รันการเทรนได้ด้วย dvc repro train

  • ทั้งสอง stage กระตุ้น preprocessing step ในฐานะ dependency

CI/CD สำหรับ Machine Learning

ผลลัพธ์จากการรัน Hyperparameter

mean_test_score std_test_score max_depth n_estimators random_state
0.999733 0.000413118 20 5 1993
0.999307 0.000574418 50 5 1993
0.99888 0.000617378 10 5 1993
0.997813 0.00117333 10 4 1993

การเปลี่ยนแปลงในสคริปต์ Python สำหรับ Hyperparameter Tuning

# Save the results of hyperparameter tuning
cv_results = pd.DataFrame(grid_search.cv_results_)
markdown_table = cv_results.to_markdown(index=False)
with open("hp_tuning_results.md", "w") as markdown_file:
  markdown_file.write(markdown_table)
CI/CD สำหรับ Machine Learning

สรุป

  • เส้นทาง Hyperparameter tuning

    • ชื่อ branch: hp_tune/<some-string>
    • แก้ไข search configuration
    • เปิด PR ด้วยตนเอง
      • บังคับรัน DVC pipeline ด้วย dvc repro -f hp_tune
      • ใช้ cml pr create สร้าง training PR ใหม่พร้อมพารามิเตอร์ที่ดีที่สุด
    • Force push commit ไปยัง training PR เพื่อเริ่มงานเทรนโมเดล
  • เส้นทางแบบ Manual

    • ชื่อ branch: train/<some-string>
    • แก้ไขไฟล์พารามิเตอร์ที่ดีที่สุดและ commit
    • เปิด PR ด้วยตนเองเพื่อเริ่มงานเทรนโมเดล
CI/CD สำหรับ Machine Learning

มาฝึกกันเถอะ!

CI/CD สำหรับ Machine Learning

Preparing Video For Download...