Skip to content
Advertisement

How to fine-tune a functional model in Keras?

Taking a pre-trained model in Keras and replacing the top classification layer to retrain the network to a new task has several examples using a Sequential model in Keras. A sequential model has methods model.pop() and model.add() which make this fairly easy.

However, how is this achieved when using a functional model? This framework does not have method model.add().

How can I load a pretrained functional model in Keras, crop the last layer and replace it with a new one?

Current approach so far:

JavaScript

AttributeError: ‘Model’ object has no attribute ‘add’

Advertisement

Answer

You can use a pretrained functional model with the last layer removed as a layer. You may think of a model as a “bigger layer”. Then redefine a new model that wraps “bigger layer” and a new layer.

An example:

JavaScript

As a result, you can see that the parameters of the last layer of pretrained functional model are missing.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement