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.