1

我还没有使用Keras,我在考虑是否使用它。是否可以保存经过训练的图层以在Keras上使用图层?

我想保存一个训练有素的图层以备后用。例如:

  1. 我训练模型。
  2. 然后,我获得了训练层t_layer
  3. 我有另一个模型来训练它由​​,layer2,layer3
  4. 我想使用t_layer作为layer2而不是更新此层(即t_layer不再学习)。

这可能是一个奇怪的尝试,但我想试试这个。这在Keras上可能吗?

回答

2

是的。

您可能必须保存图层的权重和偏差,而不是保存图层本身,但这是可能的。

Keras也允许你save entire models

假设你在var model有一个模型:

weightsAndBiases = model.layers[i].get_weights() 

这是numpy的阵列的列表,很可能与两个数组:重和偏见。你可以简单地使用numpy.save()保存这两个数组,以后你可以创建一个类似的层,并给它的权重:

from keras.layers import * 
from keras.models import Model 

inp = Input(....)  
out1 = SomeKerasLayer(...)(inp) 
out2 = AnotherKerasLayer(....)(out1) 
.... 
model = Model(inp,out2) 
#above is the usual process of creating a model  

#supposing layer 2 is the layer you want (you can also use names)  

weights = numpy.load(...path to your saved weights)  
biases = numpy.load(... path to your saved biases) 
model.layers[2].set_weights([weights,biases]) 

可以使层untrainable(必须在模型编译之前完成):

model.layers[2].trainable = False  

然后编译模型:

model.compile(.....)  

而且你去那里,一个模型,其一层为untrainable并具有由您定义的权重和偏见,从s拍摄其他地方。

相关问题