Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Work with Visual Studio and script classes to create machine learning models.

How-to ExamplesFeatureApplication → Tasks and Classes



Info

Solution (.dbsln) and model (.mlnet): MLNET-ModelBuilder

Visual Studio Files: myMLApp


Info

Please ensure that the solution and the model are located in the same folder when running the application. The model’s output will be displayed in the TraceWindow.



Table of Contents
maxLevel2
minLevel2
excludeOverview
stylenone
classon-this-page
printablefalse





Summary

Model Builder is a Visual Studio extension that lets you build, train, and generate code to consume ML models through a guided UI (AutoML-backed).

In this example, we created a binary sentiment model to predict whether a restaurant customer review is Positive or Negative (same example as the microsoft page), then integrated the generated consumption code into FrameworX Multiplatform.

Official website of the tool: ML.NET Model Builder - machine learning in Visual Studio | .NET


Technical Information

Requirements

  • Microsoft Visual Studio 2026

  • .NET 8.0

  • .NET 10.0

  • FrameworX Multiplatform

How the model was created (high level)

We followed Microsoft’s ML.NET Tutorial - Get started in 10 minutes | .NET flow and trained a model for sentiment classification. The key benefit: Model Builder generates the training/consumption code and the model file, so integration is mostly about copying the generated consumer code and loading the.mlnetmodel.

Integration with the Solution

1 - Have the necessary dependencies

  • Go to Scripts / Classes and click in the new button
  • Select "Add ML.NET namespaces"
  • Click OK


Copy the code below into Scripts / Tasks. This code is used to consume models inside the Scripts module. Once added, you can feed the model using your process variables in real time and read the model outputs for use in your logic.

Info

All the code below was generated by the Model Builder tool from Visual Studio, no changes were required. Just copy and paste it into your solution.

2 - Define the Inputs and Outputs

The input schema must match what the model was trained with (column order/types). Keep the same structure you got from the generated project.

Code Block
public class ModelInput {
    [LoadColumn(0)]
    [ColumnName(@"col0")]
    public string Col0 { get; set; }

    [LoadColumn(1)]
    [ColumnName(@"col1")]
    public float Col1 { get; set; }
}

public class ModelOutput {
    [ColumnName(@"PredictedLabel")]
    public float PredictedLabel { get; set; }
}

Notes

  • Define all input columns the model expects.

  • For outputs, you only need to define what you will actually use (e.g., PredictedLabel).

3 - Implement the consumption methods

Declare global variable and initialize the constructor

Code Block
public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);

private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine() {
    var mlContext = new MLContext();
    ITransformer mlModel = mlContext.Model.Load(@"C:\Users\eduar\source\repos\myMLApp\myMLApp\SentimentModel.mlnet", out var _);
    return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
}

Create the prediction method:

Code Block
public static ModelOutput Predict(ModelInput input) {
    var predEngine = PredictEngine.Value;
    return predEngine.Predict(input);
}

4 - Create the Main method

Code Block
public async Task Main() {

    var sampleData = new ModelInput() {
        Col0 = "This restaurant was bad."
    };
    
    var result = SentimentModel.Predict(sampleData);
    var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";

    @Info.Trace($"Text: {sampleData.Col0}");
    @Info.Trace($"Sentiment: {sentiment}");
}


5 - Output

The output in the TraceWindow will look like this:

Text: Great quality!
Sentiment: Positive

Info

Make sure the Scripts module is enabled in the Trace Window settings so you can view the output.



Info

You can use any LLM to assist during implementation. For this implementation, we used Visual Studio with Claude Code.



In this section...

Page Tree
root@parent
spaces93DRAF