我怎样才能翻译这句话;“你好,ML.NET世界”;F#的例子?
我正试图将C#转换为F#(下面复制的代码),但是我得到了一个关于不兼容类型的F#编译器错误 我看过一些关于ML.NET和F#的博客文章,但它们都使用了旧的API,其中包括显式创建LearningPipeline对象。据我所知,这个API已经被删除了 C#中有问题的线路是用于训练管道的线路:我怎样才能翻译这句话;“你好,ML.NET世界”;F#的例子?,f#,ml.net,F#,Ml.net,我正试图将C#转换为F#(下面复制的代码),但是我得到了一个关于不兼容类型的F#编译器错误 我看过一些关于ML.NET和F#的博客文章,但它们都使用了旧的API,其中包括显式创建LearningPipeline对象。据我所知,这个API已经被删除了 C#中有问题的线路是用于训练管道的线路: var pipeline = mlContext.Transforms.Concatenate("Features", new[] { "Size" }) .Append(mlContext.Regr
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)))
但是,我遇到了一个编译器错误:类型约束不匹配:类型“Transforms.ColumnConcatenateImator”与类型“IEstimator”不兼容。
我还尝试显式地将ColumnConnegateImator向下转换为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)
let pipeline'(mlContext:mlContext)=
让concat=mlContext.Transforms.Concatenate(“Features”、[|“Size”|])
让scda=mlContext.Regression.Trainers.Sdca(labelColumnName=“Price”,maximumNumberOfIterations=Nullable(100))
让concatAsEstimator=concat:>IEstimator
concatAsEstimator.Append(scda)
这会稍微更改编译器错误中的类型。新消息表明IEstimator
与IEstimator
不兼容
看起来我需要显式地将泛型中的ColumnConnegatingTransformer向下转换为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
}
}
使用系统;
使用Microsoft.ML;
使用Microsoft.ML.Data;
班级计划
{
公共类房屋数据
{
公共浮点大小{get;set;}
公开浮动价格{get;set;}
}
公共类预测
{
[栏目名称(“分数”)]
公开浮动价格{get;set;}
}
静态void Main(字符串[]参数)
{
MLContext MLContext=新的MLContext();
//1.导入或创建培训数据
房屋数据[]房屋数据={
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.指定数据准备和模型培训管道
var pipeline=mlContext.Transforms.Concatenate(“功能”,新[]{“大小”})
.Append(mlContext.Regression.Trainers.Sdca(labelColumnName:“Price”,maximumNumberOfIterations:100));
//3.列车模型
var模型=pipeline.Fit(培训数据);
//4.作出预测
var size=new HouseData(){size=2.5F};
var price=mlContext.Model.CreatePredictionEngine(Model).Predict(size);
Console.WriteLine($“尺寸预测价格:{size.size*1000}平方英尺={price.price*100:C}k”);
//面积预测价格:2500平方英尺=26198万美元
}
}
(编辑:只是为了澄清,这与问题不同。)这是一个不同的代码示例,它使用了更新版本的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)))
let append(估计量:IEstimator)=
将管道与
| :? 作为p->
p、 附加估计器
|->failwith“管道必须是IEstimator的实例。”
让管道=
mlContext.Transforms.Concatenate(“特征”、[|“大小”|])
|>追加(mlContext.Regression.Trainers.Sdca(labelColumnName=“Price”,maximumNumberOfIterations=Nullable(100)))
ML.NET在构建时考虑了C,因此有时转换为F需要在任何地方添加null
和float32
。
这是我的版本,我去掉了PredictionEngine
,将Sdca
作为培训师,并使用EstimatorChain()
附加和创建IEstimator
开放系统
打开Microsoft.ML
打开Microsoft.ML.Data
类型数据=
{
尺寸:32
价格:32
}
下泄管道(x:IEstimator)=
将x与
| :? 激励因子为y->y
|->failwith“downcastPipeline:预期激励不足”
让mlContext=mlContext(可为null的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
让培训师=
mlContext.returnal.Trainers.Sdca(
labelColumnName=“Label”,
featureColumnName=“功能”,
maximumNumberOfIterations=Nullable 100
)
让管道=
估算链()
.Append(mlContext.Transforms.Concatenate(“特征”、“大小”))
.Append(mlContext.Transforms.CopyColumns(“标签”、“价格”))
.附加(培训师)
|>下行管道
让模型=管道。拟合房屋数据
让newSize=[|{Size=2.5f;Price=0.f}}]
让预测=
新闻化
|>mlContext.Data.LoadFromEnumerable
|>模型转换
|>乐趣x->x.GetColumn“分数”
|>序号:toArray
printfn“尺寸的预测价格:%.0f平方英尺=%.2fk”(新闻尺寸[0]。尺寸*1000.f)(预测[0]*100.f)
结果
Predicted price for size: 2500 sq ft= 270.69k
Jon Wood的视频也是在F#中开始使用ML.Net的好地方。尝试使用
Estimator Chain()
启动管道。可能类似于let concat=EstimatorChain().Append(mlContext.Transforms.Concatenate…)