首页 > 解决方案 > 在 F# 中运行 ML.Net Iris 演示时,我是否使用了错误的 TextLoader?

问题描述

我是 F#/.NET 的新手,我正在尝试运行如何将介绍 ML.Net 演示转换为 F#?使用ML.NET 库,在 Visual Studio 上使用 F#,使用 Microsoft.ML (0.2.0)。

构建它时出现错误error FS0039: The type 'TextLoader' is not defined.

为了避免这种情况,我添加了这一行

open Microsoft.ML.Data

到源头。然后,然而,这条线

pipeline.Add(new TextLoader<IrisData>(dataPath,separator = ","))

触发器: error FS0033: The non-generic type 'Microsoft.ML.Data.TextLoader' does not expect any type arguments, but here is given 1 type argument(s)

更改为:

pipeline.Add(new TextLoader(dataPath,separator = ","))

产量: error FS0495: The object constructor 'TextLoader' has no argument or settable return property 'separator'. The required signature is TextLoader(filePath: string) : TextLoader.

更改为:

pipeline.Add(new TextLoader(dataPath))

使构建成功,但运行时代码失败 ArgumentOutOfRangeException: Column #1 not found in the dataset (it only has 1 columns),我认为是因为逗号分隔符没有正确拾取(顺便说一句,您可以在https://archive.ics.uci.edu/ml/找到并检查 iris 数据集机器学习数据库/iris/iris.data)。

pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','))

不会工作。

我知道TextLoader最近发生了变化(参见例如https://github.com/dotnet/machinelearning/issues/332),有人可以指出我做错了什么吗?

标签: f#ml.net

解决方案


F# 只是有一些不同的语法,可能需要一些时间来适应。它不使用new关键字来实例化新类并使用命名参数,=而不是使用:C# 中的命名参数。

所以对于 C# 中的这一行:

pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','))

在 F# 中是这样的:

pipeline.Add(TextLoader(dataPath).CreateFrom<IrisData>(separator=','))

推荐阅读