Closed
Description
Hi, I get an exception on prediction with AutoML.
Before you run the Problem you need to reference two NuGet Packages Microsoft.ML and Microsoft.ML.AutoML
Here ist the complete code to reproduce the error. Run in VS2019:
using Microsoft.ML;
using Microsoft.ML.AutoML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers.FastTree;
using System;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.ML.DataOperationsCatalog;
namespace AutoML
{
class Program
{
static void Main(string[] args)
{
var mlContext = new MLContext(seed: 0);
var examples = GenerateData(100);
var dataview = mlContext.Data.LoadFromEnumerable(examples);
TrainTestData trainTestSplit = mlContext.Data.TrainTestSplit(dataview, testFraction: 0.1, samplingKeyColumnName: null);
IDataView trainingData = trainTestSplit.TrainSet;
IDataView testData = trainTestSplit.TestSet;
ITransformer model = TrainRegresionAutoML(trainingData);
ReportOnFeatureImportance(mlContext, model, dataview);
OutputData prediction = PredictRegresinAutoML<InputData,OutputData>(model, new InputData(){A = 6, B = 6});
}
static ITransformer TrainRegresionAutoML(IDataView trainData)
{
var mlContext = new MLContext(seed: 0);
var settings = new RegressionExperimentSettings
{
MaxExperimentTimeInSeconds = 10, // In Second
OptimizingMetric = RegressionMetric.RSquared,
CacheDirectory = null
};
var experiment = mlContext.Auto().CreateRegressionExperiment(settings);
var model = experiment.Execute(trainData);
return model.BestRun.Model;
}
private static void ReportOnFeatureImportance(MLContext context, ITransformer model, IDataView data)
{
// Need to cast from the ITransformer interface to gain access to the LastTransformer property.
var typedModel = (TransformerChain<IPredictionTransformer<object>>)model;
var modelParams = typedModel.LastTransformer.Model as FastTreeRegressionModelParameters;
var weights = new VBuffer<float>();
modelParams.GetFeatureWeights(ref weights);
}
static TDst PredictRegresinAutoML<TSrc, TDst>(ITransformer model, TSrc inputData)
where TSrc : class
where TDst : class, new()
{
var mlContext = new MLContext(seed: 0);
var predictor = mlContext.Model.CreatePredictionEngine<TSrc, TDst>(model);
return predictor.Predict(inputData);
}
private static IEnumerable<InputData> GenerateData(int count,
int seed = 0)
{
for (int i = 0; i < count; i++)
{
for (int ii = 0; ii < count; ii++)
{
yield return new InputData
{
A = i,
B = ii,
Value = i * ii
};
}
}
}
}
public class InputData
{
public float A { get; set; }
public float B { get; set; }
[ColumnName("Label")]
public float Value { get; set; }
}
public class OutputData
{
[ColumnName("Score")]
public float Result;
}
public class FeatureImportance
{
public string Name { get; set; }
public double RSquaredMean { get; set; }
public double CorrelationCoefficient { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment