Search code examples
machine-learningneural-networktorch

How to get/set model's weight (delta) in torch?


Sorry for asking as a newbie to torch, but I promise to have search a lot through the documents and Internet.

There are two main demands I need, the first one is to get the weight delta after training for one or more batches, the second one is to set the new weight to model.

That means I want to update the weights by my own methods (with external library), will it be possible to achieve that in torch?

It seems that torch has an abstract module class [1] but its interface doesn't fit all my needs.

[1] https://github.com/torch/nn/blob/master/doc/module.md#nn.Module


Solution

  • Finally, I found the answer by referring to several of my colleagues.

    Understanding the getParameters() [1] correctly is the key point to solve the problem. getParameters() will get the flatten parameters (weights) and gradParameters (weights delta) and what's more, it's a memory transition and should be only called once as documented.

    This means the returned value of getParameters() is just what we want and the changes in the returned value will be reflected to the original model where updating the weights.

    So we can not only get the flatten weights by the parameters returned by getParameters() but also set the weights simply by parameters:copy(). We can absolutely use other torch.Tensor() methods to modify the weights.

    [1] https://github.com/torch/nn/blob/master/doc/module.md#flatparameters-flatgradparameters-getparameters