Skip to content

model.LastTransformer doesn't exist #4937

Description

@philiplai

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.

Activity

  1. najeeb-kazmi commented on Mar 13, 2020

    @najeeb-kazmi
    Member

    @philiplai can you share the code where you load the data, define the pipeline, and fit the model?

    LastTransformer is a property of TransformerChain. My best guess without looking at the code is that your model has only one transformer, and is not a TransformerChain. If that is the case, your model will be of type BinaryPredictionTransformer, which you can directly pass to PFI instead of the linearPredictor in the example.

  2. artemiusgreat commented on Mar 13, 2020

    @artemiusgreat

    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.

  3. antoniovs1029 commented on Mar 13, 2020

    @antoniovs1029
    Contributor

    For the record, I wouldn't think that issue #3976 is a duplicate of this issue here. @artemiusgreat is right in that mlContext.Model.Load always return an ITransformer (link to docs). Since ITransformer doesn't have a .LastTransformer property, then it needs to be casted to a TransformerChain as shown in the following sample:

    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 .LastTransformer is 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).

  4. najeeb-kazmi commented on Mar 18, 2020

    @najeeb-kazmi
    Member

    Closing this issue. Please feel free to reopen if something remains unanswered.

  5. ghost locked as resolved and limited conversation to collaborators on Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions