【问题标题】:How to return transformed data from an ML.Net pipeline before a predictor is applied如何在应用预测器之前从 ML.Net 管道返回转换后的数据
【发布时间】:2018-09-21 17:57:13
【问题描述】:

这是从 TaxiFarePrediction 示例复制的 ML.Net 管道对象的创建。

        LearningPipeline pipeline = new LearningPipeline
        {
            new TextLoader(TrainDataPath).CreateFrom<TaxiTrip>(separator:','),
            new ColumnCopier(("FareAmount", "Label")),
            new CategoricalOneHotVectorizer("VendorId","RateCode","PaymentType"),
            new ColumnConcatenator("Features","VendorId","RateCode","PassengerCount","TripDistance","PaymentType"),
            new FastTreeRegressor()
        };

基本上,我想在应用 ColumnCopier、CategoricalOneHotVectorizer 和 ColumnConcatenator 之后返回数据。

【问题讨论】:

  • 为什么不让回归器为空并在管道中检索数据? (没试过,我只是想知道)
  • @KevinAvignon 如何检索管道中的数据?
  • 也就是说,您希望 pipleline 对您的数据应用转换并返回给您一个新的转换数据集?
  • @YuraZaletskyy 是的,我想返回转换后的数据集。
  • @Gaspare,你试过我的回答了吗?

标签: c# machine-learning .net-core ml.net


【解决方案1】:

用于调试器中的可视化微软编程类

LearningPipelineDebugProxy

该类有两个信息丰富的字段:行和列。当然,用于调试的类并不是很容易创建,因为它是内部密封的:

namespace Microsoft.ML
{
  /// <summary>
  /// The debug proxy class for a LearningPipeline.
  /// Displays the current columns and values in the debugger Watch window.
  /// </summary>
  internal sealed class LearningPipelineDebugProxy
  {

根据源代码。 在这种情况下,如果调试器可视化还不够,我会使用反射。为了在 TaxiTrip 实例中创建 LearningPipelineDebugProxy 实例,我使用了以下技巧:

  1. 通过 CreateInstance 方法创建 LearningPipelineDebugProxy 实例
  2. 添加了通过反射获取所需字段的方法。

在代码中,片段看起来像这样:

    public static PredictionModel<TaxiTrip, TaxiTripFarePrediction> Train()
    {
        var pipeline = new LearningPipeline();
        pipeline.Add(new TextLoader(_datapath).CreateFrom<TaxiTrip>(useHeader: true, separator: ','));


        Type obj = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).
            Where(t => String.Equals(t.Name, "LearningPipelineDebugProxy", StringComparison.Ordinal)).First();


        var instObject = Activator.CreateInstance(obj, new []{pipeline});

        pipeline.Add(new ColumnCopier(("FareAmount", "Label")));
        pipeline.Add(new CategoricalOneHotVectorizer("VendorId", "RateCode", "PaymentType"));
        pipeline.Add(new ColumnConcatenator("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"));

        var rws = GetPropValue(instObject, "Rows");
        var clms = GetPropValue(instObject, "Columns");

        pipeline.Add(new FastTreeRegressor());

        PredictionModel<TaxiTrip, TaxiTripFarePrediction> model = pipeline.Train<TaxiTrip, TaxiTripFarePrediction>();
        return model;
    }

    public static object GetPropValue(object src, string propName)
    {
        return src.GetType().GetProperty(propName).GetValue(src, null);
    }

在调试器窗口中,不仅在调试器中,行变得可用:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多