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?

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
  • 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(separator: ',')` – Jon Jun 18 '18 at 13:19
  • Oh, sorry, my F# has gotten quite stale. Try this instead: `pipeline.Add(TextLoader(_dataPath).CreateFrom(separator: ','))` – Jon Jun 18 '18 at 13:27
  • Also, there's a [PR](https://github.com/dotnet/machinelearning-samples/pull/7) currently on the samples repo for F#. – Jon Jun 18 '18 at 13:28
  • This looks like it doesn't have any compilation errors for me: `pipeline.Add(TextLoader(_dataPath).CreateFrom(separator=','))` – Jon Jun 18 '18 at 13:46

1 Answers1

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=','))
Jon
  • 2,644
  • 1
  • 22
  • 31