For a project I'm working on, I need to use ML.NET's CreatePredictionEngine method with emitted types for TSrc
and TDst
. I'm emitting those with System.Reflection.Emit
.
Here's how I'm creating my dynamic prediction engine :
dynamic dynamicPredictionEngine;
var genericPredictionMethod = 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 });
Here, inputObject
and outputObject
are instances of my emitted classes.
Then, I'm running the prediction engine like so :
var result = dynamicPredictionEngine.Predict(inputObject)
But I get the following Exception :
'The best overloaded method match for 'Microsoft.ML.PredictionEngineBase<Object0f33ea95-c496-4c57-bd2c-728bb65dd0a9,Object5623e4bd-ba82-42cf-876f-5e11a5cb8bb2>.Predict(Object0f33ea95-c496-4c57-bd2c-728bb65dd0a9)' has some invalid arguments'
The GUID you see here are the names of my emitted classes.
To debug this, I tried to manually create a class that has the same properties as the emitted one, and ran the prediction engine again, with the manually created class as TSrc
and kept the emitted one as TDst
. This time, it worked fine.
I looked at the debugger to compare the emitted input class with the manually created one, and they seem to match (TaxiTrip
is the name of the class I created) :
Debugger screenshot
For reference, here's how my TaxiTrip
class is defined
namespace VL.ML
{
class TaxiTrip
{
public string VendorId { get; set; }
public string RateCode { get; set; }
public float PassengerCount { get; set; }
public float TripTime { get; set; }
public float TripDistance { get; set; }
public string PaymentType { get; set; }
public float FareAmount { get; set; }
}
}
Does anyone have an idea about what's going on here?
I already stumbled upon this question on SO, but my issue looks different : as I said if I create a class manually without any attributes (see above), Predict
works just fine. Plus, it seems to be able to make use of my emitted type for TDst
, so why would TSrc
fail?
Happy to provide more details if necessary.
Thanks in advance!
question from:
https://stackoverflow.com/questions/65829086/using-ml-nets-createpredictionengine-with-emitted-types