deeplift icon indicating copy to clipboard operation
deeplift copied to clipboard

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Open williamcaicedo opened this issue 5 years ago • 5 comments

Hi,

I'm trying to analise a model I built using the Tensorflow.keras implementation of Keras instead of Keras proper. I get this error when I try to call kc.convert_model_from_saved_files:

    435                 layer_weights = [np.array(nested_model_weights[x]) for x in
    436                                  nested_model_weights.keys() if
--> 437                                  x.startswith(layer_name+"/")]
    438                 if (len(layer_weights) > 0):
    439                     layer_config["config"]["weights"] = layer_weights

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Looking around I found this, that essentially says x is a bytes object, so you're calling bytes.startswith() instead of str.startswith(). Is that the intended behaviour?

Thanks.

williamcaicedo avatar Sep 17 '19 03:09 williamcaicedo

Hi, I suspect this might be a difference in how models are saved in tf.keras versus "keras proper". I actually have not tested model loading with the format that is saved by tf.keras, and I'm not sure how many things might break when using tf.keras. Happy to accept pull requests, but I unfortunately don't think I have the bandwidth to look into this myself, especially given that there are other more flexible deeplift implementations out there (listed on the FAQ) - albeit they don't support the RevealCancel rule.

AvantiShri avatar Sep 17 '19 18:09 AvantiShri

Yeah, it's a bummer the RevealCancel rule is not supported by Shap. I've used DeepLIFT in my PhD research and the RevealCancel rule was a big part of the why. I'll try to look into this and see what I can do. Thanks.

williamcaicedo avatar Sep 17 '19 21:09 williamcaicedo

Yeah, it's a bummer the RevealCancel rule is not supported by Shap. I've used DeepLIFT in my PhD research and the RevealCancel rule was a big part of the why. I'll try to look into this and see what I can do. Thanks.

Hi @williamcaicedo , I met the same issue when calling kc.convert_model_from_saved_files. Could you tell me how you solve it? I have tried solution from this link (https://stackoverflow.com/questions/19827615/startswith-typeerror-in-function), but it didnot work. Thanks.

jinzh154 avatar Dec 27 '20 18:12 jinzh154

@jinzh154 sorry mate, I ended up using SHAP (https://github.com/slundberg/shap) instead. It doesn’t support the reveal-cancel rule but I got good results with it.

williamcaicedo avatar Dec 27 '20 21:12 williamcaicedo

@jinzh154 sorry mate, I ended up using SHAP (https://github.com/slundberg/shap) instead. It doesn’t support the reveal-cancel rule but I got good results with it.

Thanks, it helps me to make decision. :-)

jinzh154 avatar Dec 28 '20 03:12 jinzh154