xnet
xnet

Reputation: 551

How to freeze specific layers of a model inside a model?

My keras model is made up of multiple models. Each "sub-model" has multiple layers. How do I call out the layers in the "sub-model" and set trainability / freeze specific layers?

Upvotes: 2

Views: 1004

Answers (1)

JimmyOnThePage
JimmyOnThePage

Reputation: 965

I'll use an example of the VGG19 convolutional neural network in Keras, although it applies to any neural network architecture:

from keras.applications.vgg19 import VGG19 

model = VGG19(weights='imagenet')

You can visualise the layers using:

model.summary()

The summary will show the amount of trainable parameters in the network. To freeze certain layers, i.e. the last 5 layers in the network:

for layer in model.layers[:-5]:
    layer.trainable = False

Calling the summary again you'll see the amount of trainable parameters have reduced.

Upvotes: 2

Related Questions