如何将 intro ML.Net 演示翻译成 F#?
How to translate the intro ML.Net demo to F#?
我正在查看这里的 cs 文件:
https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows
在我尝试将其转换为 F# 时,它编译得很好,但在 运行: FormatException: One of the identified items was in an invalid format
时抛出 System.Reflection.TargetInvocationException
。我错过了什么?
已编辑:之前使用过记录
open Microsoft.ML
open Microsoft.ML.Runtime.Api
open Microsoft.ML.Trainers
open Microsoft.ML.Transforms
open System
type IrisData =
[<Column("0")>] val mutable SepalLength : float
[<Column("1")>] val mutable SepalWidth : float
[<Column("2")>] val mutable PetalLength : float
[<Column("3")>] val mutable PetalWidth : float
[<Column("4");ColumnName("Label")>] val mutable Label : string
new(sepLen, sepWid, petLen, petWid, label) =
{ SepalLength = sepLen
SepalWidth = sepWid
PetalLength = petLen
PetalWidth = petWid
Label = label }
type IrisPrediction =
[<ColumnName("PredictedLabel")>] val mutable PredictedLabels : string
new() = { PredictedLabels = "Iris-setosa" }
[<EntryPoint>]
let main argv =
let pipeline = new LearningPipeline()
let dataPath = "iris.data.txt"
pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))
pipeline.Add(new Dictionarizer("Label"))
pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
pipeline.Add(new StochasticDualCoordinateAscentClassifier())
pipeline.Add(new PredictedLabelColumnOriginalValueConverter(PredictedLabelColumn = "PredictedLabel") )
let model = pipeline.Train<IrisData, IrisPrediction>()
let prediction = model.Predict(IrisData(3.3, 1.6, 0.2, 5.1,""))
Console.WriteLine("Predicted flower type is: {prediction.PredictedLabels}")
0 // return an integer exit code
您可能会在下面找到 ML tutorial 的有效 F# 代码版本,使用 Microsoft.ML 0.1.0(可能会因更新版本而中断)。使示例工作的代码与您的代码的两个主要区别都在 IrisData
和 IrisPrediction
类型定义中:
- 在具有无参数构造函数和public访问字段的 F# 中准确呈现 C# POCO
- 将 C#
float
正确移植到 F#,即 float32
这是代码
open Microsoft.ML
open Microsoft.ML.Runtime.Api
open Microsoft.ML.Trainers
open Microsoft.ML.Transforms
open System
type IrisData() =
[<Column("0")>]
[<DefaultValue>]
val mutable public SepalLength: float32
[<DefaultValue>]
[<Column("1")>]
val mutable public SepalWidth: float32
[<DefaultValue>]
[<Column("2")>]
val mutable public PetalLength:float32
[<DefaultValue>]
[<Column("3")>]
val mutable public PetalWidth:float32
[<DefaultValue>]
[<Column("4")>]
[<ColumnName("Label")>]
val mutable public Label:string
type IrisPrediction() =
[<ColumnName("PredictedLabel")>]
[<DefaultValue>]
val mutable public PredictedLabel : string
[<EntryPoint>]
let main argv =
let pipeline = new LearningPipeline()
let dataPath = "iris.data.txt"
let a = IrisPrediction()
pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))
pipeline.Add(new Dictionarizer("Label"))
pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
pipeline.Add(new StochasticDualCoordinateAscentClassifier())
pipeline.Add(new PredictedLabelColumnOriginalValueConverter(PredictedLabelColumn = "PredictedLabel") )
let model = pipeline.Train<IrisData, IrisPrediction>()
let x = IrisData()
x.SepalLength <- 3.3f
x.SepalWidth <- 1.6f
x.PetalLength <- 0.2f
x.PetalWidth <- 5.1f
let prediction = model.Predict(x)
printfn "Predicted flower type is: %s" prediction.PredictedLabel
0
及其产生的输出:
Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
Using 4 threads to train.
Automatically choosing a check frequency of 4.
Auto-tuning parameters: maxIterations = 9996.
Auto-tuning parameters: L2 = 2.668802E-05.
Auto-tuning parameters: L1Threshold (L1/L2) = 0.
Using best model from iteration 892.
Not training a calibrator because it is not needed.
Predicted flower type is: Iris-virginica
Press any key to continue . . .
我正在查看这里的 cs 文件:
https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows
在我尝试将其转换为 F# 时,它编译得很好,但在 运行: FormatException: One of the identified items was in an invalid format
时抛出 System.Reflection.TargetInvocationException
。我错过了什么?
已编辑:之前使用过记录
open Microsoft.ML
open Microsoft.ML.Runtime.Api
open Microsoft.ML.Trainers
open Microsoft.ML.Transforms
open System
type IrisData =
[<Column("0")>] val mutable SepalLength : float
[<Column("1")>] val mutable SepalWidth : float
[<Column("2")>] val mutable PetalLength : float
[<Column("3")>] val mutable PetalWidth : float
[<Column("4");ColumnName("Label")>] val mutable Label : string
new(sepLen, sepWid, petLen, petWid, label) =
{ SepalLength = sepLen
SepalWidth = sepWid
PetalLength = petLen
PetalWidth = petWid
Label = label }
type IrisPrediction =
[<ColumnName("PredictedLabel")>] val mutable PredictedLabels : string
new() = { PredictedLabels = "Iris-setosa" }
[<EntryPoint>]
let main argv =
let pipeline = new LearningPipeline()
let dataPath = "iris.data.txt"
pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))
pipeline.Add(new Dictionarizer("Label"))
pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
pipeline.Add(new StochasticDualCoordinateAscentClassifier())
pipeline.Add(new PredictedLabelColumnOriginalValueConverter(PredictedLabelColumn = "PredictedLabel") )
let model = pipeline.Train<IrisData, IrisPrediction>()
let prediction = model.Predict(IrisData(3.3, 1.6, 0.2, 5.1,""))
Console.WriteLine("Predicted flower type is: {prediction.PredictedLabels}")
0 // return an integer exit code
您可能会在下面找到 ML tutorial 的有效 F# 代码版本,使用 Microsoft.ML 0.1.0(可能会因更新版本而中断)。使示例工作的代码与您的代码的两个主要区别都在 IrisData
和 IrisPrediction
类型定义中:
- 在具有无参数构造函数和public访问字段的 F# 中准确呈现 C# POCO
- 将 C#
float
正确移植到 F#,即float32
这是代码
open Microsoft.ML
open Microsoft.ML.Runtime.Api
open Microsoft.ML.Trainers
open Microsoft.ML.Transforms
open System
type IrisData() =
[<Column("0")>]
[<DefaultValue>]
val mutable public SepalLength: float32
[<DefaultValue>]
[<Column("1")>]
val mutable public SepalWidth: float32
[<DefaultValue>]
[<Column("2")>]
val mutable public PetalLength:float32
[<DefaultValue>]
[<Column("3")>]
val mutable public PetalWidth:float32
[<DefaultValue>]
[<Column("4")>]
[<ColumnName("Label")>]
val mutable public Label:string
type IrisPrediction() =
[<ColumnName("PredictedLabel")>]
[<DefaultValue>]
val mutable public PredictedLabel : string
[<EntryPoint>]
let main argv =
let pipeline = new LearningPipeline()
let dataPath = "iris.data.txt"
let a = IrisPrediction()
pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))
pipeline.Add(new Dictionarizer("Label"))
pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
pipeline.Add(new StochasticDualCoordinateAscentClassifier())
pipeline.Add(new PredictedLabelColumnOriginalValueConverter(PredictedLabelColumn = "PredictedLabel") )
let model = pipeline.Train<IrisData, IrisPrediction>()
let x = IrisData()
x.SepalLength <- 3.3f
x.SepalWidth <- 1.6f
x.PetalLength <- 0.2f
x.PetalWidth <- 5.1f
let prediction = model.Predict(x)
printfn "Predicted flower type is: %s" prediction.PredictedLabel
0
及其产生的输出:
Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
Using 4 threads to train.
Automatically choosing a check frequency of 4.
Auto-tuning parameters: maxIterations = 9996.
Auto-tuning parameters: L2 = 2.668802E-05.
Auto-tuning parameters: L1Threshold (L1/L2) = 0.
Using best model from iteration 892.
Not training a calibrator because it is not needed.
Predicted flower type is: Iris-virginica
Press any key to continue . . .