Week 13 - Reproducibility Pattern: Model Versioning Pattern. Responsible AI Pattern: Explainable Predictions Pattern

Introduction

This week we look at a third reproducibility pattern, the model versioning pattern. We also look at a responsible AI patterns, the explainable predictions pattern. The model versioning pattern deploys a changed model as a microservice with a different endpoint to achieve backward compatibility for deployed models. The explainable predictions pattern applies model explainability techniques to understand how and why models make predictions and improve user trust in machine learning systems. In this week we also take a look at a general approach to design patterns.

Videos

The Model Versioning PatternMachine Learning Design Patterns | Dr Ebin Deni Raj (1:32:30-1:44:05)
Explainable Predictions PatternInterpretable vs Explainable Machine Learning

Assignment(s)

Assignment 6 - Investigation of Design Patterns for Machine Learning

The Model Versioning Design Pattern

The Rationale

The Model Versioning Pattern is a software design concept that focuses on managing and evolving machine learning models over time. Machine learning models are typically developed through an iterative process of training, evaluation, and refinement. As new data becomes available or new insights are gained, models need to be updated and improved. The Model Versioning Pattern enables the management of different versions of the model, allowing for easy tracking, comparison, and deployment of new iterations.

The UML

Here is a UML diagram for the model versioning pattern:

  +------------------+
  |   ModelVersion   |
  +------------------+
  | - Version Number |
  | - Model          |
  | - Training Data  |
  +------------------+
  | + train()        |
  | + predict()      |
  +------------------+
In this UML diagram, the Model Versioning Design Pattern consists of a single component:
  1. ModelVersion: The ModelVersion component represents a specific version of a machine learning model. It encapsulates the version number, the trained model, and the associated training data used to train the model.
The ModelVersion component can be instantiated multiple times to represent different versions of the model. Each instance of ModelVersion corresponds to a specific trained model with its own version number and associated data. For more information, see [WIP] Data model versioning pattern.

Code Example - Model Versioning

Below is a simple code example with the model versioning pattern:
C++: ModelVersioning.cpp.
C#: ModelVersioning.cs.
Java: ModelVersioning.java.
Python: ModelVersioning.py.

Common Usage

The Model Versioning pattern is commonly used in the software industry to manage and control the deployment, usage, and evolution of machine learning models and other types of models. Here are some common usages of the Model Versioning pattern:

  1. A/B Testing: Model versioning is often used in A/B testing scenarios, where different versions of a model are deployed simultaneously to measure their performance and compare their effectiveness. By maintaining multiple model versions and switching between them for different users or segments, organizations can evaluate the impact of model changes on key metrics and make informed decisions about model selection and improvements.
  2. Continuous Deployment and Rollback: Model versioning allows for continuous deployment and rollback of models in production environments. By assigning version numbers to models, organizations can easily switch between different versions, deploy new models, and roll back to previous versions if issues or regressions are detected. This enables iterative development and rapid experimentation with models while maintaining the ability to revert to a stable state if necessary.
  3. Model Governance and Auditing: Model versioning helps establish a governance framework for managing models throughout their lifecycle. Organizations can track and document changes to models, record performance metrics for different versions, and maintain an audit trail of model deployments. This supports compliance requirements, enables reproducibility, and facilitates model monitoring and analysis.
  4. Model Ensembling and Stacking: Model versioning is utilized in ensemble modeling techniques, where multiple models are combined to improve prediction accuracy. By maintaining different versions of models with varying architectures, hyperparameters, or training data, organizations can create diverse ensembles that leverage the strengths of different models and enhance overall predictive power.
  5. Model Retraining and Evolution: Model versioning enables the management of model evolution over time. By incrementing version numbers and maintaining a history of model versions, organizations can iterate on models, retrain them with updated data, fine-tune hyperparameters, and introduce new features or algorithms. This allows models to adapt to changing business requirements and improve their performance and relevance.
  6. Model Serving and Deployment: Model versioning facilitates the deployment and serving of models in production environments. Organizations can maintain different versions of models concurrently, manage their deployment configurations, and handle traffic routing to specific versions. This enables seamless updates and can prevent disruption to critical systems relying on model predictions.
  7. Collaboration and Experimentation: Model versioning supports collaborative model development and experimentation. Teams can work on different versions of models in parallel, compare their performance, share findings, and collaborate on model improvements. Versioning also allows for controlled experimentation, where different teams or individuals can propose and test modifications to models while maintaining isolation and avoiding interference with production systems.

