ZeroorOne
ZeroorOne

Reputation: 31

How to stop updating the parameters of a part of a layer in a CNN model (not the parameters of the whole layer)?

For example, there are ten parameters(filters) in a CNN layer, how I can do to only update five of them and keep the rest unchanged?

Upvotes: 0

Views: 115

Answers (1)

Raffaele Spinoni
Raffaele Spinoni

Reputation: 127

In Pythorch is easy to freeze only part of the net thanks to the requires_grad property: Here is a simple script:

def freeze_layers(model, num_of_layers):
    freezed = 0
    for layer in model.children():
        freezed += 1
        if layer < num_of_layers:
            layer.requires_grad = False

Consider however that every model has a different structure and it can have leyer nested into each other, with this code you are iterating through the first level of layers in the net. I suggest you print the layers before, to understand the network structure spefic to your case.

Upvotes: 1

Related Questions