This guide shows you how to train a model in ML.NET and create a Grasshopper component that uses said model. In this example, we predict the square root of a number using the sqrt_dataset.csv
-file that contains 100 000 real numbers between 0-1000 and their square root. For installation and setup of ML.NET, follow the official Microsoft guide: https://dotnet.microsoft.com/learn/ml-dotnet/get-started-tutorial/intro
Here, Project, refers to the name of the project you want to add ML.NET to.
1. Create ML.NET model in GH project.
- Right-click the GH project you want to add a ML.NET component to → Add → Machine Learning.
- Select the value regression scenario.
- Upload the
sqrt_dataset.csv
and select the column titled label as your label. Use the recommended training time and Start training. - After training is completed, two projects get added to your solution:
ProjectML.ConsoleApp
andProjectML.Model
.- Delete ConsoleApp.
Add dependency to Project from ProjectML.Model
- Right-click Solution → Properties
- Project dependencies:
- Projects:
Project
→ Depends on: ...Model
- Projects:
- Project dependencies:
Add project reference from Project to ProjectML.Model
-
Right-click
Project
→ Unload project. -
Right-click unloaded
Project
→ Edit project file. -
Above the first
ItemGroup
, cirka line 36, add:<ItemGroup> <ProjectReference Include="" /> </ItemGroup>
-
Right-click
ProjectML.Model
→ Copy full path. -
Inside Include, paste the full path, e.g.:
<ItemGroup> <ProjectReference Include="C:\Users\...\Project\ProjectML.Model\ProjectML.Model.csproj" /> </ItemGroup>
-
Remove everything before (and including the Project-directory):
<ItemGroup> <ProjectReference Include="ProjectML.Model\ProjectML.Model.csproj" /> </ItemGroup>
-
Right-click
Project
→ Reload project with dependencies.
- Create GH-component as usual.
- Inside the new GH-component, add:
using ProjectML.Model;
- To use model, create new
ModelInput
and get theScore
property ofPredict
-method output ofConsumeModel
class like:
ModelInput modelData = new ModelInput()
{
Number = Convert.ToSingle(numberToSquareRoot)
};
float rootedSingle = ConsumeModel.Predict(modelData).Score;
Regression input and output is by default a float. Use Convert.ToSingle to convert double to float.