모델 레지스트리: 스테이징, 운영 및 보관
학습자는 MLflow Model Registry에 모델 버전을 등록하고 Staging을 거쳐 Production으로 전환하며, Python API로 승격 작업 흐름을 스크립트로 작성합니다.
모델 레지스트리: 스테이징, 운영 및 보관은(는) CoddyKit의 무료 Machine Learning Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Machine Learning Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What Is a Model Registry?
A model registry is a centralised catalogue that stores versioned trained models with their metadata. Instead of managing model files scattered across file systems, a registry provides a single source of truth with named versions, lifecycle stages (Staging, Production, Archived), and searchable annotations. The MLflow Model Registry is the most widely used open-source solution and integrates directly with the MLflow tracking server.
Registering a Model from a Run
After training, register the model by linking it to an existing MLflow run artifact. You can register directly during logging using the registered_model_name argument, or after the fact using the MLflow client. The registry creates a named model entry (e.g., 'SentimentClassifier') and assigns it Version 1. Subsequent registrations of the same model name automatically increment the version number.
import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
# Option 1: Register during logging
with mlflow.start_run():
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# clf.fit(X_train, y_train)
mlflow.sklearn.log_model(
sk_model=clf,
artifact_path='model',
registered_model_name='SentimentClassifier' # auto-registers
)
print('Model registered as SentimentClassifier v1')Using the MLflow Client for Registry Operations
The MlflowClient Python API gives programmatic control over the registry. Use it to register models from existing run artifacts, transition stages, and add descriptions — all from scripts rather than the UI. This is essential for automated CI/CD pipelines where a new model should be promoted only after passing evaluation tests, without requiring manual UI interaction from a data scientist.
from mlflow.tracking import MlflowClient
client = MlflowClient(tracking_uri='http://localhost:5000')
# Option 2: Register from an existing run artifact
run_id = 'abc123def456' # get this from mlflow.last_active_run().info.run_id
model_uri = f'runs:/{run_id}/model'
model_version = mlflow.register_model(
model_uri=model_uri,
name='SentimentClassifier'
)
print('Version:', model_version.version)
print('Status:', model_version.status) # PENDING_REGISTRATION -> READYLifecycle Stages: None, Staging, Production, Archived
Every model version in the registry has a lifecycle stage. New versions start at None. After automated evaluation passes, promote to Staging for integration testing. After Staging passes, promote to Production — the version serving live traffic. When a newer version supersedes it, move it to Archived to preserve history without deleting it. Only one version should be in Production at a time per model name.
from mlflow.tracking import MlflowClient
client = MlflowClient()
# Transition version 1 to Staging
client.transition_model_version_stage(
name='SentimentClassifier',
version='1',
stage='Staging',
archive_existing_versions=False
)
print('Version 1 -> Staging')
# After testing, promote to Production (archives previous Production)
client.transition_model_version_stage(
name='SentimentClassifier',
version='1',
stage='Production',
archive_existing_versions=True # auto-archives old Production
)
print('Version 1 -> Production')Adding Descriptions and Tags to Versions
Model versions should carry human-readable metadata. Add a description explaining what changed in this version: training data, preprocessing, or algorithm. Add tags for quick filtering, such as the deployment environment or dataset version. Good metadata makes it possible to answer audit questions ('What model was serving in February?') months after deployment without digging through git history.
from mlflow.tracking import MlflowClient
client = MlflowClient()
# Add description to version
client.update_model_version(
name='SentimentClassifier',
version='1',
description=('RandomForest trained on IMDB v2 (50k reviews). '
'Test accuracy 0.924, F1 0.921. '
'Replaces rule-based baseline.')
)
# Add tags for filtering and search
client.set_model_version_tag(
name='SentimentClassifier',
version='1',
key='dataset',
value='imdb_v2'
)
client.set_model_version_tag('SentimentClassifier', '1', 'algorithm', 'random_forest')
print('Description and tags added.')Loading the Production Model for Inference
In your inference service, always load the model by stage alias ('Production') rather than a hardcoded version number. This way, when you promote a new version to Production, the inference service automatically uses the new model on the next load without code changes. The models:/ URI scheme is a powerful MLflow convention for stage-based loading.
import mlflow.sklearn
# Load the current Production model by stage
model_name = 'SentimentClassifier'
stage = 'Production'
model_uri = f'models:/{model_name}/{stage}'
production_model = mlflow.sklearn.load_model(model_uri)
print('Loaded model from:', model_uri)
# Or load a specific version
version_uri = f'models:/{model_name}/1'
v1_model = mlflow.sklearn.load_model(version_uri)
print('Loaded specific version 1')
# Make predictions
# predictions = production_model.predict(X_new)Searching and Comparing Versions
As more versions accumulate, use the client's search methods to filter by stage, tags, or metrics. Compare version performance programmatically: fetch the run ID associated with each version, query the run's metrics, and find the best-performing version to promote. This automation prevents manual errors and ensures promotion decisions are based on objective metric comparisons rather than guesswork.
from mlflow.tracking import MlflowClient
client = MlflowClient()
# List all versions of a model
versions = client.search_model_versions("name='SentimentClassifier'")
for v in versions:
print(f'Version {v.version}: stage={v.current_stage}, run_id={v.run_id[:8]}')
# Get the metric from the associated training run
for v in versions:
run = client.get_run(v.run_id)
acc = run.data.metrics.get('test_accuracy', 'N/A')
print(f' Version {v.version} accuracy: {acc}')Automated Promotion Script
A retraining pipeline should automatically promote a new model to Staging only if it outperforms the current Production model on a held-out evaluation set. This champion/challenger pattern prevents regression: the Production model is the champion, and the new model is the challenger. The challenger is promoted only if it beats the champion on the agreed metric (e.g., F1 on the validation set).
from mlflow.tracking import MlflowClient
import mlflow.sklearn
client = MlflowClient()
def get_metric(run_id, metric_name):
return client.get_run(run_id).data.metrics.get(metric_name, 0)
def promote_if_better(model_name, challenger_version, metric='test_f1'):
# Get current production version
prod_versions = client.get_latest_versions(model_name, stages=['Production'])
if not prod_versions:
print('No production model found -- promoting challenger directly.')
client.transition_model_version_stage(model_name, challenger_version, 'Production')
return
prod_v = prod_versions[0]
prod_score = get_metric(prod_v.run_id, metric)
chall_run_id = client.get_model_version(model_name, challenger_version).run_id
chall_score = get_metric(chall_run_id, metric)
print(f'Champion {metric}: {prod_score:.4f} Challenger: {chall_score:.4f}')
if chall_score > prod_score:
client.transition_model_version_stage(model_name, challenger_version,
'Production', archive_existing_versions=True)
print('Challenger promoted to Production!')
else:
print('Champion retained.')Archiving Superseded Models
When a new version enters Production, old Production versions should move to Archived rather than being deleted. Archived models are excluded from get_latest_versions queries but remain downloadable for audit, rollback, or future comparison. Never delete model versions in a regulated industry: financial services and healthcare require full version history for compliance audits.
from mlflow.tracking import MlflowClient
client = MlflowClient()
# Manually archive a specific version
client.transition_model_version_stage(
name='SentimentClassifier',
version='1',
stage='Archived'
)
print('Version 1 archived.')
# List only archived versions
archived = client.search_model_versions(
"name='SentimentClassifier' and stage='Archived'"
)
for v in archived:
print(f'Archived: v{v.version} created {v.creation_timestamp}')Model Serving with mlflow models serve
MLflow can serve any registered model as a local REST API with a single command. The endpoint accepts JSON payloads and returns predictions. This is useful for rapid prototyping and integration testing before deploying to a cloud platform. For production, use container-based serving (Docker + FastAPI or MLflow's Docker export) for better scalability and monitoring.
# Serve the Production model as a local REST endpoint
# mlflow models serve -m 'models:/SentimentClassifier/Production' --port 8080
# Then call it with curl:
# curl -X POST http://localhost:8080/invocations \
# -H 'Content-Type: application/json' \
# -d '{"dataframe_records": [{"feature1": 0.5, "feature2": 1.2}]}'
# Or with Python requests:
import requests
data = {'dataframe_records': [{'feature1': 0.5, 'feature2': 1.2}]}
response = requests.post('http://localhost:8080/invocations', json=data)
print('Prediction:', response.json())Registry Webhooks and Notifications
MLflow Registry (in Databricks and some enterprise setups) supports webhooks that fire HTTP callbacks when model transitions occur. For open-source MLflow, simulate webhooks by polling the registry in a cron job. Common automation patterns include: sending a Slack notification when a model enters Staging, triggering integration tests when a model reaches Staging, and alerting the team when Production is updated.
# Polling script (run on a schedule, e.g., cron every 5 minutes)
from mlflow.tracking import MlflowClient
import json
import os
client = MlflowClient()
state_file = '/tmp/model_registry_state.json'
def load_state():
if os.path.exists(state_file):
return json.load(open(state_file))
return {}
def save_state(state):
json.dump(state, open(state_file, 'w'))
state = load_state()
prod = client.get_latest_versions('SentimentClassifier', stages=['Production'])
if prod:
current_prod = prod[0].version
if state.get('production_version') != current_prod:
print(f'ALERT: Production changed to version {current_prod}')
# send_slack_notification(current_prod)
state['production_version'] = current_prod
save_state(state)Quick Check
Test your understanding of Machine Learning with Python concepts from this lesson.
Lesson Recap
In this lesson you learned: the MLflow Model Registry provides versioned model storage with lifecycle stages: None, Staging, Production, and Archived, load models by stage alias ('Production') rather than version number to enable seamless updates without code changes, and automated promotion scripts implement the champion/challenger pattern to prevent production regressions. Next up we build a GitHub Actions workflow that automatically retrains and promotes a model when new data arrives.
자주 묻는 질문
“모델 레지스트리: 스테이징, 운영 및 보관” 강의는 무료인가요?
네 — “모델 레지스트리: 스테이징, 운영 및 보관” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Machine Learning Academy 강의 전체를 잠금 해제할 수 있습니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“모델 레지스트리: 스테이징, 운영 및 보관”에서 뭘 배우나요?
학습자는 MLflow Model Registry에 모델 버전을 등록하고 Staging을 거쳐 Production으로 전환하며, Python API로 승격 작업 흐름을 스크립트로 작성합니다. 브라우저에서 직접 실행하는 실습 코드로 Machine Learning Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Machine Learning Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Machine Learning Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“모델 레지스트리: 스테이징, 운영 및 보관” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Machine Learning Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Machine Learning Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- MLflow를 활용한 실험 추적: 매개변수, 지표 및 산출물 기록
- 머신러닝을 위한 Docker 재현 가능 환경
- 모델 레지스트리: 스테이징, 운영 및 보관
- GitHub Actions를 활용한 자동 재학습 파이프라인