Mari:

How can I freeze network in initial layers except two last layers?


You can set the .requires_grad attribute of the parameters of the first layers to False to freeze them.



 Mari:

Do I need to implement filter in optimizer for those fixed layers? if yes how?


You don’t necessarily need to filter out frozen parameters, as they won’t be updated, but could do it via e.g torch.optim.SG(filter(lambda p: p.requires_grad, model.parameters()), lr=...).