AWS sagemaker documentation change
Summary
Added code examples for SageMaker Python SDK v2 (Legacy) showing MLflow model tracking and registration workflow
Security assessment
The changes only add legacy SDK code examples for model tracking/registration. No security vulnerabilities, authentication mechanisms, or access controls are mentioned or modified.
Diff
diff --git a/sagemaker/latest/dg/mlflow-track-experiments-model-registration.md b/sagemaker/latest/dg/mlflow-track-experiments-model-registration.md index ddee3817d..7efe2e24f 100644 --- a//sagemaker/latest/dg/mlflow-track-experiments-model-registration.md +++ b//sagemaker/latest/dg/mlflow-track-experiments-model-registration.md @@ -20,0 +21,3 @@ Use `create_registered_model` within your MLflow client to automatically create +SageMaker Python SDK v3 + + @@ -31,0 +35,60 @@ Use `create_registered_model` within your MLflow client to automatically create +SageMaker Python SDK v2 (Legacy) + + + + import mlflow + + from mlflow.models import infer_signature + + import pandas as pd + from sklearn import datasets + from sklearn.model_selection import train_test_split + from sklearn.linear_model import LogisticRegression + from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score + + # This is the ARN of the MLflow Tracking Server you created + mlflow.set_tracking_uri(your-tracking-server-arn) + mlflow.set_experiment("some-experiment") + + # Load the Iris dataset + X, y = datasets.load_iris(return_X_y=True) + + # Split the data into training and test sets + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + + # Define the model hyperparameters + params = {"solver": "lbfgs", "max_iter": 1000, "multi_class": "auto", "random_state": 8888} + + # Train the model + lr = LogisticRegression(**params) + lr.fit(X_train, y_train) + + # Predict on the test set + y_pred = lr.predict(X_test) + + # Calculate accuracy as a target loss metric + accuracy = accuracy_score(y_test, y_pred) + + # Start an MLflow run and log parameters, metrics, and model artifacts + with mlflow.start_run(): + # Log the hyperparameters + mlflow.log_params(params) + + # Log the loss metric + mlflow.log_metric("accuracy", accuracy) + + # Set a tag that we can use to remind ourselves what this run was for + mlflow.set_tag("Training Info", "Basic LR model for iris data") + + # Infer the model signature + signature = infer_signature(X_train, lr.predict(X_train)) + + # Log the model + model_info = mlflow.sklearn.log_model( + sk_model=lr, + name="iris_model", # Changed from artifact_path to name for MLflow 3.0 + signature=signature, + input_example=X_train, + registered_model_name="tracking-quickstart", + ) + @@ -33,0 +97,3 @@ Use `mlflow.register_model()` to automatically register a model with the SageMak +SageMaker Python SDK v3 + + @@ -56,0 +123,60 @@ Use `mlflow.register_model()` to automatically register a model with the SageMak +SageMaker Python SDK v2 (Legacy) + + + + import mlflow + + from mlflow.models import infer_signature + + import pandas as pd + from sklearn import datasets + from sklearn.model_selection import train_test_split + from sklearn.linear_model import LogisticRegression + from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score + + # This is the ARN of the MLflow Tracking Server you created + mlflow.set_tracking_uri(your-tracking-server-arn) + mlflow.set_experiment("some-experiment") + + # Load the Iris dataset + X, y = datasets.load_iris(return_X_y=True) + + # Split the data into training and test sets + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + + # Define the model hyperparameters + params = {"solver": "lbfgs", "max_iter": 1000, "multi_class": "auto", "random_state": 8888} + + # Train the model + lr = LogisticRegression(**params) + lr.fit(X_train, y_train) + + # Predict on the test set + y_pred = lr.predict(X_test) + + # Calculate accuracy as a target loss metric + accuracy = accuracy_score(y_test, y_pred) + + # Start an MLflow run and log parameters, metrics, and model artifacts + with mlflow.start_run(): + # Log the hyperparameters + mlflow.log_params(params) + + # Log the loss metric + mlflow.log_metric("accuracy", accuracy) + + # Set a tag that we can use to remind ourselves what this run was for + mlflow.set_tag("Training Info", "Basic LR model for iris data") + + # Infer the model signature + signature = infer_signature(X_train, lr.predict(X_train)) + + # Log the model + model_info = mlflow.sklearn.log_model( + sk_model=lr, + name="iris_model", # Changed from artifact_path to name for MLflow 3.0 + signature=signature, + input_example=X_train, + registered_model_name="tracking-quickstart", + ) +