DVC के साथ Hyperparameter Tuning

Machine Learning के लिए CI/CD

Ravi Bhadauria

Machine Learning Engineer

Hyperparameter tuning वर्कफ़्लो

  • Hyperparameter tuning

    • इनपुट: पैरामीटर स्वीप रेंज
    • आउटपुट: सर्वश्रेष्ठ पैरामीटर
  • Training

    • इनपुट: सर्वश्रेष्ठ पैरामीटर
    • आउटपुट: मेट्रिक्स और प्लॉट (पहले कवर किया)
  • स्वतंत्र training के लिए ढीला coupling

    • Hyperparameter tuning पर्याप्त है, आवश्यक नहीं
  • दोनों जॉब्स डेटासेट पर निर्भर हैं

# 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
}
Machine Learning के लिए CI/CD

Training कोड में बदलाव

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)
Machine Learning के लिए CI/CD

GridSearch के साथ Hyperparameter Tuning

# 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)
Machine Learning के लिए CI/CD

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: # सर्वश्रेष्ठ पैरामीटर ट्रैक नहीं कर रहे
      - hp_tuning_results.md:
          cache: false
Training
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # सर्वश्रेष्ठ पैरामीटर
    - train.py
    metrics:
      - metrics.json:
          cache: false
Machine Learning के लिए CI/CD

अलग-अलग स्टेज ट्रिगर करना

  • स्टेजेज़ को अलग से ट्रिगर कर सकते हैं dvc repro <stage_name>

  • Hyperparameter tuning स्टेज को फोर्स-रन करें dvc repro -f hp_tune

    • सुनिश्चित करता है कि best parameter फ़ाइल अपडेट होगी
  • Training को dvc repro train से चला सकते हैं

  • दोनों स्टेजेज़ प्रीप्रोसेसिंग स्टेप को dependency के रूप में ट्रिगर करते हैं

Machine Learning के लिए CI/CD

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)
Machine Learning के लिए CI/CD

सारांश

  • Hyperparameter tuning रूट

    • ब्रांच नाम hp_tune/<some-string>
    • सर्च कॉन्फ़िग में बदलाव करें
    • PR मैन्युअली खोलें
      • DVC पाइपलाइन को फोर्स-रन करता है dvc repro -f hp_tune
      • cml pr create से सर्वश्रेष्ठ पैरामीटर के साथ नया training PR बनाता है
    • मॉडल training जॉब शुरू करने के लिए training PR पर एक कमिट फोर्स-पुश करें
  • मैन्युअल रूट

    • ब्रांच नाम train/<some-string>
    • best parameters फ़ाइल एडिट कर के कमिट करें
    • मॉडल training जॉब शुरू करने के लिए PR मैन्युअली खोलें
Machine Learning के लिए CI/CD

अभ्यास करते हैं!

Machine Learning के लिए CI/CD

Preparing Video For Download...