Search code examples
torch

torch/nn: What's the canonical way to multiply by a constant matrix?


nn.MM requires a table argument of the matrices that will be multiplied. In my case, one of the matrices is the output of some previously defined model (e.g. an nn.Sequential) and the other is just a constant matrix. How can I inject a constant into nn's pipeline and should I be worried that optimizer will start changing it if I do?

I'm aware that I could solve the injection problem by:

  1. Writing my own nn.Module. This seems heavy handed.
  2. Breaking the model into two parts and manually injecting the constant. I really want the model to just be some nn.Module subclass that gets called with :forward(input) and allows consumers to be blissfully ignorant of the existence of the constant.
  3. Using nn.ParallelTable, but that would also expose the constant to model consumers.
  4. Using nn.Linear with no bias and overwriting the weights. I'm just not sure how to prevent the optimizer from performing the update.

Solution

  • You can create an nn.Linear and the override the :accGradParameters to be a no-op function

    m = nn.Linear(100,200)
    -- copy your weights / bias into m.weight / m.bias
    m.accGradParameters = function() end
    -- m is a constant multiplier thing