Điều chỉnh siêu tham số với DVC

CI/CD cho Machine Learning

Ravi Bhadauria

Machine Learning Engineer

Quy trình điều chỉnh siêu tham số

  • Điều chỉnh siêu tham số

    • Đầu vào: Khoảng quét tham số
    • Đầu ra: Tham số tốt nhất
  • Huấn luyện

    • Đầu vào: Tham số tốt nhất
    • Đầu ra: Chỉ số và biểu đồ (đã học)
  • Ghép lỏng để huấn luyện độc lập

    • Điều chỉnh siêu tham số là đủ nhưng không bắt buộc
  • Cả hai job phụ thuộc vào dữ liệu

# 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 cho Machine Learning

Thay đổi mã huấn luyện

Thay đổi trong mã huấn luyện (Python)
# 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 cho Machine Learning

Điều chỉnh siêu tham số với 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 cho Machine Learning

Thay đổi DVC YAML

Điều chỉnh siêu tham số
stages:
  preprocess: ...
  train: ...
  hp_tune:
    cmd: python hp_tuning.py
    deps:
    - processed_dataset/weather.csv
    - hp_config.json
    - hp_tuning.py
    outs: # Không theo dõi tham số tốt nhất
      - hp_tuning_results.md:
          cache: false
Huấn luyện
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # Tham số tốt nhất
    - train.py
    metrics:
      - metrics.json:
          cache: false
CI/CD cho Machine Learning

Kích hoạt từng stage

  • Có thể chạy từng stage riêng dvc repro <stage_name>

  • Ép chạy stage điều chỉnh siêu tham số dvc repro -f hp_tune

    • Đảm bảo cập nhật tệp tham số tốt nhất
  • Huấn luyện: dvc repro train

  • Cả hai stage sẽ kích hoạt bước tiền xử lý do phụ thuộc

CI/CD cho Machine Learning

Kết quả chạy điều chỉnh siêu tham số

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

Thay đổi trong script Python điều chỉnh siêu tham số

# 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 cho Machine Learning

Tóm tắt

  • Quy trình điều chỉnh siêu tham số

    • Tên nhánh hp_tune/<some-string>
    • Chỉnh cấu hình tìm kiếm
    • Mở PR thủ công
      • Ép chạy pipeline DVC dvc repro -f hp_tune
      • Dùng cml pr create để tạo PR huấn luyện mới với tham số tốt nhất
    • Force push một commit vào PR huấn luyện để khởi động job huấn luyện
  • Quy trình thủ công

    • Tên nhánh train/<some-string>
    • Sửa tệp tham số tốt nhất và commit
    • Mở PR thủ công để khởi động job huấn luyện
CI/CD cho Machine Learning

Ayo berlatih!

CI/CD cho Machine Learning

Preparing Video For Download...