如何将"Hello ML.NET World"示例转换为 F#?

  • 本文关键字:转换 NET Hello ML World f# ml.net
  • 更新时间 :
  • 英文 :


我正试图将"Hello ML.NET World"示例从C#转换为F#(代码复制如下(,但我收到了一个关于不兼容类型的F#编译器错误。

我看过几篇关于ML.NET和F#的博客文章,但它们都使用了较旧的API,其中涉及显式创建LearningPipeline对象。据我所知,这个API已经被删除了。

C#中有问题的行是训练管道的行:

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

我试着把F#翻译成这样:

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

但是,我得到了一个编译器错误:Type constraint mismatch: The type 'Transforms.ColumnConcatenatingEstimator' is not compatible with the type IEstimator<ITransformer>'

我还尝试过将ColumnConcatenatingEstimator显式下转换为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)

这会稍微改变编译器错误中的类型。新消息指示IEstimator<ColumnConcatenatingTransformer>IEstimator<ITransformer>不兼容。

看起来我需要显式地将泛型中的ColumnConcatenatingTransformer向下转换为ITransformer,但我不确定如何在F#中做到这一点。这可能吗?

作为参考,以下是我正在尝试改编的来自微软的完整C#代码:

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
}
}

(编辑:只是为了澄清,这与如何将介绍ML.NET演示翻译成F#不是同一个问题。(这是一个不同的代码示例,它使用了ML.NET的新版本。该答案中的Microsoft链接现在似乎也断了

ML.NET是在考虑C#的情况下构建的,因此有时转换为F#需要在各处添加Nullablefloat32。这是我的版本,我去掉了PredictionEngine,将Sdca作为训练器,并使用EstimatorChain()附加和创建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)

结果

Predicted price for size: 2500 sq ft= 270.69k

Jon Wood的视频F#ML.Net也是开始在F#中使用ML.Net的好地方。

我也遇到过这种情况。试试这个辅助功能:

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)))

最新更新