Am I using TextLoader wrong when running the ML.Net Iris demo in F#?
Asked Answered
W

1

4

I am new to F#/.NET and I am trying to run the F# example provided in the accepted answer of How to translate the intro ML.Net demo to F#? with the ML.NET library, using F# on Visual Studio, using Microsoft.ML (0.2.0).

When building it I get the error error FS0039: The type 'TextLoader' is not defined.

To avoid this, I added the line

open Microsoft.ML.Data

to the source. Then, however, the line

pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))

triggers: error FS0033: The non-generic type 'Microsoft.ML.Data.TextLoader' does not expect any type arguments, but here is given 1 type argument(s)

Changing to:

pipeline.Add(new TextLoader(dataPath,separator = ","))

yields: error FS0495: The object constructor 'TextLoader' has no argument or settable return property 'separator'. The required signature is TextLoader(filePath: string) : TextLoader.

Changing to:

pipeline.Add(new TextLoader(dataPath))

makes the build successful, but the code fails when running with ArgumentOutOfRangeException: Column #1 not found in the dataset (it only has 1 columns), I assume because the comma separator is not correctly picked up (incidentally, you can find and inspect the iris dataset at https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data).

Also

pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','))

won't work.

I understand that there have been changes in TextLoader recently (see e.g. https://github.com/dotnet/machinelearning/issues/332), can somebody point me to what I am doing wrong?

Winnie answered 18/6, 2018 at 13:13 Comment(4)
Try updating the NuGet package to make sure it's at 0.2. But yes, the API for that has changed to something like new TextLoader(_dataPath).CreateFrom<SentimentData>(separator: ',')Oenone
Oh, sorry, my F# has gotten quite stale. Try this instead: pipeline.Add(TextLoader(_dataPath).CreateFrom<SentimentData>(separator: ','))Oenone
Also, there's a PR currently on the samples repo for F#.Oenone
This looks like it doesn't have any compilation errors for me: pipeline.Add(TextLoader(_dataPath).CreateFrom<SentimentData>(separator=','))Oenone
O
7

F# just has a bit of a different syntax that can take some getting used to. It doesn't use the new keyword to instantiate a new class and to use named parameters it uses the = instead of : that you would in C#.

So for this line in C#:

pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','))

It would be this in F#:

pipeline.Add(TextLoader(dataPath).CreateFrom<IrisData>(separator=','))
Oenone answered 18/6, 2018 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.