Is it possible to save a trained layer to use laye

2019-04-10 09:49发布

I haven't used Keras and I'm thinking whether to use it or not.

I want to save a trained layer to use later. For example:

  1. I train a model.
  2. Then, I gain a trained layer t_layer.
  3. I have another model to train which consists of layer1, layer2, layer3 .
  4. I want to use t_layer as layer2 and not to update this layer(i.e. t_layer does not learn any more).

This may be an odd attempt, but I want to try this. Is this possible on Keras?

1条回答
Emotional °昔
2楼-- · 2019-04-10 10:21

Yes, it is.

You will probably have to save the layer's weights and biases instead of saving the layer itself, but it's possible.

Keras also allows you to save entire models.

Suppose you have a model in the var model:

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

This is a list of numpy arrays, very probably with two arrays: weighs and biases. You can simply use numpy.save() to save these two arrays and later you can create a similar layer and give it the weights:

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])

You can make layers untrainable (must be done before the model compilation):

model.layers[2].trainable = False    

Then you compile the model:

model.compile(.....)    

And there you go, a model, whose one layer is untrainable and has weights and biases defined by you, taken from somewhere else.

查看更多
登录 后发表回答