Using an emitted class with CreatePredictionEngine and Predict

sebescudie 1 Reputation point
2021-01-21T06:45:31.863+00:00

hey all,

For an application I'm writing, I need to use CreatePredictionEngine with two emitted classes for TSrc and TDst. Those are emitted via System.Reflection.Emit. I'm creating the prediction engine as follows :

   dynamic dynamicPredictionEngine;  
   var genericPredictionMethod = description.mlContext.Model.GetType().GetMethod("CreatePredictionEngine", new[] { typeof(ITransformer), typeof(DataViewSchema) });  
   var predictionMethod = genericPredictionMethod.MakeGenericMethod(inputObject.GetType(), outputObject.GetType());  
   dynamicPredictionEngine = predictionMethod.Invoke(mlContext.Model, new object[] { TrainedModel, PredictionPipeline });  

inputObject and outputObject being instances of my two emitted classes.

When running such a prediction engine (with var result = dynamicPredictionEngine.Predict(inputObject);), I get this Exception :

   Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The best overloaded method match for 'Microsoft.ML.PredictionEngineBase<inputType>.Predict(inputObject)' has some invalid arguments'  

To debug this, I created the inputType manually, instead of using my emitted class. This works fine if input type is created manually, and output type is emitted. I checked in the debugger to see what differs between my emitted class and the manually created one, they seem to match :

58984-image.png

inputObject is emitted, TaxiTrip is manually created.

I'm totally new to this Reflection.Emit thing, and I have no idea what could cause the Predict function to fail, since both classes seem to have the same properties here.

Does anyone have an idea?

Thanks in advance!

.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
150 questions
0 comments No comments
{count} votes