0
votes

I use python and tensorflow CNN for text classification. lrp_toolbox is a explain model of the neural network, it will provide the reason of classification of neural network model like "CNN", "RNN". The input of lrp_toolbox is trained "CNN" model with the following format.

I don't know how to make this format from the sess or graph of tensorflow CNN. I try to pickle the sess use code:

filename = 'trainedCNN_model_%s.pickle' % str(current_step) pickle.dump(sess, open(filename, 'wb')),

but it fails and shows TypeError: can't pickle module objects.

And actually, I think sess can't meet the input format of lrp_toolbox. The example input of lrp_toolbox is text format like

Linear 2 3 -2.01595799878 -2.05379403106 0.688953420218 1.20338836267 -1.7518249173 -1.90515935663 -0.519917325831 0.400368842573 0.0699950389094 Tanh Linear 3 3 -1.18542075899 -1.62921811225 0.134698917906 0.111469267787 1.85227669488 -0.350827317469 0.102194311719 -1.67678396935 0.256312518679 0.116095097279 -0.0138454065897 0.0469443958438 Tanh Linear 3 2 1.10940574175 0.26799513777 2.51842248017 -1.5497671807 -0.606042655911 0.197763706892 -0.115832556216 0.115832556216 SoftMax Here is the manual of lrp_toolbox.

2

2 Answers

0
votes

You need to implement some method, when you need to serialize object by pickle.

TypeError: can't pickle module objects. As this error message explain, sess is not be implemented method for serialize sess object. And also, I can not see what value the sess has. Could you explain how you made the sess?

ref) https://docs.python.org/3/library/pickle.html

0
votes

Once you have a trained neural network model (be it trained with Tensorflow or some other neural network toolbox), you basically just have a set of weights and biases as floating point values.

Now, in order to convert this model to the plain text file format of the lrp_toolbox, such that you can subsequently explain your model with the Python Implementation of the lrp_toolbox, you simply need to manually write a function that saves your model weights+biases in a plain text file, following the format described in the Section The shared plain text file format pages 16-17 of the manual.

This means, that you have to write a function very similar to the function _write_txt(model, path) of the lrp_toolbox, that takes as first input say your trained Tensorflow model, and as second input the output text file path, and that writes all the weights+biases (as well as the sequence of other layers of your model: pooling and activation) in this output file in human-readable raw text.

Then, you can load your model in the lrp_toolbox with something like model_io.read('my_model.txt'), and perform LRP on some data (for reading the data, you can use data_io.read('my_input')). See a full usage example here.