Click here to Skip to main content
15,887,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following is my first attempt to use ML.Net with VB.Net via
Microsoft Visual Studio Community 2017 
Version 15.8.1
VisualStudio.15.Release/15.8.1+28010.2003
Microsoft .NET Framework
Version 4.7.03056


1. Why am I seeing
oPrediction(Setosa).Label = 1
oPrediction(versicolor).Label = 1
oPrediction(virginica).Label = 1

rather than?
oPrediction(Setosa).Label = Iris-setosa
oPrediction(versicolor).Label = Iris-versicolor
oPrediction(virginica).Label = Iris-virginica


2. How can I check the iris-data.txt file is actually being read?
The reason I ask is that I had mis-spelled the name & there was no error mentioned.

3. Why do I see "System.ArgumentOutOfRangeException: 'Score column 'Score' not found. Parameter name: name' when the Test section is uncommented?

4. I have seen details of Preview used in C# providing model details (weights & biases).
How do I enable on VB.Net?

Option Explicit On

Imports Microsoft.ML
Imports Microsoft.ML.Runtime.Api
Imports Microsoft.ML.Runtime.Data
Imports Microsoft.ML.Runtime.Learners
Imports Microsoft.ML.Transforms.Conversions
Imports System
Imports System.IO

Public Class frmMain

    Private Sub btnIRIS_Click(sender As Object, e As EventArgs) Handles btnIRIS.Click
        Dim sPath As String = "C:\VBnet bespoke\MachineLearning\MachineLearning\dat"
        Dim sTrainingDataFile As String = sPath & "\iris-data.txt"
        Dim sTestDataFile As String = sPath & "\iris-data.txt"

        Dim mlContext As New MLContext()

        ' ------------------------------------------------------------------------------------------------------------------------------
        ' Train
        ' ------------------------------------------------------------------------------------------------------------------------------

        ' Define how to read what from the file
        Dim oTextReader = mlContext.Data.TextReader(New TextLoader.Arguments() With
                                        {
                                          .Separator = "," _
                                        , .HasHeader = False _
                                        , .Column = {New TextLoader.Column("SepalLength", DataKind.R4, 0),              ' seems values have to be Single & not Double (ie can't use R8)
                                                        New TextLoader.Column("SepalWidth", DataKind.R4, 1),
                                                        New TextLoader.Column("PetalLength", DataKind.R4, 2),
                                                        New TextLoader.Column("PetalWidth", DataKind.R4, 3),
                                                        New TextLoader.Column("Label", DataKind.TX, 4)
                                                    }
                                        })

        ' Define lazy (present only when needed) data view as read from Training Data file
        Dim oTrainingDataView = oTextReader.Read(sTrainingDataFile)

        'Debug.Print("Number of rows read from file" & Format(oTrainingDataView.GetRowCount))

        ' We're going to use "SepalLength", "SepalWidth", "PetalLength", "PetalWidth" as the known Features to train the network to predict Label
        Dim oPipeline = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")

        With oPipeline
            ' Normalize the data file feature values to ensure all are between -1 & +1
            .Append(mlContext.Transforms.Normalize("SepalLength"))
            .Append(mlContext.Transforms.Normalize("SepalWidth"))
            .Append(mlContext.Transforms.Normalize("PetalLength"))
            .Append(mlContext.Transforms.Normalize("PetalWidth"))

            ' Ensure the text value is mapped to a numeric (single,R4) value (just an index into the list of possible label text values).
            .Append(mlContext.Transforms.Categorical.MapValueToKey("Label"))

            ' Given the features & the corresponding known label, use SDCA to train the network by deriving best weights & biases
            .Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(label:="Label", features:="Features"))

            ' Output from network is a number so convert back to the appropriate string describing the flower
            .Append(mlContext.Transforms.Conversion.MapKeyToValue(("PredictedLabel", "Label")))
        End With

        Dim oModel = oPipeline.Fit(oTrainingDataView)

        ' ------------------------------------------------------------------------------------------------------------------------------
        ' Test
        ' ------------------------------------------------------------------------------------------------------------------------------

        Dim oTestData = oTextReader.Read(sTestDataFile)
        Dim oTestPredictions = oModel.Transform(oTestData)
        Dim oMetrics = mlContext.MulticlassClassification.Evaluate(oTestPredictions, label:="Label")

        Debug.Print("AccurancyMicro = " & Format(oMetrics.AccuracyMicro, "#.######"))

        ' ------------------------------------------------------------------------------------------------------------------------------
        ' Predict
        ' ------------------------------------------------------------------------------------------------------------------------------

        Dim oIRIS_Data As New Iris_Data
        Dim oIRIS_Prediction As New Iris_Prediction

        ' 5.1,3.5,1.4,0.2,Iris-Setosa
        Dim oPredictionSetosa = oModel.MakePredictionFunction(Of Iris_Data, Iris_Prediction)(mlContext).Predict _
                (New Iris_Data With {.SepalLength = 5.1, .SepalWidth = 3.5, .PetalLength = 1.4, .PetalWidth = 0.2, .Label = vbNull})
        ' Label seems to have to be vbNull. If missing nothing returned. If a non-null value then same is returned.

        Debug.Print("oPrediction(Setosa).Label = " & oPredictionSetosa.Label)

        ' 7.0,3.2,4.7,1.4,Versicolor (from file)
        Dim oPredictionVersicolor = oModel.MakePredictionFunction(Of Iris_Data, Iris_Prediction)(mlContext).Predict _
                (New Iris_Data With {.SepalLength = 7.0, .SepalWidth = 3.2, .PetalLength = 4.7, .PetalWidth = 1.4, .Label = vbNull})
        ' Label seems to have to be vbNull. If missing nothing returned. If a non-null value then same is returned.

        Debug.Print("oPrediction(ersicolor).Label = " & oPredictionVersicolor.Label)

        '6.3,3.3,6.0,2.5,Iris-virginica
        Dim oPredictionVirginica = oModel.MakePredictionFunction(Of Iris_Data, Iris_Prediction)(mlContext).Predict _
                (New Iris_Data With {.SepalLength = 6.3, .SepalWidth = 3.3, .PetalLength = 6.0, .PetalWidth = 2.5, .Label = vbNull})
        ' Label seems to have to be vbNull. If missing nothing returned. If a non-null value then same is returned.

        Debug.Print("oPrediction(virginica).Label = " & oPredictionVirginica.Label)
    End Sub
End Class

Public Class Iris_Data
    Public SepalLength As Single
    Public SepalWidth As Single
    Public PetalLength As Single
    Public PetalWidth As Single

    <ColumnName("Label")>
    Public Label As String
End Class

Public Class Iris_Prediction
    <ColumnName("Label")>
    Public Label As String
End Class


All help/pointers are appreciated.

What I have tried:

I've gleamed some info from
https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/single-predict-model-ml-net
https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/taxi-fare
https://docs.microsoft.com/en-us/dotnet/machine-learning/resources/transforms#normalization
https://blogs.msdn.microsoft.com/dotnet/2018/10/08/announcing-ml-net-0-6-machine-learning-net/
https://github.com/dotnet/machinelearning/blob/master/docs/code/MlNetCookBook.md
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900