Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use finetune in Pytorch

Shulou Source: shulou.com Published: 2022-06-02 05:53:18 10月15日 Update

How to use finetune in Pytorch, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

1. fixed parameters

for name, child in model.named_children(): for param in child.parameters(): param.requires_grad = False

After that, only pass in parameters that need to be passed back, otherwise an error will be reported.

filter(lambda param: param.requires_grad, model.parameters())

2. Lower the learning rate and speed up the decay

Finetune is fine-tuning on a pre-trained model, and the learning rate cannot be too large.

It is not clear that the learning rate could be reduced more rapidly. This way, the stepsize can be smaller when using the step policy.

base_lr trained directly from raw data is generally 0.01, fine tuning is smaller than 0.01, set to 0.001

Smaller than direct training, direct training stepsize is 100000, finetune stepsize: 50000

3. Fix bn or cancel dropout:

Batchnorm affects the effectiveness of training, tracking the mean and variance of samples with each batch. For fixed networks, bn should use global values

def freeze_bn(self): for layer in self.modules(): if isinstance(layer, nn.BatchNorm2d): layer.eval()

During training, model.train() modifies the pattern, freeze_zn() should follow here

4. filtering parameters

When training, for the optimizer, only the parameters that need to be changed should be passed, otherwise an error will be reported.

filter(lambda p: p.requires_grad, model.parameters()) Does it help you to read the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Tags: Training parameters learning clarity speed will report help original global content mean response amplitude effect numerical value data article novice variance is in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Docker Shulou Tech Info MariaDB