Code Problem - Improved Model Versioning

In the main function, we create instances of ModelVersion1, ModelVersion2, and ModelVersion3. We register these model versions with the ModelVersionManager by calling the registerModelVersion method. We then demonstrate loading and prediction using specific model versions.

Later in the example, we create improved versions of Model Version 2 and Model Version 3, represented by ModelVersion2Improved and ModelVersion3Improved. We register these improved model versions with the manager, effectively replacing the previous versions.

Finally, we again demonstrate loading and prediction using the improved model versions.
ModelVersion.h,
ModelVersion1.h,
ModelVersion2.h,
ModelVersion3.h,
ModelVersionManager.h,
ModelVersionManager.cpp,
ImprovedModelVersioning.cpp.

Code Problem - Algorithm Models

The following code represents a scenario where you have multiple algorithms for the same task, and you want to version each algorithm independently. We'll use a factory pattern for creating instances of models, and each model will have its own versioning. Additionally, we'll introduce a model manager to coordinate the training and prediction processes.
MLModel.h,
VersionedMLModel.h,
DecisionTreeModel.h,
RandomForestModel.h,
ModelFactory.h,
ModelManager.h,
AlgorithmModelMain.cpp.

The Explainable Predictions Design Pattern

The Rationale

The Explainable Predictions design pattern aims to provide transparent and interpretable explanations for the predictions or decisions made by machine learning models. As machine learning models are increasingly used in critical domains such as healthcare, finance, or autonomous systems, it becomes crucial to ensure trust and transparency in the decision-making process. Machine learning models are black boxes in general. But having a clear understanding of the model behavior is very important to diagnose the errors and to identify potential biases to decide if they can be employed. Introducing explainability in machine learning is a major factor in Responsible AI. Hence, the key idea of this pattern is to interpret the machine learning models to understand why and how the model made the predictions in a certain way.

The UML

Here is a UML diagram for the explainable predictions pattern:

  +------------------+
  | ExplanableModel  |
  +------------------+
  | - Model          |
  +------------------+
  | + explain()      |
  | + predict()      |
  +------------------+  
In this UML diagram, the Explainable Predictions Design Pattern consists of a single component:
  1. ExplainableModel: The ExplainableModel component represents a machine learning model that is designed to provide explainable predictions. It encapsulates the underlying model and provides methods for explanation and prediction. The ExplainableModel component provides two main methods:
The ExplainableModel component facilitates the development and usage of machine learning models that prioritize explainability. By incorporating the Explainable Predictions Design Pattern, data scientists and developers can create models that not only provide accurate predictions but also offer insights into the reasoning behind those predictions.

Code Example - Explainable Predictions

Below is a simple code example with the explainable predictions pattern:
C++: ExplainablePredictions.cpp.
C#: ExplainablePredictions.cs.
Java: ExplainablePredictions.java.
Python: ExplainablePredictions.py.

Common Usage

