This directory contains comprehensive examples for training machine learning models on SageMaker using the Codex API. Each example demonstrates best practices for different frameworks and use cases.
Location: pytorch-image-classification/
Framework: PyTorch
Use Case: Image classification with ResNet
Features:
- Loads image datasets from SageMaker channels
- Trains ResNet-18 for classification
- Supports data augmentation and preprocessing
- Saves model in PyTorch format
- Includes comprehensive error handling
Location: tensorflow-regression/
Framework: TensorFlow/Keras
Use Case: Neural network regression
Features:
- Loads CSV data from SageMaker channels
- Trains configurable neural network
- Supports multiple evaluation metrics
- Saves model in SavedModel format
- Includes training history visualization
Location: sklearn-clustering/
Framework: Scikit-learn
Use Case: Unsupervised clustering
Features:
- Supports multiple clustering algorithms (KMeans, DBSCAN, Agglomerative)
- Loads CSV data from SageMaker channels
- Comprehensive evaluation metrics
- Cluster visualization plots
- Saves model and cluster assignments
Location: docker-custom/
Framework: Any (Custom Docker)
Use Case: Custom container training
Features:
- Demonstrates custom Docker container usage
- Random Forest regression example
- Shows both Script Mode and Docker Mode
- Includes Dockerfile and build instructions
- Flexible framework support
Select the example that best matches your use case:
- Image Classification: Use PyTorch example
- Neural Network Regression: Use TensorFlow example
- Clustering: Use Scikit-learn example
- Custom Framework: Use Docker example
# Navigate to the example directory
cd examples/pytorch-image-classification/
# Install dependencies
pip install -r requirements.txt
# Run locally with test data
python train.py --data_dir ./test_data --model_dir ./output --epochs 5# Create bundle (avoid including local test data)
tar -czf example.tar.gz train.py requirements.txt README.md
# Upload using API client
python3 scripts/e2e_client.py example.tar.gz --entry train.py
Or, using the pdtrain CLI with exclusions for local test data:
```bash
# From within the example directory
pdtrain bundle upload . --name "my-example" \
--exclude "data" \
--exclude "test_data" \
--wait
# Upload your dataset separately
pdtrain dataset upload ./data --name "my-dataset" --wait
### 4. Create and Submit Run
```bash
# Create run
curl -X POST "http://localhost:8000/v1/runs" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"execution_mode": "script",
"bundle_id": "BUNDLE_ID",
"framework": "pytorch",
"framework_version": "1.13.1",
"python_version": "py38",
"hyperparameters": {
"epochs": 10,
"batch_size": 32,
"learning_rate": 0.001
},
"entry": "train.py",
"inputs": [
{
"type": "dataset",
"dataset_id": "DATASET_ID",
"version": 1
}
]
}'
# Submit to SageMaker
curl -X POST "http://localhost:8000/v1/runs/RUN_ID/submit" \
-H "x-api-key: YOUR_API_KEY"
Each example follows a consistent structure:
example-name/
├── train.py # Main training script
├── requirements.txt # Python dependencies
├── README.md # Detailed documentation
└── (optional files) # Additional configuration
-
train.py: Main training script with:- Command-line argument parsing
- Data loading from SageMaker channels
- Model training and evaluation
- Model saving to
SM_MODEL_DIR - Comprehensive error handling and logging
-
requirements.txt: Python dependencies with:- Framework-specific packages
- Data processing libraries
- Visualization tools
- Utility libraries
-
README.md: Comprehensive documentation with:- What the example does
- How to test locally
- How to package and upload
- Expected inputs and outputs
- Hyperparameter descriptions
- Troubleshooting guide
- Always load data from
/opt/ml/input/data/dataset/(SageMaker default) - Handle missing values gracefully
- Use appropriate data preprocessing
- Validate data format and structure
- Accept hyperparameters via command-line arguments
- Use appropriate logging for debugging
- Implement proper error handling
- Save model checkpoints for long training runs
- Save models to
/opt/ml/model/(SageMaker default) - Include model metadata and configuration
- Save preprocessing objects (scalers, encoders)
- Provide model loading instructions
- Use try-catch blocks for critical operations
- Log detailed error messages
- Provide meaningful error responses
- Handle edge cases gracefully
- Use structured logging with timestamps
- Log hyperparameters and configuration
- Log training progress and metrics
- Log model saving confirmation
- Use
torch.save()for model state dicts - Include model architecture information
- Handle GPU/CPU device selection
- Use appropriate data loaders
- Use
model.save()for SavedModel format - Include model metadata and metrics
- Use callbacks for monitoring
- Handle eager execution properly
- Use
joblib.dump()for model persistence - Include feature names and metadata
- Save preprocessing objects
- Provide prediction examples
- Create efficient Dockerfiles
- Use multi-stage builds for smaller images
- Include all necessary dependencies
- Test containers locally before deployment
-
Data Loading Errors
- Check file paths and formats
- Verify data structure
- Handle missing values
-
Memory Issues
- Reduce batch size or model size
- Use data sampling
- Optimize data loading
-
Model Saving Errors
- Check directory permissions
- Verify model format
- Include all necessary files
-
Hyperparameter Issues
- Validate parameter types
- Check parameter ranges
- Provide sensible defaults
- Check the example-specific README
- Review the logs for error messages
- Test locally before uploading
- Verify SageMaker permissions
- Check CloudWatch logs for detailed errors
To add a new example:
- Create a new directory with the example name
- Follow the established structure
- Include comprehensive documentation
- Test locally and with SageMaker
- Update this README with the new example
These examples are provided as-is for educational and reference purposes. Modify them as needed for your specific use cases.