如何将tensorflow训练好的CNN神经网络转为lrp_toolbox_master的输入格式?
How to transfer the tensorflow trained CNN neural network to the input format of lrp_toolbox_master?
我使用 python 和 tensorflow CNN 进行文本分类。
lrp_toolbox是神经网络的解释模型,它会提供神经网络模型分类的原因,如"CNN"、"RNN"。
lrp_toolbox 的输入使用以下 格式训练 "CNN" 模型。
我不知道如何从tensorflow CNN的sess或graph制作这种格式。
我尝试腌制 sess 使用代码:
filename = 'trainedCNN_model_%s.pickle' % str(current_step)
pickle.dump(sess, open(filename, 'wb'))
,
但失败并显示 TypeError: can't pickle module objects
。
其实我觉得sess不能满足lrp_toolbox的输入格式。 lrp_toolbox 的示例输入是
这样的文本格式
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
这是lrp_toolbox.
的手册
当你需要通过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?
一旦你有了一个经过训练的神经网络模型(无论是用 Tensorflow 还是其他一些神经网络工具箱训练),你基本上只有一组权重和偏差作为浮点值。
现在,为了将此模型转换为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的纯文本文件格式。
这意味着,您必须编写一个与 lrp_toolbox 的函数 _write_txt(model, path)
非常相似的函数,该函数将您训练好的 Tensorflow 模型作为第一个输入,并将输出作为第二个输入文本文件路径,并以人类可读的原始文本形式在此输出文件中写入所有权重+偏差(以及模型其他层的序列:池化和激活)。
然后,您可以在 lrp_toolbox 中加载您的模型,例如 model_io.read('my_model.txt')
,并对某些数据执行 LRP(要读取数据,您可以使用 data_io.read('my_input')
)。查看完整的用法示例 here。
我使用 python 和 tensorflow CNN 进行文本分类。 lrp_toolbox是神经网络的解释模型,它会提供神经网络模型分类的原因,如"CNN"、"RNN"。 lrp_toolbox 的输入使用以下 格式训练 "CNN" 模型。
我不知道如何从tensorflow CNN的sess或graph制作这种格式。 我尝试腌制 sess 使用代码:
filename = 'trainedCNN_model_%s.pickle' % str(current_step)
pickle.dump(sess, open(filename, 'wb'))
,
但失败并显示 TypeError: can't pickle module objects
。
其实我觉得sess不能满足lrp_toolbox的输入格式。 lrp_toolbox 的示例输入是
这样的文本格式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
这是lrp_toolbox.
当你需要通过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?
一旦你有了一个经过训练的神经网络模型(无论是用 Tensorflow 还是其他一些神经网络工具箱训练),你基本上只有一组权重和偏差作为浮点值。
现在,为了将此模型转换为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的纯文本文件格式。
这意味着,您必须编写一个与 lrp_toolbox 的函数 _write_txt(model, path)
非常相似的函数,该函数将您训练好的 Tensorflow 模型作为第一个输入,并将输出作为第二个输入文本文件路径,并以人类可读的原始文本形式在此输出文件中写入所有权重+偏差(以及模型其他层的序列:池化和激活)。
然后,您可以在 lrp_toolbox 中加载您的模型,例如 model_io.read('my_model.txt')
,并对某些数据执行 LRP(要读取数据,您可以使用 data_io.read('my_input')
)。查看完整的用法示例 here。