ML.NET Project Structure

Learn the fundamental structure of an ML.NET project.

The interactive playground below contains a project that is autogenerated when we build an ML model by using ML.NET. This is a binary classification model that aims to determine whether a specific sentence has a positive or negative sentiment. It was trained by using the yelp_labelled.txt file found in the TrainingData folder.

// This file was auto-generated by ML.NET Model Builder. 
using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
namespace MLApp
{
    public partial class DemoMLModel
    {
        /// <summary>
        /// model input class for DemoMLModel.
        /// </summary>
        #region model input class
        public class ModelInput
        {
            [ColumnName(@"col0")]
            public string Col0 { get; set; }

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

        }

        #endregion

        /// <summary>
        /// model output class for DemoMLModel.
        /// </summary>
        #region model output class
        public class ModelOutput
        {
            [ColumnName(@"col0")]
            public float[] Col0 { get; set; }

            [ColumnName(@"col1")]
            public uint Col1 { get; set; }

            [ColumnName(@"Features")]
            public float[] Features { get; set; }

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

            [ColumnName(@"Score")]
            public float[] Score { get; set; }

        }

        #endregion

        private static string MLNetModelPath = Path.GetFullPath("/models/DemoSentimentMLModel.zip");

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

        /// <summary>
        /// Use this method to predict on <see cref="ModelInput"/>.
        /// </summary>
        /// <param name="input">model input.</param>
        /// <returns><seealso cref=" ModelOutput"/></returns>
        public static ModelOutput Predict(ModelInput input)
        {
            var predEngine = PredictEngine.Value;
            return predEngine.Predict(input);
        }

        private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine()
        {
            var mlContext = new MLContext();
            ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var _);
            return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
        }
    }
}
Basic ML.NET project

Note: The original model generated by the CLI command had nullable warnings when the application was executed. Although these warnings did not affect the execution of the application, they were disabled to keep the console output clean. This was done by changing the value of the Nullable element in the MLApp.csproj file from enable to disable.

The Program.cs file has been added so we can consume the trained model. It uses an interactive console interface where we can type sentences. For each sentence we type, the model will tell us whether the sentiment is positive or negative.

If we build and run ...