代码之家  ›  专栏  ›  技术社区  ›  michasaucer

将“数据”从新的TextLoader添加到现有的管道(版本。0.6)

  •  0
  • michasaucer  · 技术社区  · 8 年前

    我从中复制粘贴示例 ML.NET Cookbook 上面写着:

    // Create a new environment for ML.NET operations. It can be used for 
    exception tracking and logging, 
    // as well as the source of randomness.
    var env = new LocalEnvironment();
    
    // Create the reader: define the data columns and where to find them in the 
     text file.
     var reader = TextLoader.CreateReader(env, ctx => (
        // We read the first 11 values as a single float vector.
        FeatureVector: ctx.LoadFloat(0, 10),
        // Separately, read the target variable.
        Target: ctx.LoadFloat(11)
        ),
        // Default separator is tab, but we need a comma.
        separator: ',');
    
    
    // Now read the file (remember though, readers are lazy, so the actual 
    reading will happen when the data is accessed).
    var data = reader.Read(new MultiFileSource(dataPath));
    

    所以我开始在我的模型中实现它:

    using System;
    using Microsoft.ML.Legacy;
    using Microsoft.ML.Legacy.Data;
    using Microsoft.ML.Legacy.Transforms;
    using Microsoft.ML.Legacy.Trainers;
    using Microsoft.ML.Legacy.Models;
    using Microsoft.ML.Runtime.Data;
    
    public static PredictionModel<CancerData, CancerPrediction> Train()
        {
            var pipeline = new LearningPipeline();
            //0.6 way to upload data into model
            var env = new LocalEnvironment();
                var reader = Microsoft.ML.Runtime.Data.TextLoader.CreateReader(env, ctx => (
                FeatureVector: ctx.LoadFloat(0, 30),
                Target: ctx.LoadText(31)
                    ),
                separator: ';');
    
            var data = reader.Read(new MultiFileSource("Cancer-Train.csv"));
    
            //pipeline.Add(new TextLoader("Cancer-Train.csv").CreateFrom<CancerData>(useHeader: true, separator: ';'));
            pipeline.Add(new Dictionarizer(("Diagnosis", "Label")));
            pipeline.Add(data); //dont work, i just write it to show you what i want to do
    
            //below the 0.5 way to load data into pipeline!
            //pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
            //    "RadiusMean",
            //    "TextureMean",
            // .. and so on...
            //    "SymmetryWorst",
            //    "FractalDimensionWorst"));
            pipeline.Add(new StochasticDualCoordinateAscentBinaryClassifier());
            pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });
            PredictionModel<CancerData, CancerPrediction> model = pipeline.Train<CancerData, CancerPrediction>();
    
            model.WriteAsync(modelPath);
            return model;
    
        }
    

    var data 进入我的存在 pipeline ? 我需要做的是 var数据 从0.6版到0.5版 管道 ?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Gal Oshri    8 年前

    我不认为 LearningPipeline API与新的静态类型API兼容(例如。 TextLoader.CreateReader ). 这个 cookbook This 测试也可能有助于二进制分类。

    var env = new LocalEnvironment();
    var reader = Microsoft.ML.Runtime.Data.TextLoader.CreateReader(env, ctx => (
    FeatureVector: ctx.LoadFloat(0, 30),
    Target: ctx.LoadBool(31)
        ),
    separator: ';');
    
    var data = reader.Read(new MultiFileSource("Cancer-Train.csv"));
    
    BinaryClassificationContext bcc = new BinaryClassificationContext(env);
    
    var estimator = reader.MakeNewEstimator()
        .Append(row => (
            label: row.Target,
            features: row.FeatureVector.Normalize()))
        .Append(row => (
            row.label,
            score: bcc.Trainers.Sdca(row.label, row.features)))
        .Append(row => (
            row.label,
            row.score,
            predictedLabel: row.score.predictedLabel));
    
    var model = estimator.Fit(data);
    
    推荐文章