void New_MultithreadedPrediction() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var reader = new TextLoader(env, MakeSentimentTextLoaderArgs()); var data = reader.Read(new MultiFileSource(dataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(data); // Create prediction engine and test predictions. var engine = new MyPredictionEngine <SentimentData, SentimentPrediction>(env, model); // Take a couple examples out of the test data and run predictions on top. var testData = reader.Read(new MultiFileSource(GetDataPath(SentimentTestPath))) .AsEnumerable <SentimentData>(env, false); Parallel.ForEach(testData, (input) => { lock (engine) { var prediction = engine.Predict(input); } }); } }
public void New_ReconfigurablePrediction() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var dataReader = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .Fit(new MultiFileSource(dataPath)); var data = dataReader.Read(new MultiFileSource(dataPath)); var testData = dataReader.Read(new MultiFileSource(testDataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Fit(data); var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); var trainData = pipeline.Transform(data); var model = trainer.Fit(trainData); var scoredTest = model.Transform(pipeline.Transform(testData)); var metrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments()).Evaluate(scoredTest, "Label", "Probability"); var newModel = new BinaryPredictionTransformer <IPredictorProducing <float> >(env, model.Model, trainData.Schema, model.FeatureColumn, threshold: 0.01f, thresholdColumn: DefaultColumnNames.Probability); var newScoredTest = newModel.Transform(pipeline.Transform(testData)); var newMetrics = new MyBinaryClassifierEvaluator(env, new BinaryClassifierEvaluator.Arguments { Threshold = 0.01f, UseRawScoreThreshold = false }).Evaluate(newScoredTest, "Label", "Probability"); } }
public void New_SimpleTrainAndPredict() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var reader = new TextLoader(env, MakeSentimentTextLoaderArgs()); var data = reader.Read(new MultiFileSource(dataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(data); // Create prediction engine and test predictions. var engine = new MyPredictionEngine <SentimentData, SentimentPrediction>(env, model); // Take a couple examples out of the test data and run predictions on top. var testData = reader.Read(new MultiFileSource(GetDataPath(SentimentTestPath))) .AsEnumerable <SentimentData>(env, false); foreach (var input in testData.Take(5)) { var prediction = engine.Predict(input); // Verify that predictions match and scores are separated from zero. Assert.Equal(input.Sentiment, prediction.Sentiment); Assert.True(input.Sentiment && prediction.Score > 1 || !input.Sentiment && prediction.Score < -1); } } }
void New_CrossValidation() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var data = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .FitAndRead(new MultiFileSource(dataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1, ConvergenceTolerance = 1f }, "Features", "Label")); var cv = new MyCrossValidation.BinaryCrossValidator(env) { NumFolds = 2 }; var cvResult = cv.CrossValidate(data, pipeline); } }
public void New_TrainWithInitialPredictor() { var dataPath = GetDataPath(SentimentDataPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var data = new TextLoader(env, MakeSentimentTextLoaderArgs()).Read(new MultiFileSource(dataPath)); // Pipeline. var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()); // Train the pipeline, prepare train set. var trainData = pipeline.FitAndTransform(data); // Train the first predictor. var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label"); var firstModel = trainer.Fit(trainData); // Train the second predictor on the same data. var secondTrainer = new MyAveragedPerceptron(env, new AveragedPerceptronTrainer.Arguments(), "Features", "Label"); var finalModel = secondTrainer.Train(trainData, firstModel.Model); } }
public void New_TrainWithValidationSet() { var dataPath = GetDataPath(SentimentDataPath); var validationDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { // Pipeline. var reader = new TextLoader(env, MakeSentimentTextLoaderArgs()); var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()); // Train the pipeline, prepare train and validation set. var data = reader.Read(new MultiFileSource(dataPath)); var preprocess = pipeline.Fit(data); var trainData = preprocess.Transform(data); var validData = preprocess.Transform(reader.Read(new MultiFileSource(validationDataPath))); // Train model with validation set. var trainer = new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments(), "Features", "Label"); var model = trainer.Train(trainData, validData); } }
public void New_IntrospectiveTraining() { var dataPath = GetDataPath(SentimentDataPath); var testDataPath = GetDataPath(SentimentTestPath); using (var env = new TlcEnvironment(seed: 1, conc: 1)) { var data = new MyTextLoader(env, MakeSentimentTextLoaderArgs()) .FitAndRead(new MultiFileSource(dataPath)); var pipeline = new MyTextTransform(env, MakeSentimentTextTransformArgs()) .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(data); // Get feature weights. VBuffer <float> weights = default; model.LastTransformer.Model.GetFeatureWeights(ref weights); } }