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:
- I train a model.
- Then, I gain a trained layer
t_layer
. - I have another model to train which consists of
layer1
,layer2
,layer3
. - I want to use
t_layer
aslayer2
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?
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
: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:You can make layers untrainable (must be done before the model compilation):
Then you compile the model:
And there you go, a model, whose one layer is untrainable and has weights and biases defined by you, taken from somewhere else.