Error in shap - AttributeError: module 'shap' has no attribute 'TreeExplainer' - python

when I'm using:
gb_explainer = shap.TreeExplainer
I get this error:
AttributeError: module 'shap' has no attribute 'TreeExplainer'
The full code:
def create_shap_tree_explainer(self):
self.gb_explainer = shap.TreeExplainer(self.gb_model)
self.shap_values_X_test = self.gb_explainer.shap_values(self.X_test)
self.shap_values_X_train = self.gb_explainer.shap_values(self.X_train)
The gradient boosting classifier model is:
gbc_model = Create_Gradient_Boosting_Classifier(X_train, y_train, ps)

Which SHAP do you use?
Please check it.
print(shap.__version__)
Also, did you install SHAP via pip or conda? Where your python access when you run the script? I think, after such checks you will get the answer what is going on.

Related

Talos, syntax to select best_model

I'm running a toy model for learning, on Ubuntu 21.10, in a conda environment that comprises python 3.74, keras 2.4.3 and talos 1.0, among many other packages. I've run a talos scan using this code:
jam1 = talos.Scan(data,
labels[0,],
model = DLAt,
params = ParamsJam1,
experiment_name = "DL2Outputs"
)
However I've tried everything I can find but can not find correct syntax to select the best model using talos.best_model.
bm = talos.best_model(metric='loss', asc=False)
just gets this error.
AttributeError: module 'talos' has no attribute 'best_model'
Is this not the correct function to achieve this?
The best model isn't saved in the package, it's saved in the Scan object:
bm.best_model(metric='loss', asc=False)

Can someone help me find a solution to error source in my code?

I am training a BI-LSTM-CRF model for a NER task. I was able to build the model, but when I fit it with the training data, googlecolab throw me an error.
Here is the code for my model below (or here : code for my model):
input_layer = layers.Input(shape=(MAX_SENTENCE,))
model = layers.Embedding(WORD_COUNT, DENSE_EMBEDDING, embeddings_initializer="uniform", input_length=MAX_SENTENCE)(input_layer)
model = layers.Bidirectional(layers.LSTM(LSTM_UNITS, recurrent_dropout=LSTM_DROPOUT, return_sequences=True))(model)
model = layers.TimeDistributed(layers.Dense(DENSE_UNITS, activation="relu"))(model)
crf_layer = CRF(units=TAG_COUNT)
output_layer = crf_layer(model)
ner_model = Model(input_layer, output_layer)
loss = losses.crf_loss
acc_metric = metrics.crf_accuracy
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
ner_model.compile(optimizer=opt, loss=loss, metrics=[acc_metric])
ner_model.summary()
Then after fitting the model, I get the following error:
AttributeError: 'Tensor' object has no attribute '_keras_history' ([error message for google colab][2])
Here is the list of my dependencies :Dependencies
Can someone help me out?
I'm not sure what module you're using because you didn't provide any but I'm guessing that's tensorflow. Try using lower versions of Python like Python 3.7 and also try installing a specific and compatible version: pip install tensorflow==1.13.1

Model object has no attribute save while reducing size of CoreML?

i am reducing the size of CoreML. I make this CoreML with Python Turicate but i am getting a error Model object has no attribute save. I have Python 2.7 and pip install coremltools==2.0b1 before execute python file. Here's my code -
import coremltools
from coremltools.models.neural_network.quantization_utils import *
model = coremltools.models.MLModel('/Users/Desktop/MLClassifier/animals.mlmodel')
lin_quant_model = quantize_weights(model, 16, "linear")
lin_quant_model.save('/Users/Desktop/animals2.mlmodel')
My guess is you're not on macOS 10.14 or later (Mojave), in which case you don't get an MLModel but the model specification when you call quantize_weights() (according to the docs). No idea why, but that's what it says.
I have also faced this problem, running model quantization on Ubuntu (Python 3.8, coremltools==4.1).
I don't know why exactly this error occurs (maybe it should be run on macOS), but as you are getting specs, you can get and save model as follows:
model_fp16_specs = quantization_utils.quantize_weights(model_fp32, nbits=16)
model_fp16 = ct.models.MLModel(model_fp16_specs)
model_fp16.save("model_quantized.mlmodel")

Explain features from my keras object with lime R package

After obtaining accuracy metric from my keras binary classification model, I need know what the model made the predictions. So, I'm interested in variable importance. I use lime package.
library(lime)
explainer <- lime (
x = x_train,
model = model_keras,
bin_continuous = FALSE)
explanation <- explain (
x_test[1:20,], # Show first 20 samples
explainer = explainer,
n_labels = 1,
n_features = 5)
Explain function gives me the following error in py_get_attr_impl function: AttributeError: 'function' object has no attribute 'func_name'.
I have compiled keras model with R, but this Issue seems to be that error comes from Python version. Problems with Reticulate package?
It works with python 2.7 but generates error with python 3+.
Actually function attribute func_name was renamed in python 3+ to __name__.
lime package (models.R) has a line:
if (keras::get_layer(x, index = -1)$activation$func_name == 'linear')
I removed $func_name and the code worked for me.
I suppose this is not the best workaround, however the possible solution that comes to mind:
if (keras::get_layer(x, index = -1)$activation$__name__ == 'linear')
did not work with R.

Python error in SVM classifier.predict()

I am getting the following error when i perform classification of new data with the following command in Python:
classifier.predict(new_data)
AttributeError: python 'SVC' object has no attribute _dual_coef_
In my laptop though, the command works fine! What's wrong?
I had this exact error
AttributeError: python 'SVC' object has no attribute _dual_coef_
with a model trained using scikit-learn version 0.15.2, when I tried to run it in scikit-learn version 0.16.1. I did solve it by re-training the model in the latest scikit-learn 0.16.1.
Make sure you are loading the right version of the package.
Have you loaded the model based on which you try to predict?
In this case it can be a version conflict, try to re-learn the model using the same sklearn version.
You can see a similar problem here: Sklearn error: 'SVR' object has no attribute '_impl'
I had the same problem,I use Sklearn version 0.23.02 but I was trying to run an archive trained with a version 0.18... and my error said: "'SVC' object has no attribute 'break_ties'", I just retrained the model with my version and fix the problem I generate other svc.pickle to run with the 0.23.02 version and replace the oldie.
"""
X = X_train
y = y_train
"""
X = X_test
y = y_test
# Instantiate and train the classifier
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X, y)
# Check the results using metrics
from sklearn import metrics
y_pred = clf.predict(X)
print(metrics.confusion_matrix(y_pred, y))

Categories

Resources