CI/CD для машинного навчання
Ravi Bhadauria
Machine Learning Engineer
Налаштування гіперпараметрів
Тренування
Вільне з'єднання для незалежного тренування
Обидва завдання залежать від набору даних
# Вміст конфігурації hp
{
"n_estimators": [2, 4, 5],
"max_depth": [10, 20, 50],
"random_state": [1993]
}
# Вміст найкращих параметрів
{
"n_estimators": 5,
"max_depth": 20,
"random_state": 1993
}
# Завантажте гіперпараметри з JSON-файла with open("rfc_best_params.json", "r") as params_file: rfc_params = json.load(params_file)# Визначте та натренуйте модель model = RandomForestClassifier(**rfc_params) model.fit(X_train, y_train)
# Визначте модель і простір пошуку гіперпараметрів model = RandomForestClassifier() param_grid = json.load(open("hp_config.json", "r"))# Виконайте GridSearch з п'ятиразовою CV grid_search = GridSearchCV(model, param_grid, cv=5) grid_search.fit(X_train, y_train)# Отримайте найкращі гіперпараметри best_params = grid_search.best_params_ with open("rfc_best_params.json", "w") as outfile: json.dump(best_params, outfile)
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
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
Етапи можна запускати окремо dvc repro <stage_name>
Примусово запустіть етап налаштування гіперпараметрів dvc repro -f hp_tune
Тренування можна запустити командою dvc repro train
Обидва етапи тригерять крок препроцесингу як залежність
| 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-скрипті для налаштування гіперпараметрів
# Збережіть результати налаштування гіперпараметрів
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)
Маршрут налаштування гіперпараметрів
hp_tune/<some-string>dvc repro -f hp_tunecml pr create для створення нового PR на тренування з найкращими параметрамиРучний маршрут
train/<some-string>CI/CD для машинного навчання