public void EvaluateStaticApi() { ConsoleWriteHeader("Loading model"); Console.WriteLine($"Model loaded: {modelLocation}"); // Load the model ITransformer loadedModel; using (var f = new FileStream(modelLocation, FileMode.Open)) loadedModel = TransformerChain.LoadFrom(env, f); // Make prediction function (input = ImageNetData, output = ImageNetPrediction) var predictor = loadedModel.MakePredictionFunction <ImageNetData, ImageNetPrediction>(env); // Read csv file into List<ImageNetData> var testData = ImageNetData.ReadFromCsv(dataLocation, imagesFolder).ToList(); ConsoleWriteHeader("Making classifications"); // There is a bug (https://github.com/dotnet/machinelearning/issues/1138), // that always buffers the response from the predictor // so we have to make a copy-by-value op everytime we get a response // from the predictor testData .Select(td => new { td, pred = predictor.Predict(td) }) .Select(pr => (pr.td.ImagePath, pr.pred.PredictedLabelValue, pr.pred.Score)) .ToList() .ForEach(pr => ConsoleWriteImagePrediction(pr.ImagePath, pr.PredictedLabelValue, pr.Score.Max())); }
private static void PredictWithModelLoadedFromFile(IrisData sampleData) { // Test with Loaded Model from .zip file using (var env = new LocalEnvironment()) { ITransformer loadedModel; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = TransformerChain.LoadFrom(env, stream); } // Create prediction engine and make prediction. var prediction = loadedModel.MakePredictionFunction <IrisData, IrisPrediction>(env).Predict( new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Console.WriteLine(); Console.WriteLine($"Clusters assigned for setosa flowers:" + prediction.SelectedClusterId); } }
public void TestEstimatorSaveLoad() { using (var env = new ConsoleEnvironment()) { var dataFile = GetDataPath("images/images.tsv"); var imageFolder = Path.GetDirectoryName(dataFile); var data = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile)); var pipe = new ImageLoaderEstimator(env, imageFolder, ("ImagePath", "ImageReal")) .Append(new ImageResizerEstimator(env, "ImageReal", "ImageReal", 100, 100)) .Append(new ImagePixelExtractorEstimator(env, "ImageReal", "ImagePixels")) .Append(new ImageGrayscaleEstimator(env, ("ImageReal", "ImageGray"))); pipe.GetOutputSchema(Core.Data.SchemaShape.Create(data.Schema)); var model = pipe.Fit(data); using (var file = env.CreateTempFile()) { using (var fs = file.CreateWriteStream()) model.SaveTo(env, fs); var model2 = TransformerChain.LoadFrom(env, file.OpenReadStream()); var newCols = ((ImageLoaderTransform)model2.First()).Columns; var oldCols = ((ImageLoaderTransform)model.First()).Columns; Assert.True(newCols .Zip(oldCols, (x, y) => x == y) .All(x => x)); } } Done(); }
public static float carregar(float Prioridade, float Punicao, float Dispensa, float UltimoDiaSemana, float DiaAtualSemanaServico) { string pathModel = "model.pb"; if (!File.Exists(pathModel)) { WaitForm waitForm = new WaitForm("Modelo em treinamento...", ServicoPredicaoControl.criar_e_treinar); waitForm.ShowDialog(); } FileStream fs = File.OpenRead(pathModel); var env = new LocalEnvironment(); var model = TransformerChain.LoadFrom(env, fs); var prediction = model.MakePredictionFunction<ModeloPredicao, ServicoPredicao>(env).Predict( new ModeloPredicao() { prioridade = Prioridade, punicao = Punicao, dispensa = Dispensa, ultimoDiaSemana = UltimoDiaSemana, diaAtualSemanaServico = DiaAtualSemanaServico } ); fs.Close(); return prediction.PredictedLabels; }
public void Evaluate() { ITransformer model; using (var file = File.OpenRead(modelLocation)) { model = TransformerChain .LoadFrom(env, file); } var reader = new TextLoader(env, new TextLoader.Arguments { Column = new[] { new TextLoader.Column("Features", DataKind.R4, new[] { new TextLoader.Range(0, 31) }), new TextLoader.Column("LastName", DataKind.Text, 32) }, HasHeader = true, Separator = "," }); ConsoleWriteHeader("Read model"); Console.WriteLine($"Model location: {modelLocation}"); var data = reader.Read(new MultiFileSource(pivotDataLocation)); var predictions = model.Transform(data) .AsEnumerable <ClusteringPrediction>(env, false) .ToArray(); SaveCustomerSegmentationPlot(predictions, plotLocation); OpenChartInDefaultWindow(plotLocation); }
private static ITransformer LoadModel() { using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return(TransformerChain.LoadFrom(s_environment, stream)); } }
public ITransformer LoadModelFromZipFile(string modelPath) { using (var stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { _trainedModel = TransformerChain.LoadFrom(_mlContext, stream); } return(_trainedModel); }
public static ITransformer ReadModel(this LocalEnvironment env, string modelLocation) { ITransformer model; using (var file = File.OpenRead(@modelLocation)) { model = TransformerChain.LoadFrom(env, file); } return(model); }
public static ITransformer LoadModelFromZipFile(MLContext mlContext, string modelPath) { ITransformer loadedModel; using (var stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = TransformerChain.LoadFrom(mlContext, stream); } return(loadedModel); }
public ITransformer LoadModelFromZipFile(string modelPath) { using (var stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { TrainedModel = TransformerChain.LoadFrom(_mlContext, stream); } // Create prediction engine related to the loaded trained model PredictionFunction = TrainedModel.MakePredictionFunction <TObservation, TPrediction>(_mlContext); return(TrainedModel); }
private bool LoadModel() { using (var stream = File.OpenRead(_modelName)) { var env = new LocalEnvironment(); ITransformer loadedModel = TransformerChain.LoadFrom(env, stream); _predictionFunction = loadedModel.MakePredictionFunction <PEModelData, PEPrediction>(env); return(true); } }
/// <summary> /// This function creates a prediction engine from the model located in the <paramref name="modelPath"/>. /// </summary> private PredictionFunction <CountryData, CountrySalesPrediction> CreatePredictionEngineAsync(string modelPath) { var env = new LocalEnvironment(seed: 1); //Seed set to any number so you have a deterministic environment ITransformer model; using (var file = File.OpenRead(modelPath)) { model = TransformerChain .LoadFrom(env, file); } return(model.MakePredictionFunction <CountryData, CountrySalesPrediction>(env)); }
private void LoadModel() { // Here the model is being loaded from a file. We could also embed the model in an // assembly as a resource. This would then allow us to update the model via a NuGet // package. using (FileStream fileStream = new FileStream("test-model.zip", FileMode.Open, FileAccess.Read, FileShare.Read)) { // The MLContext is the starting point for all ML things using ML.Net. MLContext context = new MLContext(); // Build the model from the data contained within the zip file. TransformerChain <ITransformer> model = TransformerChain.LoadFrom(context, fileStream); // Create the predictor we'll use whenever the user clicks a button. _predictor = model.CreatePredictionEngine <SentimentData, SentimentPrediction>(context); } }
public void New_TrainSaveModelAndPredict() { 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 TextTransform(env, "SentimentText", "Features") .Append(new LinearClassificationTrainer(env, new LinearClassificationTrainer.Arguments { NumThreads = 1 }, "Features", "Label")); // Train. var model = pipeline.Fit(data); ITransformer loadedModel; using (var file = env.CreateTempFile()) { // Save model. using (var fs = file.CreateWriteStream()) model.SaveTo(env, fs); // Load model. loadedModel = TransformerChain.LoadFrom(env, file.OpenReadStream()); } // Create prediction engine and test predictions. var engine = loadedModel.MakePredictionFunction <SentimentData, SentimentPrediction>(env); // 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); } } }
public static string Predict(GitHubIssue issue) { using (var env = new LocalEnvironment()) { ITransformer loadedModel; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = TransformerChain.LoadFrom(env, stream); } // Create prediction engine and make prediction. var engine = loadedModel.MakePredictionFunction <GitHubIssue, GitHubIssuePrediction>(env); var prediction = engine.Predict(issue); return(prediction.Area); } }
public void TrainSaveModelAndPredict() { var ml = new MLContext(seed: 1, conc: 1); var data = ml.Data.LoadFromTextFile <SentimentData>(GetDataPath(TestDatasets.Sentiment.trainFilename), hasHeader: true); // Pipeline. var pipeline = ml.Transforms.Text.FeaturizeText("Features", "SentimentText") .AppendCacheCheckpoint(ml) .Append(ml.BinaryClassification.Trainers.StochasticDualCoordinateAscentNonCalibrated( new SdcaNonCalibratedBinaryTrainer.Options { NumberOfThreads = 1 })); // Train. var model = pipeline.Fit(data); var modelPath = GetOutputPath("temp.zip"); // Save model. using (var file = File.Create(modelPath)) model.SaveTo(ml, file); // Load model. ITransformer loadedModel; using (var file = File.OpenRead(modelPath)) loadedModel = TransformerChain.LoadFrom(ml, file); // Create prediction engine and test predictions. var engine = loadedModel.CreatePredictionEngine <SentimentData, SentimentPrediction>(ml); // Take a couple examples out of the test data and run predictions on top. var testData = ml.Data.CreateEnumerable <SentimentData>( ml.Data.LoadFromTextFile <SentimentData>(GetDataPath(TestDatasets.Sentiment.testFilename), hasHeader: true), 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); } }
public void New_TrainSaveModelAndPredict() { var ml = new MLContext(seed: 1, conc: 1); var reader = ml.Data.TextReader(MakeSentimentTextLoaderArgs()); var data = reader.Read(GetDataPath(TestDatasets.Sentiment.trainFilename)); // Pipeline. var pipeline = ml.Transforms.Text.FeaturizeText("SentimentText", "Features") .AppendCacheCheckpoint(ml) .Append(ml.BinaryClassification.Trainers.StochasticDualCoordinateAscent("Label", "Features", advancedSettings: s => s.NumThreads = 1)); // Train. var model = pipeline.Fit(data); var modelPath = GetOutputPath("temp.zip"); // Save model. using (var file = File.Create(modelPath)) model.SaveTo(ml, file); // Load model. ITransformer loadedModel; using (var file = File.OpenRead(modelPath)) loadedModel = TransformerChain.LoadFrom(ml, file); // Create prediction engine and test predictions. var engine = loadedModel.MakePredictionFunction <SentimentData, SentimentPrediction>(ml); // Take a couple examples out of the test data and run predictions on top. var testData = reader.Read(GetDataPath(TestDatasets.Sentiment.testFilename)) .AsEnumerable <SentimentData>(ml, 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); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [Blob("models/customer-churn.zip", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream modelStream, ILogger log) { var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var data = JsonConvert.DeserializeObject <CustomerChurnPredictionData>(requestBody); log.LogInformation("Loading model from blob storage"); var env = new LocalEnvironment(); var model = TransformerChain.LoadFrom(env, modelStream); var predictor = model.MakePredictionFunction <CustomerChurnPredictionData, CustomerChurnPredictionResult>(env); log.LogInformation("Scoring sample for customer churn"); var result = predictor.Predict(data); return(new OkObjectResult(result)); }
public void TestEstimatorSaveLoad() { IHostEnvironment env = new MLContext(); var dataFile = GetDataPath("images/images.tsv"); var imageFolder = Path.GetDirectoryName(dataFile); var data = TextLoader.Create(env, new TextLoader.Arguments() { Columns = new[] { new TextLoader.Column("ImagePath", DataKind.TX, 0), new TextLoader.Column("Name", DataKind.TX, 1), } }, new MultiFileSource(dataFile)); var pipe = new ImageLoadingEstimator(env, imageFolder, ("ImageReal", "ImagePath")) .Append(new ImageResizingEstimator(env, "ImageReal", 100, 100, "ImageReal")) .Append(new ImagePixelExtractingEstimator(env, "ImagePixels", "ImageReal")) .Append(new ImageGrayscalingEstimator(env, ("ImageGray", "ImageReal"))); pipe.GetOutputSchema(Core.Data.SchemaShape.Create(data.Schema)); var model = pipe.Fit(data); var tempPath = Path.GetTempFileName(); using (var file = new SimpleFileHandle(env, tempPath, true, true)) { using (var fs = file.CreateWriteStream()) model.SaveTo(env, fs); var model2 = TransformerChain.LoadFrom(env, file.OpenReadStream()); var newCols = ((ImageLoaderTransformer)model2.First()).Columns; var oldCols = ((ImageLoaderTransformer)model.First()).Columns; Assert.True(newCols .Zip(oldCols, (x, y) => x == y) .All(x => x)); } Done(); }
private static void PredictWithModelLoadedFromFile(SentimentIssue sampleStatement) { // Test with Loaded Model from .zip file using (var env2 = new LocalEnvironment()) { ITransformer loadedModel; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = TransformerChain.LoadFrom(env2, stream); } // Create prediction engine and make prediction. var engine = loadedModel.MakePredictionFunction <SentimentIssue, SentimentPrediction>(env2); var predictionFromLoaded = engine.Predict(sampleStatement); Console.WriteLine(); Console.WriteLine("=============== Test of model with a sample ==============="); Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(predictionFromLoaded.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {predictionFromLoaded.Probability} "); } }
public static void IidSpikeDetectorPrediction() { // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, // as well as the source of randomness. var ml = new MLContext(); // Generate sample series data with a spike const int Size = 10; var data = new List <IidSpikeData>(Size); for (int i = 0; i < Size / 2; i++) { data.Add(new IidSpikeData(5)); } // This is a spike data.Add(new IidSpikeData(10)); for (int i = 0; i < Size / 2; i++) { data.Add(new IidSpikeData(5)); } // Convert data to IDataView. var dataView = ml.CreateStreamingDataView(data); // Setup IidSpikeDetector arguments string outputColumnName = nameof(IidSpikePrediction.Prediction); string inputColumnName = nameof(IidSpikeData.Value); var args = new IidSpikeDetector.Arguments() { Source = inputColumnName, Name = outputColumnName, Confidence = 95, // The confidence for spike detection in the range [0, 100] PvalueHistoryLength = Size / 4 // The size of the sliding window for computing the p-value; shorter windows are more sensitive to spikes. }; // The transformed model. ITransformer model = new IidSpikeEstimator(ml, args).Fit(dataView); // Create a time series prediction engine from the model. var engine = model.CreateTimeSeriesPredictionFunction <IidSpikeData, IidSpikePrediction>(ml); for (int index = 0; index < 5; index++) { // Anomaly spike detection. var prediction = engine.Predict(new IidSpikeData(5)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2]); } // Spike. var spikePrediction = engine.Predict(new IidSpikeData(10)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 10, spikePrediction.Prediction[0], spikePrediction.Prediction[1], spikePrediction.Prediction[2]); // Checkpoint the model. var modelPath = "temp.zip"; engine.CheckPoint(ml, modelPath); // Load the model. using (var file = File.OpenRead(modelPath)) model = TransformerChain.LoadFrom(ml, file); for (int index = 0; index < 5; index++) { // Anomaly spike detection. var prediction = engine.Predict(new IidSpikeData(5)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2]); } // Data Alert Score P-Value // 5 0 5.00 0.50 // 5 0 5.00 0.50 // 5 0 5.00 0.50 // 5 0 5.00 0.50 // 5 0 5.00 0.50 // 10 1 10.00 0.00 <-- alert is on, predicted spike (check-point model) // 5 0 5.00 0.26 <-- load model from disk. // 5 0 5.00 0.26 // 5 0 5.00 0.50 // 5 0 5.00 0.50 // 5 0 5.00 0.50 }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static void TestPrediction(string outputModelPath = "product_month_fastTreeTweedie.zip") { ConsoleWriteHeader("Testing Product Unit Sales Forecast model"); // Read the model that has been previously saved by the method SaveModel var env = new LocalEnvironment(seed: 1); //Seed set to any number so you have a deterministic environment ITransformer model; using (var file = File.OpenRead(outputModelPath)) { model = TransformerChain .LoadFrom(env, file); } var predictor = model.MakePredictionFunction <ProductData, ProductUnitPrediction>(env); Console.WriteLine("** Testing Product 1 **"); // Build sample data ProductData dataSample = new ProductData() { productId = "263", month = 10, year = 2017, avg = 91, max = 370, min = 1, count = 10, prev = 1675, units = 910 }; //model.Predict() predicts the nextperiod/month forecast to the one provided ProductUnitPrediction prediction = predictor.Predict(dataSample); Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Real value (units): 551, Forecast Prediction (units): {prediction.Score}"); dataSample = new ProductData() { productId = "263", month = 11, year = 2017, avg = 29, max = 221, min = 1, count = 35, prev = 910, units = 551 }; //model.Predict() predicts the nextperiod/month forecast to the one provided prediction = predictor.Predict(dataSample); Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecast Prediction (units): {prediction.Score}"); Console.WriteLine(" "); Console.WriteLine("** Testing Product 2 **"); dataSample = new ProductData() { productId = "988", month = 10, year = 2017, avg = 43, max = 220, min = 1, count = 25, prev = 1036, units = 1094 }; prediction = predictor.Predict(dataSample); Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Real Value (units): 1076, Forecasting (units): {prediction.Score}"); dataSample = new ProductData() { productId = "988", month = 11, year = 2017, avg = 41, max = 225, min = 4, count = 26, prev = 1094, units = 1076 }; prediction = predictor.Predict(dataSample); Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecasting (units): {prediction.Score}"); }
public void ChangePointDetectionWithSeasonalityPredictionEngineNoColumn() { const int ChangeHistorySize = 10; const int SeasonalitySize = 10; const int NumberOfSeasonsInTraining = 5; const int MaxTrainingSize = NumberOfSeasonsInTraining * SeasonalitySize; List <Data> data = new List <Data>(); var ml = new MLContext(seed: 1, conc: 1); var dataView = ml.CreateStreamingDataView(data); for (int j = 0; j < NumberOfSeasonsInTraining; j++) { for (int i = 0; i < SeasonalitySize; i++) { data.Add(new Data(i)); } } for (int i = 0; i < ChangeHistorySize; i++) { data.Add(new Data(i * 100)); } // Pipeline. var pipeline = ml.Transforms.Text.FeaturizeText("Text", "Text_Featurized") .Append(new SsaChangePointEstimator(ml, new SsaChangePointDetector.Arguments() { Confidence = 95, Source = "Value", Name = "Change", ChangeHistoryLength = ChangeHistorySize, TrainingWindowSize = MaxTrainingSize, SeasonalWindowSize = SeasonalitySize })); // Train. var model = pipeline.Fit(dataView); //Create prediction function. var engine = model.CreateTimeSeriesPredictionFunction <Data, Prediction1>(ml); //Checkpoint with no inputs passed at prediction. var modelPath = "temp.zip"; engine.CheckPoint(ml, modelPath); //Load time series model and we will use this to pass two inputs and compare the raw score //with "engine". ITransformer model2 = null; using (var file = File.OpenRead(modelPath)) model2 = TransformerChain.LoadFrom(ml, file); //Raw score after state gets updated with two inputs. var engine2 = model2.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml); var prediction2 = engine2.Predict(new Data(1)); //Raw score after first input. Assert.Equal(1.1661833524703979, prediction2.Change[1], precision: 5); // Raw score prediction2 = engine2.Predict(new Data(1)); //Raw score after second input. Assert.Equal(0.12216401100158691, prediction2.Change[1], precision: 5); // Raw score //Even though time series column is not requested it will // pass the observation through time series transform and update the state with the first input. var prediction = engine.Predict(new Data(1)); Assert.Equal(-1, prediction.Random); //Save the model with state updated with just one input. engine.CheckPoint(ml, modelPath + 1); ITransformer model3 = null; using (var file = File.OpenRead(modelPath + 1)) model3 = TransformerChain.LoadFrom(ml, file); //Load the model with state updated with just one input, then pass in the second input //and raw score should match the raw score obtained by passing the two input in the first model. var engine3 = model3.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml); var prediction3 = engine3.Predict(new Data(1)); Assert.Equal(0.12216401100158691, prediction2.Change[1], precision: 5); // Raw score }
private void TrainRegression(string trainDataPath, string testDataPath, string modelPath) { // 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(); // Step one: read the data as an IDataView. // First, we define the reader: specify 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) ), // The data file has header. hasHeader: true, // Default separator is tab, but we need a semicolon. separator: ';'); // Now read the file (remember though, readers are lazy, so the actual reading will happen when the data is accessed). var trainData = reader.Read(new MultiFileSource(trainDataPath)); // Step two: define the learning pipeline. // We know that this is a regression task, so we create a regression context: it will give us the algorithms // we need, as well as the evaluation procedure. var regression = new RegressionContext(env); // We 'start' the pipeline with the output of the reader. var learningPipeline = reader.MakeNewEstimator() // Now we can add any 'training steps' to it. In our case we want to 'normalize' the data (rescale to be // between -1 and 1 for all examples), and then train the model. .Append(r => ( // Retain the 'Target' column for evaluation purposes. r.Target, // We choose the SDCA regression trainer. Note that we normalize the 'FeatureVector' right here in // the the same call. Prediction: regression.Trainers.Sdca(label: r.Target, features: r.FeatureVector.Normalize()))); var fx = trainData.GetColumn(x => x.FeatureVector); // Step three. Train the pipeline. var model = learningPipeline.Fit(trainData); // Read the test dataset. var testData = reader.Read(new MultiFileSource(testDataPath)); // Calculate metrics of the model on the test data. // We are using the 'regression' context object here to perform evaluation. var metrics = regression.Evaluate(model.Transform(testData), label: r => r.Target, score: r => r.Prediction); using (var stream = File.Create(modelPath)) { // Saving and loading happens to 'dynamic' models, so the static typing is lost in the process. model.AsDynamic.SaveTo(env, stream); } // Potentially, the lines below can be in a different process altogether. // When you load the model, it's a 'dynamic' transformer. ITransformer loadedModel; using (var stream = File.OpenRead(modelPath)) loadedModel = TransformerChain.LoadFrom(env, stream); }
// This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot). // IidChangePointDetector is applied then to identify points where data distribution changed using time series // prediction engine. The engine is checkpointed and then loaded back from disk into memory and used for prediction. public static void IidChangePointDetectorPrediction() { // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, // as well as the source of randomness. var ml = new MLContext(); // Generate sample series data with a change const int Size = 16; var data = new List <IidChangePointData>(Size); for (int i = 0; i < Size / 2; i++) { data.Add(new IidChangePointData(5)); } // This is a change point for (int i = 0; i < Size / 2; i++) { data.Add(new IidChangePointData(7)); } // Convert data to IDataView. var dataView = ml.Data.ReadFromEnumerable(data); // Setup IidSpikeDetector arguments string outputColumnName = nameof(ChangePointPrediction.Prediction); string inputColumnName = nameof(IidChangePointData.Value); // Time Series model. ITransformer model = ml.Transforms.IidChangePointEstimator(outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView); // Create a time series prediction engine from the model. var engine = model.CreateTimeSeriesPredictionFunction <IidChangePointData, ChangePointPrediction>(ml); for (int index = 0; index < 8; index++) { // Anomaly change point detection. var prediction = engine.Predict(new IidChangePointData(5)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 5, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Change point var changePointPrediction = engine.Predict(new IidChangePointData(7)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, changePointPrediction.Prediction[0], changePointPrediction.Prediction[1], changePointPrediction.Prediction[2], changePointPrediction.Prediction[3]); // Checkpoint the model. var modelPath = "temp.zip"; engine.CheckPoint(ml, modelPath); // Reference to current time series engine because in the next step "engine" will point to the // checkpointed model being loaded from disk. var timeseries1 = engine; // Load the model. using (var file = File.OpenRead(modelPath)) model = TransformerChain.LoadFrom(ml, file); // Create a time series prediction engine from the checkpointed model. engine = model.CreateTimeSeriesPredictionFunction <IidChangePointData, ChangePointPrediction>(ml); for (int index = 0; index < 8; index++) { // Anomaly change point detection. var prediction = engine.Predict(new IidChangePointData(7)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Prediction from the original time series engine should match the prediction from // check pointed model. engine = timeseries1; for (int index = 0; index < 8; index++) { // Anomaly change point detection. var prediction = engine.Predict(new IidChangePointData(7)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Data Alert Score P-Value Martingale value // 5 0 5.00 0.50 0.00 <-- Time Series 1. // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 5 0 5.00 0.50 0.00 // 7 1 7.00 0.00 10298.67 <-- alert is on, predicted changepoint (and model is checkpointed). // 7 0 7.00 0.13 33950.16 <-- Time Series 2 : Model loaded back from disk and prediction is made. // 7 0 7.00 0.26 60866.34 // 7 0 7.00 0.38 78362.04 // 7 0 7.00 0.50 0.01 // 7 0 7.00 0.50 0.00 // 7 0 7.00 0.50 0.00 // 7 0 7.00 0.50 0.00 // 7 0 7.00 0.13 33950.16 <-- Time Series 1 and prediction is made. // 7 0 7.00 0.26 60866.34 // 7 0 7.00 0.38 78362.04 // 7 0 7.00 0.50 0.01 // 7 0 7.00 0.50 0.00 // 7 0 7.00 0.50 0.00 // 7 0 7.00 0.50 0.00 }
/// <summary> /// Load the model from the stream. /// </summary> /// <param name="stream">A readable, seekable stream to load from.</param> /// <returns>The loaded model.</returns> public ITransformer Load(Stream stream) => TransformerChain.LoadFrom(_env, stream);
/// <summary> /// Load the model from the stream. /// </summary> /// <param name="stream">A readable, seekable stream to load from.</param> /// <returns>The loaded model.</returns> public ITransformer Load(Stream stream) => TransformerChain.LoadFrom(Environment, stream);
// This example shows change point detection as above, but demonstrates how to train a model // that can run predictions on streaming data, and how to persist the trained model and then re-load it. public static void SsaChangePointDetectorPrediction() { // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, // as well as the source of randomness. var ml = new MLContext(); // Generate sample series data with a recurring pattern const int SeasonalitySize = 5; const int TrainingSeasons = 3; const int TrainingSize = SeasonalitySize * TrainingSeasons; var data = new List <SsaChangePointData>(); for (int i = 0; i < TrainingSeasons; i++) { for (int j = 0; j < SeasonalitySize; j++) { data.Add(new SsaChangePointData(j)); } } // Convert data to IDataView. var dataView = ml.Data.LoadFromEnumerable(data); // Setup SsaChangePointDetector arguments var inputColumnName = nameof(SsaChangePointData.Value); var outputColumnName = nameof(ChangePointPrediction.Prediction); // Train the change point detector. ITransformer model = ml.Transforms.SsaChangePointEstimator(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView); // Create a prediction engine from the model for feeding new data. var engine = model.CreateTimeSeriesPredictionFunction <SsaChangePointData, ChangePointPrediction>(ml); // Start streaming new data points with no change point to the prediction engine. Console.WriteLine($"Output from ChangePoint predictions on new data:"); Console.WriteLine("Data\tAlert\tScore\tP-Value\tMartingale value"); ChangePointPrediction prediction = null; for (int i = 0; i < 5; i++) { var value = i; prediction = engine.Predict(new SsaChangePointData(value)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Now stream data points that reflect a change in trend. for (int i = 0; i < 5; i++) { var value = (i + 1) * 100; prediction = engine.Predict(new SsaChangePointData(value)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Now we demonstrate saving and loading the model. // Save the model that exists within the prediction engine. // The engine has been updating this model with every new data point. var modelPath = "model.zip"; engine.CheckPoint(ml, modelPath); // Load the model. using (var file = File.OpenRead(modelPath)) model = TransformerChain.LoadFrom(ml, file); // We must create a new prediction engine from the persisted model. engine = model.CreateTimeSeriesPredictionFunction <SsaChangePointData, ChangePointPrediction>(ml); // Run predictions on the loaded model. for (int i = 0; i < 5; i++) { var value = (i + 1) * 100; prediction = engine.Predict(new SsaChangePointData(value)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); } // Output from ChangePoint predictions on new data: // Data Alert Score P-Value Martingale value // 0 0 - 1.01 0.50 0.00 // 1 0 - 0.24 0.22 0.00 // 2 0 - 0.31 0.30 0.00 // 3 0 0.44 0.01 0.00 // 4 0 2.16 0.00 0.24 // 100 0 86.23 0.00 2076098.24 // 200 0 171.38 0.00 809668524.21 // 300 1 256.83 0.01 22130423541.93 <-- alert is on, note that delay is expected // 400 0 326.55 0.04 241162710263.29 // 500 0 364.82 0.08 597660527041.45 <-- saved to disk // 100 0 - 58.58 0.15 1096021098844.34 <-- loaded from disk and running new predictions // 200 0 - 41.24 0.20 97579154688.98 // 300 0 - 30.61 0.24 95319753.87 // 400 0 58.87 0.38 14.24 // 500 0 219.28 0.36 0.05 }
public void ChangePointDetectionWithSeasonalityPredictionEngine() { const int ChangeHistorySize = 10; const int SeasonalitySize = 10; const int NumberOfSeasonsInTraining = 5; const int MaxTrainingSize = NumberOfSeasonsInTraining * SeasonalitySize; List <Data> data = new List <Data>(); var ml = new MLContext(seed: 1, conc: 1); var dataView = ml.CreateStreamingDataView(data); for (int j = 0; j < NumberOfSeasonsInTraining; j++) { for (int i = 0; i < SeasonalitySize; i++) { data.Add(new Data(i)); } } for (int i = 0; i < ChangeHistorySize; i++) { data.Add(new Data(i * 100)); } // Pipeline. var pipeline = ml.Transforms.Text.FeaturizeText("Text", "Text_Featurized") .Append(new SsaChangePointEstimator(ml, new SsaChangePointDetector.Arguments() { Confidence = 95, Source = "Value", Name = "Change", ChangeHistoryLength = ChangeHistorySize, TrainingWindowSize = MaxTrainingSize, SeasonalWindowSize = SeasonalitySize })); // Train. var model = pipeline.Fit(dataView); //Model 1: Prediction #1. var engine = model.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml); var prediction = engine.Predict(new Data(1)); Assert.Equal(0, prediction.Change[0], precision: 7); // Alert Assert.Equal(1.1661833524703979, prediction.Change[1], precision: 5); // Raw score Assert.Equal(0.5, prediction.Change[2], precision: 7); // P-Value score Assert.Equal(5.1200000000000114E-08, prediction.Change[3], precision: 7); // Martingale score //Model 1: Checkpoint. var modelPath = "temp.zip"; engine.CheckPoint(ml, modelPath); //Model 1: Prediction #2 prediction = engine.Predict(new Data(1)); Assert.Equal(0, prediction.Change[0], precision: 7); // Alert Assert.Equal(0.12216401100158691, prediction.Change[1], precision: 5); // Raw score Assert.Equal(0.14823824685192111, prediction.Change[2], precision: 5); // P-Value score Assert.Equal(1.5292508189989167E-07, prediction.Change[3], precision: 7); // Martingale score // Load Model 1. ITransformer model2 = null; using (var file = File.OpenRead(modelPath)) model2 = TransformerChain.LoadFrom(ml, file); //Predict and expect the same result after checkpointing(Prediction #2). engine = model2.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml); prediction = engine.Predict(new Data(1)); Assert.Equal(0, prediction.Change[0], precision: 7); // Alert Assert.Equal(0.12216401100158691, prediction.Change[1], precision: 5); // Raw score Assert.Equal(0.14823824685192111, prediction.Change[2], precision: 5); // P-Value score Assert.Equal(1.5292508189989167E-07, prediction.Change[3], precision: 5); // Martingale score }
// This example shows spike detection as above, but demonstrates how to train a model // that can run predictions on streaming data, and how to persist the trained model and then re-load it. public static void SsaSpikeDetectorPrediction() { // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, // as well as the source of randomness. var ml = new MLContext(); // Generate sample series data with a recurring pattern const int SeasonalitySize = 5; const int TrainingSeasons = 3; const int TrainingSize = SeasonalitySize * TrainingSeasons; var data = new List <SsaSpikeData>(); for (int i = 0; i < TrainingSeasons; i++) { for (int j = 0; j < SeasonalitySize; j++) { data.Add(new SsaSpikeData(j)); } } // Convert data to IDataView. var dataView = ml.Data.ReadFromEnumerable(data); // Setup IidSpikeDetector arguments var inputColumnName = nameof(SsaSpikeData.Value); var outputColumnName = nameof(SsaSpikePrediction.Prediction); var args = new SsaSpikeDetector.Arguments() { Source = inputColumnName, Name = outputColumnName, Confidence = 95, // The confidence for spike detection in the range [0, 100] PvalueHistoryLength = 8, // The size of the sliding window for computing the p-value; shorter windows are more sensitive to spikes. TrainingWindowSize = TrainingSize, // The number of points from the beginning of the sequence used for training. SeasonalWindowSize = SeasonalitySize + 1 // An upper bound on the largest relevant seasonality in the input time series." }; // Train the change point detector. ITransformer model = new SsaSpikeEstimator(ml, args).Fit(dataView); // Create a prediction engine from the model for feeding new data. var engine = model.CreateTimeSeriesPredictionFunction <SsaSpikeData, SsaSpikePrediction>(ml); // Start streaming new data points with no change point to the prediction engine. Console.WriteLine($"Output from spike predictions on new data:"); Console.WriteLine("Data\tAlert\tScore\tP-Value"); SsaSpikePrediction prediction = null; for (int j = 0; j < 2; j++) { for (int i = 0; i < 5; i++) { var value = i; prediction = engine.Predict(new SsaSpikeData(value)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2]); } } // Now send a data point that reflects a spike. var newValue = 100; prediction = engine.Predict(new SsaSpikeData(newValue)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", newValue, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2]); // Now we demonstrate saving and loading the model. // Save the model that exists within the prediction engine. // The engine has been updating this model with every new data point. var modelPath = "model.zip"; engine.CheckPoint(ml, modelPath); // Load the model. using (var file = File.OpenRead(modelPath)) model = TransformerChain.LoadFrom(ml, file); // We must create a new prediction engine from the persisted model. engine = model.CreateTimeSeriesPredictionFunction <SsaSpikeData, SsaSpikePrediction>(ml); // Run predictions on the loaded model. for (int i = 0; i < 5; i++) { var value = i; prediction = engine.Predict(new SsaSpikeData(value)); Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2]); } // Output from spike predictions on new data: // Data Alert Score P-Value // 0 0 - 1.01 0.50 // 1 0 - 0.24 0.22 // 2 0 - 0.31 0.30 // 3 0 0.44 0.01 // 4 0 2.16 0.00 // 0 0 - 0.78 0.27 // 1 0 - 0.80 0.30 // 2 0 - 0.84 0.31 // 3 0 0.33 0.31 // 4 0 2.21 0.07 // 100 1 86.17 0.00 <-- alert is on, predicted spike // 0 0 - 2.74 0.40 <-- saved to disk, re-loaded, and running new predictions // 1 0 - 1.47 0.42 // 2 0 - 17.50 0.24 // 3 0 - 30.82 0.16 // 4 0 - 23.24 0.28 }