keras:保存keras学习好的深度神经网络模型参数为二进制和txt文件

来源:互联网 发布:mybatis显示sql语句 编辑:程序博客网 时间:2024/05/26 12:04

http://blog.csdn.net/linmingan/article/details/50906141

由于工程需要,保存为hdf5的keras权值在c/c++中的读取比较不方便。因此将keras中的权值剥离出来,并保存为二进制文件或者txt文件。在进行代码的编写之前我们必须先知道keras中model类对于每一层的参数如何表示的。以下,我将以加入Batch Normalization的5层神经网络(每层的节点数为[500 400 300 200 100],输入为:39)作为例子,来解读keras的参数表示。

假设我们已经学习好了上述的神经网络,怎么我们可以得到一个包含有各种对象的keras对象:model。model的具体内容如下图(model内部结构的显示由Pycharm调试功能中的观察变量功能得来)。图中有非常多变量,但是我们只关心其中的layers变量。

我们可以展开layers变量,得到下图。从_len_={int} 17,知道我们的模型共有17层(按正常理解应该只有5层,但是在keras中,将上一层的输出乘以该层的权值,以及通过激活函数这两个都看做是一层。因为我们的模型加入了BN操作,所有BN操作也看做一层。这样一来我们理解的一层就扩展到了三层。),接下来的00,01,...16就是里面具体的层的内容了,我们要保存的参数也都在里面。


以第一层隐藏层作为例子,来看看里面具体的内容。第一层隐藏层由00,01,02这三个变量构成,分别展开,得到下面三个图。可以看到参数W和b都在00层,但在01层也会有记录(在previous变量中)。02中主要记录BN中的gamma,beta,running_mean和running_std。其他隐藏层的参数也是这样的,我们不具体看了。keras中的每一层都是用list类型来表示,其中的元素是TesorType(float32,matrix)或者TesorType(float32,vector)的类型(theano的矩阵和向量类型)。theano提供了对每一层的参数获得函数get_params(),这个函数能够从model变量中获取每一层的参数,也就是我们上面讲的list类型。由于获得后的list中是theano的矩阵和向量,为了进一步得到我们可以观察的矩阵和向量theano提供了get_value()函数,通过这个函数可以将TesorType(float32,matrix)或者TesorType(float32,vector)转化为numpy中的ndarray格式的矩阵和向量。这样就能够利用numpy提供的二进制保存函数tofile()和txt保存函数savetxt()保存学习好的神经网络模型参数。

00


01


02


通过上面的分析,我编写了如下的代码,来保存每一层的W,b,gamma,beta,mean和std。代码如下:

model = model_from_json(open('model54321_architecture.json').read())model.load_weights('model54321_weights.h5')for i in range(2,16,3):#save the BN params    param = model.layers[i].get_params()    gamma = param[0][0].get_value() #get the gamma    beta = param[0][1].get_value() #get the beta    mean = param[3][0][0].get_value() #get the mean    std = param[3][1][0].get_value() #get the std    bn = np.c_[gamma,beta,mean,std] # combine the gamma beta mean std as a n*4 matrix    bn_name = str(i)+'bn'+'.txt'    np.savetxt(bn_name,bn,fmt='%f') #save as txt    bn_name = str(i)+'bn'+'.bin'    bn.tofile(bn_name) #save as binfor i in range(0,16,3):#save the W and b    var = model.layers[i].get_params()    w = var[0][0].get_value() #get W    b = var[0][1].get_value() #get b    #save as txt    w_name = str(i)+'w'+'.txt'    np.savetxt(w_name,w,fmt='%f')    b_name = str(i)+'b'+'.txt'    np.savetxt(b_name,b,fmt='%f')    #save as bin    w_name = str(i)+'w'+'.bin'    w.tofile(w_name)    b_name = str(i)+'b'+'.bin'    b.tofile(b_name)


0 0
原创粉丝点击