SeldonIO / alibi

Algorithms for explaining machine learning models
https://docs.seldon.io/projects/alibi/en/stable/
Other
2.37k stars 248 forks source link

Error with incorporating auto-encoder in CEM Explainer #145

Closed divyat09 closed 4 years ago

divyat09 commented 4 years ago

I am using CEM to generate explanations over a dataset with three continuous features and a binary outcome variable. The black box machine learning model is a single layer neural network with sigmoid activation.

When I generate explanations using CEM explainer without the auto-encoder, the code works fine without any errors. However, when I use an auto-encoder with the CEM explainer I get the following error:


ValueError Traceback (most recent call last)

in 11 cem = CEM(sess, pred_model, mode, shape, ae_model=ae_model, kappa=kappa, beta=beta, feature_range=feature_range, 12 gamma=gamma, max_iterations=max_iterations, ---> 13 c_init=c_init, c_steps=c_steps, learning_rate_init=lr, clip=clip, no_info_val=no_info_val) 14 15 ~/CounterFactuals/env/lib/python3.5/site-packages/alibi/explainers/cem.py in __init__(self, sess, predict, mode, shape, kappa, beta, feature_range, gamma, ae_model, learning_rate_init, max_iterations, c_init, c_steps, eps, clip, update_num_grad, no_info_val, write_dir) 181 self.loss_ae_s = self.gamma * tf.square(tf.norm(self.ae(self.delta_s) - self.delta_s)) 182 elif self.mode == "PN" and callable(self.ae): --> 183 self.loss_ae = self.gamma * tf.square(tf.norm(self.ae(self.adv) - self.adv)) 184 self.loss_ae_s = self.gamma * tf.square(tf.norm(self.ae(self.adv_s) - self.adv_s)) 185 else: # no auto-encoder available ~/CounterFactuals/env/lib/python3.5/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs) 432 # Raise exceptions in case the input is not compatible 433 # with the input_spec set at build time. --> 434 self.assert_input_compatibility(inputs) 435 436 # Handle mask propagation. ~/CounterFactuals/env/lib/python3.5/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs) 291 'but it received ' + str(len(inputs)) + 292 ' input tensors. Input received: ' + --> 293 str(inputs)) 294 for input_index, (x, spec) in enumerate(zip(inputs, input_spec)): 295 if spec is None: ValueError: Layer model_3 expects 3 inputs, but it received 1 input tensors. Input received: [] --------------------------------------------------------------------------- The code that I use for generating explanations using CEM: --------------------------------------------------------------------------- sess = tf.Session() K.set_session(sess) sess.run(tf.global_variables_initializer()) cem = CEM(sess, pred_model, mode, shape, ae_model=ae_model, kappa=kappa, beta=beta, feature_range=feature_range, gamma=gamma, max_iterations=max_iterations, c_init=c_init, c_steps=c_steps, learning_rate_init=lr, clip=clip, no_info_val=no_info_val) for idx in range(0, len(train_dataset)): explain_x = train_dataset[idx] explanation = cem.explain(explain_x, verbose=True) print('Done for instance idx: ', idx) sess.close() K.clear_session() --------------------------------------------------------------------------- The error message says the Input tensor with shape (1,3) is wrong, however, my auto-encoder was trained on inputs of the same dimension ( Three continuous features, hence (batch_size, 3) ).
jklaise commented 4 years ago

Hmm, is the ae model a keras model (as opposed to tf.keras)? What about pred_model - you said it's black box, but is there an underlying framework?

divyat09 commented 4 years ago

Both the pred_model and the autoencoder are tf.keras model. I think the error is because I have a variational autoencoder, hence I also need to input the noise from the standard normal distribution for sampling. Using auto-encoder should do the job here, however, are variational auto-encoders supported? Thanks for your help.

jklaise commented 4 years ago

That's strange, the noise sampling should be part of the VAE model, so as long as you can do a forward and backward pass it should work with VAEs without special handling.

divyat09 commented 4 years ago

Sure, due to some errors with my Keras model I had noise sampling as part of the input to the model. It should work fine with noise sampling as part of the VAE model. Thanks.