Repository navigation
model.LastTransformer doesn't exist #4937
Description
Activity
@philiplai can you share the code where you load the data, define the pipeline, and fit the model?
LastTransformeris a property ofTransformerChain. My best guess without looking at the code is that your model has only one transformer, and is not aTransformerChain. If that is the case, your model will be of typeBinaryPredictionTransformer, which you can directly pass to PFI instead of thelinearPredictorin the example.- addedquestionFurther information is requestedFurther information is requested
on Mar 13, 2020 A duplicate of this issue #3976
The problem is that model.Load by default returns incorrect type that doesn't have property LastTransformer, so you have to cast it to the right data type explicitly.For the record, I wouldn't think that issue #3976 is a duplicate of this issue here. @artemiusgreat is right in that
mlContext.Model.Loadalways return anITransformer(link to docs). SinceITransformerdoesn't have a.LastTransformerproperty, then it needs to be casted to aTransformerChainas shown in the following sample:Lines 36 to 41 in 290da82
var model = mlContext.Model.Load(modelPath, out var schema); // Transform the dataset. var transformedData = model.Transform(data); var linearPredictor = (model as TransformerChain<ITransformer>).LastTransformer as BinaryPredictionTransformer<CalibratedModelParametersBase<LinearBinaryModelParameters, PlattCalibrator>>; The need for a cast to access the
.LastTransformeris by design, and it isn't a bug.On the other hand, issue #3976 was actually that attempting to do the cast above caused an exception, and so PRs #4262 and #4306 fixed this bug. The only reason #3976 is still open is because the issue specifically refers to using ML.NET on F#, and the fix made on those PRs haven't been tested on F#, only on C#.
Notice that an alternative cast that would also work would be:
var linearPredictor = (loadedmodel as TransformerChain<ITransformer>).LastTransformer as ISingleFeaturePredictionTransformer<object>;
As for the issue in here, I wouldn't know if the user who created the issue is trying to load a model (in which case, making the cast @artemiusgreat suggested might be enough), or if the user has a model with one transformer (as @najeeb-kazmi has suggested, in which case passing that predictor directly to PFI should be enough).
Closing this issue. Please feel free to reopen if something remains unanswered.
- ghost locked as resolved and limited conversation to collaborators
on Mar 18, 2022
I was trying to implement Permutation Feature Importance (PFI) for Binary Classification. But I was stuck on the following line of code. This method simply doesn't exist.
// Extract the predictor.
var linearPredictor = model.LastTransformer;
I was following the example on https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.permutationfeatureimportanceextensions.permutationfeatureimportance?view=ml-dotnet
Any idea?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.