public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, [Blob("models/model.zip", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream serializedModel, TraceWriter log) { // Workaround for Azure Functions Host if (typeof(Microsoft.ML.Runtime.Data.LoadTransform) == null || typeof(Microsoft.ML.Runtime.Learners.LinearClassificationTrainer) == null || typeof(Microsoft.ML.Runtime.Internal.CpuMath.SseUtils) == null || typeof(Microsoft.ML.Runtime.FastTree.FastTree) == null) { log.Error("Error loading ML.NET"); return(new StatusCodeResult(500)); } //Read incoming request body string requestBody = new StreamReader(req.Body).ReadToEnd(); log.Info(requestBody); //Bind request body to IrisData object IrisData data = JsonConvert.DeserializeObject <IrisData>(requestBody); //Load prediction model var model = PredictionModel.ReadAsync <IrisData, IrisPrediction>(serializedModel).Result; //Make prediction IrisPrediction prediction = model.Predict(data); //Return prediction return((IActionResult) new OkObjectResult(prediction.PredictedLabels)); }
public void FirstExperienceWithML() { // Load the data into the system. string dataPath = "iris-data.txt"; var data = TextReader.FitAndRead(env, dataPath, row => ( Label: row.ReadString(0), SepalWidth: row.ReadFloat(1), SepalLength: row.ReadFloat(2), PetalWidth: row.ReadFloat(3), PetalLength: row.ReadFloat(4))); var preprocess = data.Schema.MakeEstimator(row => ( // Convert string label to key. Label: row.Label.DictionarizeLabel(), // Concatenate all features into a vector. Features: row.SepalWidth.ConcatWith(row.SepalLength, row.PetalWidth, row.PetalLength))); var pipeline = preprocess // Append the trainer to the training pipeline. .AppendEstimator(row => row.Label.PredictWithSdca(row.Features)) .AppendEstimator(row => row.PredictedLabel.KeyToValue()); // Train the model and make some predictions. var model = pipeline.Fit <IrisExample, IrisPrediction>(data); IrisPrediction prediction = model.Predict(new IrisExample { SepalWidth = 3.3f, SepalLength = 1.6f, PetalWidth = 0.2f, PetalLength = 5.1f }); }
public async Task <string> Post([FromBody] IrisData observation) { IrisPrediction prediction = await _predictActorPool .Ask <IrisPrediction>(new Predict <IrisData> { Observation = observation }); return(prediction.PredictedLabel); }
public void CanTrain() { var pipeline = new Legacy.LearningPipeline(); var data = new List <IrisData>() { new IrisData { SepalLength = 1f, SepalWidth = 1f, PetalLength = 0.3f, PetalWidth = 5.1f, Label = 1 }, new IrisData { SepalLength = 1f, SepalWidth = 1f, PetalLength = 0.3f, PetalWidth = 5.1f, Label = 1 }, new IrisData { SepalLength = 1.2f, SepalWidth = 0.5f, PetalLength = 0.3f, PetalWidth = 5.1f, Label = 0 } }; var collection = CollectionDataSource.Create(data); pipeline.Add(collection); pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); var model = pipeline.Train <IrisData, IrisPrediction>(); IrisPrediction prediction = model.Predict(new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); pipeline = new Legacy.LearningPipeline(); collection = CollectionDataSource.Create(data.AsEnumerable()); pipeline.Add(collection); pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); model = pipeline.Train <IrisData, IrisPrediction>(); prediction = model.Predict(new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); }
private void ComparePredictions(PredictionEngine <IrisData, IrisPrediction> model) { IrisPrediction prediction = model.Predict(new IrisData() { SepalLength = 5.1f, SepalWidth = 3.3f, PetalLength = 1.6f, PetalWidth = 0.2f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 6.4f, SepalWidth = 3.1f, PetalLength = 5.5f, PetalWidth = 2.2f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 4.4f, SepalWidth = 3.1f, PetalLength = 2.5f, PetalWidth = 1.2f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); }
public void TrainAndPredictIrisModelTest() { var mlContext = new MLContext(seed: 1); var reader = mlContext.Data.CreateTextLoader(columns: new[] { new TextLoader.Column("Label", DataKind.Single, 0), new TextLoader.Column("SepalLength", DataKind.Single, 1), new TextLoader.Column("SepalWidth", DataKind.Single, 2), new TextLoader.Column("PetalLength", DataKind.Single, 3), new TextLoader.Column("PetalWidth", DataKind.Single, 4) } ); var pipe = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(mlContext.Transforms.Normalize("Features")) .Append(mlContext.Transforms.Conversion.MapValueToKey("Label")) .AppendCacheCheckpoint(mlContext) .Append(mlContext.MulticlassClassification.Trainers.SdcaCalibrated( new SdcaCalibratedMulticlassTrainer.Options { NumberOfThreads = 1 })); // Read training and test data sets string dataPath = GetDataPath(TestDatasets.iris.trainFilename); string testDataPath = dataPath; var trainData = reader.Load(dataPath); var testData = reader.Load(testDataPath); // Train the pipeline var trainedModel = pipe.Fit(trainData); // Make predictions var predictFunction = mlContext.Model.CreatePredictionEngine <IrisData, IrisPrediction>(trainedModel); IrisPrediction prediction = predictFunction.Predict(new IrisData() { SepalLength = 5.1f, SepalWidth = 3.3f, PetalLength = 1.6f, PetalWidth = 0.2f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = predictFunction.Predict(new IrisData() { SepalLength = 6.4f, SepalWidth = 3.1f, PetalLength = 5.5f, PetalWidth = 2.2f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = predictFunction.Predict(new IrisData() { SepalLength = 4.4f, SepalWidth = 3.1f, PetalLength = 2.5f, PetalWidth = 1.2f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Evaluate the trained pipeline var predicted = trainedModel.Transform(testData); var metrics = mlContext.MulticlassClassification.Evaluate(predicted, topKPredictionCount: 3); Assert.Equal(.98, metrics.MacroAccuracy); Assert.Equal(.98, metrics.MicroAccuracy, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Count); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); }
public void TrainAndPredictIrisModelTest() { string dataPath = GetDataPath("iris.txt"); var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader <IrisData>(dataPath, useHeader: false, separator: "tab")); pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); PredictionModel <IrisData, IrisPrediction> model = pipeline.Train <IrisData, IrisPrediction>(); IrisPrediction prediction = model.Predict(new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 3.1f, SepalWidth = 5.5f, PetalLength = 2.2f, PetalWidth = 6.4f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 3.1f, SepalWidth = 2.5f, PetalLength = 1.2f, PetalWidth = 4.4f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Note: Testing against the same data set as a simple way to test evaluation. // This isn't appropriate in real-world scenarios. string testDataPath = GetDataPath("iris.txt"); var testData = new TextLoader <IrisData>(testDataPath, useHeader: false, separator: "tab"); var evaluator = new ClassificationEvaluator(); evaluator.OutputTopKAcc = 3; ClassificationMetrics metrics = evaluator.Evaluate(model, testData); Assert.Equal(.98, metrics.AccuracyMacro); Assert.Equal(.98, metrics.AccuracyMicro, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.InRange(metrics.LogLossReduction, 94, 96); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Length); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); ConfusionMatrix matrix = metrics.ConfusionMatrix; Assert.Equal(3, matrix.Order); Assert.Equal(3, matrix.ClassNames.Count); Assert.Equal("0", matrix.ClassNames[0]); Assert.Equal("1", matrix.ClassNames[1]); Assert.Equal("2", matrix.ClassNames[2]); Assert.Equal(50, matrix[0, 0]); Assert.Equal(50, matrix["0", "0"]); Assert.Equal(0, matrix[0, 1]); Assert.Equal(0, matrix["0", "1"]); Assert.Equal(0, matrix[0, 2]); Assert.Equal(0, matrix["0", "2"]); Assert.Equal(0, matrix[1, 0]); Assert.Equal(0, matrix["1", "0"]); Assert.Equal(48, matrix[1, 1]); Assert.Equal(48, matrix["1", "1"]); Assert.Equal(2, matrix[1, 2]); Assert.Equal(2, matrix["1", "2"]); Assert.Equal(0, matrix[2, 0]); Assert.Equal(0, matrix["2", "0"]); Assert.Equal(1, matrix[2, 1]); Assert.Equal(1, matrix["2", "1"]); Assert.Equal(49, matrix[2, 2]); Assert.Equal(49, matrix["2", "2"]); }
public void TrainAndPredictIrisModelWithStringLabelTest() { string dataPath = GetDataPath("iris.data"); var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader(dataPath).CreateFrom <IrisDataWithStringLabel>(useHeader: false, separator: ',')); pipeline.Add(new Dictionarizer("Label")); // "IrisPlantType" is used as "Label" because of column attribute name on the field. pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); PredictionModel <IrisDataWithStringLabel, IrisPrediction> model = pipeline.Train <IrisDataWithStringLabel, IrisPrediction>(); IrisPrediction prediction = model.Predict(new IrisDataWithStringLabel() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisDataWithStringLabel() { SepalLength = 3.1f, SepalWidth = 5.5f, PetalLength = 2.2f, PetalWidth = 6.4f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisDataWithStringLabel() { SepalLength = 3.1f, SepalWidth = 2.5f, PetalLength = 1.2f, PetalWidth = 4.4f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Note: Testing against the same data set as a simple way to test evaluation. // This isn't appropriate in real-world scenarios. string testDataPath = GetDataPath("iris.data"); var testData = new TextLoader(testDataPath).CreateFrom <IrisDataWithStringLabel>(useHeader: false, separator: ','); var evaluator = new ClassificationEvaluator(); evaluator.OutputTopKAcc = 3; ClassificationMetrics metrics = evaluator.Evaluate(model, testData); Assert.Equal(.98, metrics.AccuracyMacro); Assert.Equal(.98, metrics.AccuracyMicro, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.InRange(metrics.LogLossReduction, 94, 96); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Length); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); ConfusionMatrix matrix = metrics.ConfusionMatrix; Assert.Equal(3, matrix.Order); Assert.Equal(3, matrix.ClassNames.Count); Assert.Equal("Iris-setosa", matrix.ClassNames[0]); Assert.Equal("Iris-versicolor", matrix.ClassNames[1]); Assert.Equal("Iris-virginica", matrix.ClassNames[2]); Assert.Equal(50, matrix[0, 0]); Assert.Equal(50, matrix["Iris-setosa", "Iris-setosa"]); Assert.Equal(0, matrix[0, 1]); Assert.Equal(0, matrix["Iris-setosa", "Iris-versicolor"]); Assert.Equal(0, matrix[0, 2]); Assert.Equal(0, matrix["Iris-setosa", "Iris-virginica"]); Assert.Equal(0, matrix[1, 0]); Assert.Equal(0, matrix["Iris-versicolor", "Iris-setosa"]); Assert.Equal(48, matrix[1, 1]); Assert.Equal(48, matrix["Iris-versicolor", "Iris-versicolor"]); Assert.Equal(2, matrix[1, 2]); Assert.Equal(2, matrix["Iris-versicolor", "Iris-virginica"]); Assert.Equal(0, matrix[2, 0]); Assert.Equal(0, matrix["Iris-virginica", "Iris-setosa"]); Assert.Equal(1, matrix[2, 1]); Assert.Equal(1, matrix["Iris-virginica", "Iris-versicolor"]); Assert.Equal(49, matrix[2, 2]); Assert.Equal(49, matrix["Iris-virginica", "Iris-virginica"]); }
public void TrainAndPredictIrisModelTest() { var mlContext = new MLContext(seed: 1, conc: 1); var reader = mlContext.Data.CreateTextReader(columns: new[] { new TextLoader.Column("Label", DataKind.R4, 0), new TextLoader.Column("SepalLength", DataKind.R4, 1), new TextLoader.Column("SepalWidth", DataKind.R4, 2), new TextLoader.Column("PetalLength", DataKind.R4, 3), new TextLoader.Column("PetalWidth", DataKind.R4, 4) } ); var pipe = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(mlContext.Transforms.Normalize("Features")) .AppendCacheCheckpoint(mlContext) .Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent("Label", "Features", advancedSettings: s => s.NumThreads = 1)); // Read training and test data sets string dataPath = GetDataPath(TestDatasets.iris.trainFilename); string testDataPath = dataPath; var trainData = reader.Read(dataPath); var testData = reader.Read(testDataPath); // Train the pipeline var trainedModel = pipe.Fit(trainData); // Make predictions var predictFunction = trainedModel.CreatePredictionEngine <IrisData, IrisPrediction>(mlContext); IrisPrediction prediction = predictFunction.Predict(new IrisData() { SepalLength = 5.1f, SepalWidth = 3.3f, PetalLength = 1.6f, PetalWidth = 0.2f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = predictFunction.Predict(new IrisData() { SepalLength = 6.4f, SepalWidth = 3.1f, PetalLength = 5.5f, PetalWidth = 2.2f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = predictFunction.Predict(new IrisData() { SepalLength = 4.4f, SepalWidth = 3.1f, PetalLength = 2.5f, PetalWidth = 1.2f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Evaluate the trained pipeline var predicted = trainedModel.Transform(testData); var metrics = mlContext.MulticlassClassification.Evaluate(predicted, topK: 3); Assert.Equal(.98, metrics.AccuracyMacro); Assert.Equal(.98, metrics.AccuracyMicro, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Length); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); }
private void HandleMessage(IrisData observation) { IrisPrediction prediction = _predictionEngine.Predict(observation); Sender.Tell(prediction, Self); }
public static string Predict(PredictionEngine <IrisData, IrisPrediction> predictionEngine, IrisData input) { IrisPrediction prediction = predictionEngine.Predict(input); return(prediction.FlowerType); }