Search code examples
torchconv-neural-network

Finetune a Torch model


I have loaded a model in Torch and I would like to fine-tune it. For now I'd like to retrain the last 2 layers of the network (though in the future I may want to add layers). How can I do this? I have been looking for tutorials, but I haven't found what I am looking for. Any tips?


Solution

  • I don't know if I understood what you are asking for. If you want to leave the net as it was except for the 2 layers you want to train (or fine-tune) you have to stop the backpropagation on the ones you don't want to train, like this:

    for i=1, x do
      c = model:get(i)
      c.updateGradInput = function(self, inp, out) end
      c.accGradParameters = function(self,inp, out) end
     end
    

    Now only the layers outside of this loop will upgrade their parameters. If you want to add new layers just call model:insert(module, position), you can have a look here Torch containers

    If that was not what you were looking for, please elaborate more on the question.