Why if("bn" not in name): param.requires_grad_(False)

Do you know what is the reason for the lines

 if("bn" not in name):
                param.requires_grad_(False)

in the following code snippet?

        mnet = models.mobilenet_v2(pretrained=True)
        for name, param in mnet.named_parameters():
            if("bn" not in name):
                param.requires_grad_(False)

Hi,

BatchNormalization has two trainable (weight and bias / γ and β) and two mini - batch dependent (mean and variance) parameters. Therefore your code snippet freezes all layers except for the BatchNormalization - Layers what can be useful for finetuning (setting the BatchNormalization - Layers requires_grad flag to false would therefore freeze the two trainable parameters).

Regards,
Unity05

1 Like