The Explainable Predictions design pattern is gaining importance in the software industry as the need for interpretability and transparency in machine learning models increases. Here are some common usages of the Explainable Predictions design pattern in industry:

  1. Regulatory Compliance: In industries where regulatory compliance is crucial, such as finance, healthcare, and insurance, the Explainable Predictions pattern is used to provide explanations for the predictions made by machine learning models. This helps organizations meet regulatory requirements and ensures that the decision-making process is transparent and accountable.
  2. Risk Assessment and Fraud Detection: Explainable Predictions are extensively used in risk assessment and fraud detection systems. By providing explanations for the factors contributing to a prediction or identifying the important features that influenced the decision, organizations can understand the reasons behind risk scores or fraud alerts. This enables better decision-making and helps in identifying and addressing potential vulnerabilities or biases in the models.
  3. Credit Scoring and Loan Approval: In the financial industry, Explainable Predictions are used to provide transparency in credit scoring and loan approval systems. By explaining the factors that led to a particular credit score or loan decision, organizations can provide customers with understandable justifications and build trust. It also helps in identifying potential biases and discriminatory practices.
  4. Healthcare Diagnosis and Treatment: In healthcare, Explainable Predictions play a vital role in diagnostic systems and treatment recommendation engines. By providing explanations for the predictions made by the models, healthcare professionals can better understand the reasoning behind the recommendations. This facilitates collaboration between clinicians and machine learning models, leading to improved patient care and better-informed decision-making.
  5. Customer Relationship Management: In industries like e-commerce and customer service, Explainable Predictions are used to enhance customer relationship management. By providing explanations for product recommendations, personalized offers, or customer segmentation, organizations can improve customer satisfaction and loyalty. Customers gain insights into why certain recommendations are made, which helps build trust and improves the overall customer experience.
  6. Human Resources and Talent Management: Explainable Predictions are used in human resources and talent management systems to support decision-making in areas such as candidate screening, performance evaluation, and employee retention. By explaining the factors influencing decisions, organizations can ensure fairness, reduce bias, and improve transparency in these processes.
  7. Algorithmic Auditing and Ethical AI: Explainable Predictions are an important component of algorithmic auditing and ethical AI initiatives. Organizations use this pattern to assess the fairness, transparency, and ethical implications of their machine learning models. By providing explanations for predictions, potential biases and discriminatory patterns can be identified, leading to fairer and more ethical decision-making.

Code Problem - Complex Predictions

The PredictionModel abstract base class has three pure virtual methods: loadModel, predict, and explain. These methods are responsible for loading the model, making predictions, and providing explanations, respectively. The PredictionModel abstract base class includes two additional pure virtual methods: preprocessData and postprocessResults. These methods represent the steps for preprocessing the data before prediction and post-processing the prediction results, respectively.

The ConcretePredictionModel class is updated to implement these additional methods. It performs data preprocessing before making the prediction and performs post-processing on the prediction results.

The ExplainablePredictionModel wrapper class is also updated to delegate the preprocessData and postprocessResults methods to the wrapped model, similar to the existing delegation for other methods.

In the main function, we demonstrate the complete prediction workflow using the explainableModel. We load the model, preprocess the data, make a prediction, explain the prediction, and post-process the prediction results.
PredictionModel.h,
ConcretePredictionModel.h,
ExplainablePredictionModel.h,
ComplexPredictions.cpp.

Code Problem - Linear Regression Explainer

The following code implements a linear regression model and an explainer class to provide explanations for predictions.
MLModel.h,
ExplainablePrediction.h,
LinearRegressionModel.h,
LinearRegressionExplainer.h,
LRExplainerMain.cpp.

Design Patterns - A Generalization

This concludes our study of design patterns, both standard and specific to machine learning. As we have studied them, we notice a common theme in all of them. On the one hand is an interface to some concrete components, and on the other hand is a manager or director (or server or publisher) of some sort to these components. There could in fact be several managers/directors/servers/publishers accessible through their own interface. See the diagram below:
Design Patterns, a Generalization

Compare the above generalization to our standard design patterns. The UMLs can be seen at:
Week 1:Singleton Pattern
Factory Design Pattern
Week 2:Abstract Factory Pattern
Builder Pattern
Prototype Pattern
Week 3:Adapter Pattern
Bridge Pattern
Week 4:Decorator Pattern
Composite Pattern
Week 5:Proxy Pattern
Flyweight Pattern
Week 6:Observer Pattern
Strategy Pattern
Week 7:Template Pattern
Chain of Responsibility Pattern
Compare this generalization to the machine learning design patterns. See the lecture notes at:
Week 8,
Week 9,
Week 10,
Week 11,
Week 12,
Week 13.