3

I'm attempting to translate the "Hello ML.NET World" example from C# to F# (code copied below), but I'm getting an F# compiler error about incompatible types.

I've seen a couple blog posts about ML.NET and F#, but they all use the older API which involved explicitly creating a LearningPipeline object. As far as I can tell, this API has been removed.

The problematic line in C# is the line to train a pipeline:

var pipeline = mlContext.Transforms.Concatenate("Features", new[] { "Size" })
    .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));

I've attempted to translate into F# like this:

let pipeline (mlContext:MLContext) =
    mlContext.Transforms
        .Concatenate("Features", [| "Size" |])
        .Append(mlContext.Regression.Trainers.Sdca(labelColumnName = "Price", maximumNumberOfIterations = Nullable(100)))

However, I'm getting a compiler error: Type constraint mismatch: The type 'Transforms.ColumnConcatenatingEstimator' is not compatible with the type IEstimator<ITransformer>'.

I've also tried explicitly downcasting the ColumnConcatenatingEstimator to an IEstimator:

let pipeline' (mlContext:MLContext) =
    let concat = mlContext.Transforms.Concatenate("Features", [| "Size" |])
    let scda = mlContext.Regression.Trainers.Sdca(labelColumnName = "Price", maximumNumberOfIterations = Nullable(100))

    let concatAsEstimator = concat :> IEstimator<_>
    concatAsEstimator.Append(scda)

This slightly changes the types in the compiler error. The new message indicates that IEstimator<ColumnConcatenatingTransformer> is not compatible with IEstimator<ITransformer>.

It looks like I need to explicitly downcast the ColumnConcatenatingTransformer inside the generic into an ITransformer, but I'm not sure how to do this in F#. Is this possible?

For reference, here is the full C# code from Microsoft that I'm trying to adapt:

using System;
using Microsoft.ML;
using Microsoft.ML.Data;

class Program
{
   public class HouseData
   {
       public float Size { get; set; }
       public float Price { get; set; }
   }

   public class Prediction
   {
       [ColumnName("Score")]
       public float Price { get; set; }
   }

   static void Main(string[] args)
   {
       MLContext mlContext = new MLContext();

       // 1. Import or create training data
       HouseData[] houseData = {
           new HouseData() { Size = 1.1F, Price = 1.2F },
           new HouseData() { Size = 1.9F, Price = 2.3F },
           new HouseData() { Size = 2.8F, Price = 3.0F },
           new HouseData() { Size = 3.4F, Price = 3.7F } };
       IDataView trainingData = mlContext.Data.LoadFromEnumerable(houseData);

       // 2. Specify data preparation and model training pipeline
       var pipeline = mlContext.Transforms.Concatenate("Features", new[] { "Size" })
           .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));

       // 3. Train model
       var model = pipeline.Fit(trainingData);

       // 4. Make a prediction
       var size = new HouseData() { Size = 2.5F };
       var price = mlContext.Model.CreatePredictionEngine<HouseData, Prediction>(model).Predict(size);

       Console.WriteLine($"Predicted price for size: {size.Size*1000} sq ft= {price.Price*100:C}k");

       // Predicted price for size: 2500 sq ft= $261.98k
   }
}

(Edit: just to clarify, this is not the same question as How to translate the intro ML.NET demo to F#.) This is a different code example, and it uses a newer version of ML.NET. The Microsoft link in that answer also appears to be broken now.

Freddy The Horse
  • 335
  • 2
  • 12
  • Try starting your pipeline with `EstimatorChain()`. Maybe something like `let concat = EstimatorChain().Append(mlContext.Transforms.Concatenate...)` – Jon May 07 '20 at 17:41

2 Answers2

5

ML.NET is built with C# in mind, hence sometimes converting to F# need to add Nullable and float32 everywhere. Here is my version where I get rid of the PredictionEngine, I put the Sdca as trainer and use EstimatorChain() to append and create a IEstimator

open System
open Microsoft.ML
open Microsoft.ML.Data


type HouseData = 
    {
        Size  : float32
        Price : float32 
    }
let downcastPipeline (x : IEstimator<_>) = 
    match x with 
    | :? IEstimator<ITransformer> as y -> y
    | _ -> failwith "downcastPipeline: expecting a IEstimator<ITransformer>"

let mlContext = MLContext(Nullable 0)
let houseData = 
    [|
        { Size = 1.1F; Price = 1.2F }
        { Size = 1.1F; Price = 1.2F }
        { Size = 2.8F; Price = 3.0F }
        { Size = 3.4F; Price = 3.7F }
    |] |> mlContext.Data.LoadFromEnumerable 
let trainer = 
    mlContext.Regression.Trainers.Sdca(
        labelColumnName= "Label",
        featureColumnName = "Features",
        maximumNumberOfIterations = Nullable 100
        )
let pipeline = 
    EstimatorChain()
        .Append(mlContext.Transforms.Concatenate("Features", "Size"))
        .Append(mlContext.Transforms.CopyColumns("Label", "Price"))
        .Append(trainer)
    |> downcastPipeline 

let model = pipeline.Fit houseData

let newSize = [| {Size = 2.5f; Price = 0.f} |] 
let prediction = 
    newSize
    |> mlContext.Data.LoadFromEnumerable
    |> model.Transform
    |> fun x -> x.GetColumn<float32> "Score"
    |> Seq.toArray
printfn "Predicted price for size: %.0f sq ft= %.2fk" (newSize.[0].Size * 1000.f) (prediction.[0] * 100.f)

result

Predicted price for size: 2500 sq ft= 270.69k

Jon Wood's video F# ML.Net is also a good place to start using ML.Net in F#.

Jose Vu
  • 621
  • 5
  • 13
4

I've also struggled with this. Try with this helper function:

let append (estimator : IEstimator<'a>) (pipeline : IEstimator<'b>)  =
      match pipeline with
      | :? IEstimator<ITransformer> as p ->
          p.Append estimator
      | _ -> failwith "The pipeline has to be an instance of IEstimator<ITransformer>."

let pipeline = 
    mlContext.Transforms.Concatenate("Features",[|"Size"|])
    |> append(mlContext.Regression.Trainers.Sdca(labelColumnName = "Price", maximumNumberOfIterations = Nullable(100)))
FRocha
  • 942
  • 7
  • 11