dotnet / dotnet/machinelearning-modelbuilder

Code reusability post brain dump

Open
#2,269 0 comments 1 reaction 0 assignees View on GitHub
Stale
Dominant language
Dockerfile
Stars
285
Forks
66
PR merge metrics
No merged PRs in 30d

Description

There's been a few requests / questions around sharing models across multiple projects. Today we recommend the following approach:

## Add model to C# Class Library

The general process for this is:

1. Create C# Class Library project
2. Right-Click > Add > Machine Learning Model
3. Select Machine Learning Model (ML.NET) in Add New Item dialog.
4. Provide name for your model
5. Follow Model Builder prompts to train your model.

Once the model is trained, you can add a reference to it in any project that needs it and reuse the consumption code that gets generated by Model Builder to make predictions.

---

An approach you might also consider is:

## Package model as NuGet package

NuGet packages can also be used. Models can be treated as code. They are basically stateful functions that take data in, apply transformations based on patterns learned during the training process, and output a result (often a prediction). As such, models can be packaged like any other code asset using NuGet.

Advantages:

- Versioning. Not only for auditability and rollback purposes, but upgrading to the latest version of the model is as easy as updating the version number.
- Retraining and consumption code are included as part of the package reducing the need to write boilerplate code for consumption.
- Leverage existing CI/CD processes for packaging and distributing code.
- Leverage existing security measures already in place when it comes to packages.
- Leverage existing tools and knowledge for consuming packages.
- Opportunity to create model libraries. Models that are intended to work together can be included in a single package for easier accessibility and dependency management.

Disadvantages:

- Depending on where the package is being published to, there may be package size limitations / restrictions. This is especially true for larger models.

The general process of packaging models as follows:

1. Create C# Class Library project
2. Right-Click > Add > Machine Learning Model
3. Select Machine Learning Model (ML.NET) in Add New Item dialog.
4. Provide name for your model
5. Follow Model Builder prompts to train your model.
6. Create and publish NuGet package. For simplicity, the package can be published to a local directory. However, if you already have your own private or NuGet feed, those are options as well.
7. Add feed as a package source (if it isn't already one of your sources).
8. Create a new C# Console application
9. Use VS NuGet Package Manager to install the NuGet Package for your ML Model
10. Use the code from the installed package to make predictions.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.