NorskRegnesentral / shapr

Explaining the output of machine learning models with more accurately estimated Shapley values
https://norskregnesentral.github.io/shapr/
Other
147 stars 34 forks source link

SHAPR for custom defined models #420

Open mehwish2021 opened 4 days ago

mehwish2021 commented 4 days ago

Hello All, I am using DeepCC model in which I want to apply SHAPR to get the shaply values . I defined the functions as described in here https://cran.r-project.org/web/packages/shapr/vignettes/understanding_shapr.html , but its still giving me the error that SHAPR is not defined for this model.

I have defined these functions get_model_specs() and predict_model() as mentioned in the vignette but I am confused how and where are those functions called? Please help as it has taken much time and I am unable to resolve this issue

martinju commented 4 days ago

Hi! Please use the github version of the shapr package.

mehwish2021 commented 4 days ago

Thanks for your prompt response Martinju.
A little more clarification, do you mean I have to add these functions in the code of shapR package?? Can you please guide in which file I need to add these functions ? I would really appreciate you response

martinju commented 4 days ago

No, the package on CRAN is outdated (and will be replaced by the main at GitHub) soon. It does things a bit differently. I suggest you install the github version of shapr instead, modify your code accordingly, try to use that to explain your custom model instead. The procedure is explained her: https://norskregnesentral.github.io/shapr/articles/understanding_shapr.html#explain-custom-models (which is slightly different from the procedure with the version on CRAN)

If you still got issues after that, let me know.

vmombo commented 1 day ago

Hello All, I have a Keras model, is it possible to make it work with it?

martinju commented 1 day ago

Hello All, I have a Keras model, is it possible to make it work with it?

Yes. Please see the main vignetten on the pkgdown site for instructions.

vmombo commented 1 day ago

Thanks. I tried and that works well on model I built with the Boston Dataset. However with one of my real model I get this

`explanation <- explain( model = finalModel, x_explain = sdt.test, x_train = dt.test[1:100, ], approach = "empirical", # Choose explanation approach phi0 = phi0, # Specify baseline value predict_model = predict_model.EnsembleModel, get_model_specs = get_model_specs,

max_n_coalitions = 2^10, # Reasonable limit

verbose = "progress" )

Success with message: max_n_coalitions is NULL or larger than or 2^n_features = 3.86856262276681e+25, and is therefore set to 2^n_features = 3.86856262276681e+25.

Error in .makeMessage(..., domain = domain) : argument is missing, with no default`

I am trying to understand this error by looking on the source code I still don't understand. Any suggest about where I should investigate.? PS: I use the version from the main of this github repo.

martinju commented 23 hours ago

It seems you have an enormous number of features? (83 or 84 features?)

I have not seen this error message before, but in any case, you should first try to reduce the number of features to see if it works then. Also, set max_n_coalitions to 500 or something to reduce runtime when testing.

vmombo commented 23 hours ago

Yes, excellent. But I've dug into the code a little bit to have like a traceback of my error.

I think apart from the big number of features. my error is coming from the warning in the code. Especially for me . In the explain function, we call setup and in the function check_computability my input leads me to this condition:

if (isFALSE(is_groupwise) && n_features > 30) { warning( "Due to computation time, we strongly recommend enabling iterative estimation with iterative = TRUE", " when n_features > 30.\n", ) }

However a warning written this way produce an error because of the "," at the end. You can reproduce my error just by calling this line : warning( "Due to computation time, we strongly recommend enabling iterative estimation with iterative = TRUE", " when n_features > 30.\n", )

martinju commented 22 hours ago

Oh, thanks a lot for catching that bug! I will fix it ASAP

martinju commented 22 hours ago

@vmombo I just merged the fix to main

vmombo commented 17 hours ago

thank you very much. I have just seen your comment