-
Notifications
You must be signed in to change notification settings - Fork 263
Description
Dear Team,
I get the following error when trying to initialise the CEM explainer:
TypeError
cem = CEM(
lr,
mode,
(...)
clip=0.5,
)
cem.fit(X_train, no_info_type='median')
--> explanation = cem.explain(X, verbose=False)
print('Original instance: {}'.format(explanation.X))
print('Predicted class: {}'.format([explanation.X_pred]))
[Python\Python310\lib\site-packages\alibi\explainers\cem.py:695] in CEM.explain(self, X, Y, verbose)
693 # find best PP or PN
694 self.best_attack = False
--> 695 best_attack, grads = self.attack(X, Y=Y, verbose=verbose)
697 # output explanation dictionary
698 data = copy.deepcopy(DEFAULT_DATA_CEM)
[Python\Python310\lib\site-packages\alibi\explainers\cem.py:564] in CEM.attack(self, X, Y, verbose)
562 grads_vars_graph = self.sess.run(self.compute_grads)
563 grads_graph = [g for g, _ in grads_vars_graph][0]
--> 564 grads_graph = np.clip(grads_graph, self.clip[0], self.clip[1])
566 # apply gradients
567 grads = grads_graph + grads_num_s
TypeError: 'float' object is not subscriptable
This is my code:
def lr_model():
x_in = Input(shape=(len(feature_names),))
x_out = Dense(2, activation='softmax')(x_in)
lr = Model(inputs=x_in, outputs=x_out)
lr.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
return lr
lr = lr_model()
lr.summary()
lr.fit(X_train,y_train,epochs=500, verbose=0, batch_size=None)
idx = 1
X = X_test[idx].reshape((1,) + X_test[idx].shape)
print(f'Prediction on instance to be explained: {np.argmax(lr.predict(X))}')
print(f'Prediction probabilities for each class on the instance: {lr.predict(X)}')
class_names=['0','1']
print(type(X_train))
mode = "PP"
shape = (1,) + X_train.shape[1:]
feature_range = (
X_train.min(axis=0).reshape(1, -1) - 0.1,
X_train.max(axis=0).reshape(1, -1) + 0.1
)
cem = CEM(
lr,
mode,
shape=shape,
kappa=0.5,
beta=0.1,
feature_range=feature_range,
max_iterations=1000,
c_init=1.0,
c_steps=10,
learning_rate_init=0.1,
clip=0.5,
)
cem.fit(X_train, no_info_type='median')
explanation = cem.explain(X, verbose=False)
The model can show the result
Model: "model_1"
Layer (type) Output Shape Param
input_2 (InputLayer) [(None, 152)] 0
dense_1 (Dense) (None, 2) 306
=================================================================
Total params: 306
Trainable params: 306
Non-trainable params: 0
Prediction on instance to be explained: 1
Prediction probabilities for each class on the instance: [[0.3051093 0.6948907]]
But when analyzing the model, this error always occurs.
Please assist! It would be much appreciated!