-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add SrCnn Anomaly Detector #3693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
74da250
Add SrCnn Anomaly Detector
mengaims 4126cb9
Merge remote-tracking branch 'upstream/master' into timeseries/sr
mengaims fa6520a
Add core calculation code to SrCnn
mengaims c488693
Fix implementation bugs
mengaims 5f07695
Merge remote-tracking branch 'upstream/master' into timeseries/sr
mengaims 406c664
Add test and sample.
mengaims cc0b479
Merge remote-tracking branch 'upstream/master' into timeseries/sr
mengaims c56d4d4
Fix commented problems:
mengaims be277ba
Fix a predict bug
mengaims 8115e87
Merge remote-tracking branch 'upstream/master' into timeseries/sr
mengaims cc5a8ee
Merge remote-tracking branch 'upstream/master' into timeseries/sr
mengaims 7e41da8
1. Fix build fail problem; 2. Improve samples; 3. Minor change to docs
mengaims dcb271b
Add document.
mengaims a4f8b24
Add equations to doc
mengaims File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectAnomalyBySrCnn.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.ML; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Transforms.TimeSeries; | ||
|
|
||
| namespace Samples.Dynamic | ||
| { | ||
| public static class DetectAnomalyBySrCnn | ||
| { | ||
| // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot). | ||
| // The estimator is applied then to identify spiking points in the series. | ||
| public static void Example() | ||
| { | ||
| // 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 an anomaly | ||
| var data = new List<TimeSeriesData>(); | ||
| for (int index = 0; index < 20; index++) | ||
| { | ||
| data.Add(new TimeSeriesData(5)); | ||
| } | ||
| data.Add(new TimeSeriesData(10)); | ||
| for (int index = 0; index < 5; index++) | ||
| { | ||
| data.Add(new TimeSeriesData(5)); | ||
| } | ||
|
|
||
| // Convert data to IDataView. | ||
| var dataView = ml.Data.LoadFromEnumerable(data); | ||
|
|
||
| // Setup the estimator arguments | ||
| string outputColumnName = nameof(SrCnnAnomalyDetection.Prediction); | ||
| string inputColumnName = nameof(TimeSeriesData.Value); | ||
|
|
||
| // The transformed model. | ||
| ITransformer model = ml.Transforms.DetectAnomalyBySrCnn(outputColumnName, inputColumnName, 16, 5, 5, 3, 8, 0.35).Fit(dataView); | ||
|
|
||
| // Create a time series prediction engine from the model. | ||
| var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, SrCnnAnomalyDetection>(ml); | ||
|
|
||
| Console.WriteLine($"{outputColumnName} column obtained post-transformation."); | ||
| Console.WriteLine("Data\tAlert\tScore\tMag"); | ||
|
|
||
| // Prediction column obtained post-transformation. | ||
| // Data Alert Score Mag | ||
|
|
||
| // Create non-anomalous data and check for anomaly. | ||
| for (int index = 0; index < 20; index++) | ||
| { | ||
| // Anomaly detection. | ||
| PrintPrediction(5, engine.Predict(new TimeSeriesData(5))); | ||
| } | ||
|
|
||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
|
|
||
| // Anomaly. | ||
| PrintPrediction(10, engine.Predict(new TimeSeriesData(10))); | ||
|
|
||
| //10 1 0.47 0.93 <-- alert is on, predicted anomaly | ||
|
|
||
| // Checkpoint the model. | ||
| var modelPath = "temp.zip"; | ||
| engine.CheckPoint(ml, modelPath); | ||
|
|
||
| // Load the model. | ||
| using (var file = File.OpenRead(modelPath)) | ||
| model = ml.Model.Load(file, out DataViewSchema schema); | ||
|
|
||
| for (int index = 0; index < 5; index++) | ||
| { | ||
| // Anomaly detection. | ||
| PrintPrediction(5, engine.Predict(new TimeSeriesData(5))); | ||
| } | ||
|
|
||
| //5 0 0.31 0.50 | ||
| //5 0 0.05 0.30 | ||
| //5 0 0.01 0.23 | ||
| //5 0 0.00 0.21 | ||
| //5 0 0.01 0.25 | ||
| } | ||
|
|
||
| private static void PrintPrediction(float value, SrCnnAnomalyDetection prediction) => | ||
| Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", value, prediction.Prediction[0], | ||
| prediction.Prediction[1], prediction.Prediction[2]); | ||
|
|
||
| private class TimeSeriesData | ||
| { | ||
| public float Value; | ||
|
|
||
| public TimeSeriesData(float value) | ||
| { | ||
| Value = value; | ||
| } | ||
| } | ||
|
|
||
| private class SrCnnAnomalyDetection | ||
| { | ||
| [VectorType(3)] | ||
| public double[] Prediction { get; set; } | ||
| } | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
...Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectAnomalyBySrCnnBatchPrediction.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.ML; | ||
| using Microsoft.ML.Data; | ||
|
|
||
| namespace Samples.Dynamic | ||
| { | ||
| public static class DetectAnomalyBySrCnnBatchPrediction | ||
| { | ||
| public static void Example() | ||
| { | ||
| // 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 an anomaly | ||
| var data = new List<TimeSeriesData>(); | ||
| for (int index = 0; index < 20; index++) | ||
| { | ||
| data.Add(new TimeSeriesData(5)); | ||
| } | ||
| data.Add(new TimeSeriesData(10)); | ||
| for (int index = 0; index < 5; index++) | ||
| { | ||
| data.Add(new TimeSeriesData(5)); | ||
| } | ||
|
|
||
| // Convert data to IDataView. | ||
| var dataView = ml.Data.LoadFromEnumerable(data); | ||
|
|
||
| // Setup the estimator arguments | ||
| string outputColumnName = nameof(SrCnnAnomalyDetection.Prediction); | ||
| string inputColumnName = nameof(TimeSeriesData.Value); | ||
|
|
||
| // The transformed data. | ||
| var transformedData = ml.Transforms.DetectAnomalyBySrCnn(outputColumnName, inputColumnName, 16, 5, 5, 3, 8, 0.35).Fit(dataView).Transform(dataView); | ||
|
|
||
| // Getting the data of the newly created column as an IEnumerable of SrCnnAnomalyDetection. | ||
| var predictionColumn = ml.Data.CreateEnumerable<SrCnnAnomalyDetection>(transformedData, reuseRowObject: false); | ||
|
|
||
| Console.WriteLine($"{outputColumnName} column obtained post-transformation."); | ||
| Console.WriteLine("Data\tAlert\tScore\tMag"); | ||
|
|
||
| int k = 0; | ||
| foreach (var prediction in predictionColumn) | ||
| PrintPrediction(data[k++].Value, prediction); | ||
|
|
||
| //Prediction column obtained post-transformation. | ||
| //Data Alert Score Mag | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.00 0.00 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //5 0 0.03 0.18 | ||
| //10 1 0.47 0.93 | ||
| //5 0 0.31 0.50 | ||
| //5 0 0.05 0.30 | ||
| //5 0 0.01 0.23 | ||
| //5 0 0.00 0.21 | ||
| //5 0 0.01 0.25 | ||
| } | ||
|
|
||
| private static void PrintPrediction(float value, SrCnnAnomalyDetection prediction) => | ||
| Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", value, prediction.Prediction[0], | ||
| prediction.Prediction[1], prediction.Prediction[2]); | ||
|
|
||
| private class TimeSeriesData | ||
| { | ||
| public float Value; | ||
|
|
||
| public TimeSeriesData(float value) | ||
| { | ||
| Value = value; | ||
| } | ||
| } | ||
|
|
||
| private class SrCnnAnomalyDetection | ||
| { | ||
| [VectorType(3)] | ||
| public double[] Prediction { get; set; } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.