void New_DecomposableTrainAndPredict()
        {
            var dataPath = GetDataPath(IrisDataPath);

            using (var env = new TlcEnvironment())
            {
                var data = new MyTextLoader(env, MakeIrisTextLoaderArgs())
                           .FitAndRead(new MultiFileSource(dataPath));

                var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
                               .Append(new MyTermTransform(env, "Label"), TransformerScope.TrainTest)
                               .Append(new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments {
                    MaxIterations = 100, Shuffle = true, NumThreads = 1
                }, "Features", "Label"))
                               .Append(new MyKeyToValueTransform(env, "PredictedLabel"));

                var model  = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring);
                var engine = new MyPredictionEngine <IrisDataNoLabel, IrisPrediction>(env, model);

                var testLoader = new TextLoader(env, MakeIrisTextLoaderArgs(), new MultiFileSource(dataPath));
                var testData   = testLoader.AsEnumerable <IrisData>(env, false);
                foreach (var input in testData.Take(20))
                {
                    var prediction = engine.Predict(input);
                    Assert.True(prediction.PredictedLabel == input.Label);
                }
            }
        }
示例#2
0
        public void New_Metacomponents()
        {
            var dataPath = GetDataPath(IrisDataPath);

            using (var env = new TlcEnvironment())
            {
                var data = new MyTextLoader(env, MakeIrisTextLoaderArgs())
                           .FitAndRead(new MultiFileSource(dataPath));

                var sdcaTrainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments {
                    MaxIterations = 100, Shuffle = true, NumThreads = 1
                }, "Features", "Label");
                var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
                               .Append(new MyTermTransform(env, "Label"), TransformerScope.TrainTest)
                               .Append(new MyOva(env, sdcaTrainer))
                               .Append(new MyKeyToValueTransform(env, "PredictedLabel"));

                var model = pipeline.Fit(data);
            }
        }
示例#3
0
        void New_Extensibility()
        {
            var dataPath = GetDataPath(IrisDataPath);

            using (var env = new TlcEnvironment())
            {
                var data = new TextLoader(env, MakeIrisTextLoaderArgs())
                           .Read(new MultiFileSource(dataPath));

                Action <IrisData, IrisData> action = (i, j) =>
                {
                    j.Label       = i.Label;
                    j.PetalLength = i.SepalLength > 3 ? i.PetalLength : i.SepalLength;
                    j.PetalWidth  = i.PetalWidth;
                    j.SepalLength = i.SepalLength;
                    j.SepalWidth  = i.SepalWidth;
                };
                var pipeline = new MyConcatTransform(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
                               .Append(new MyLambdaTransform <IrisData, IrisData>(env, action), TransformerScope.TrainTest)
                               .Append(new TermEstimator(env, "Label"), TransformerScope.TrainTest)
                               .Append(new SdcaMultiClassTrainer(env, new SdcaMultiClassTrainer.Arguments {
                    MaxIterations = 100, Shuffle = true, NumThreads = 1
                }, "Features", "Label"))
                               .Append(new KeyToValueEstimator(env, "PredictedLabel"));

                var model  = pipeline.Fit(data).GetModelFor(TransformerScope.Scoring);
                var engine = model.MakePredictionFunction <IrisDataNoLabel, IrisPrediction>(env);

                var testLoader = TextLoader.ReadFile(env, MakeIrisTextLoaderArgs(), new MultiFileSource(dataPath));
                var testData   = testLoader.AsEnumerable <IrisData>(env, false);
                foreach (var input in testData.Take(20))
                {
                    var prediction = engine.Predict(input);
                    Assert.True(prediction.PredictedLabel == input.Label);
                }
            }
        }