I want to have a Module that is parametrized by a list of tensors. In the situation, when this Module is a “leaf” module in the pipeline (i.e. its parameters are trainable parameters), then it’s pretty simple: I just have to use ParameterList to store its parameters.
The question is, what should I do, if it’s not a “leaf” module, i.e. I somehow constructing it’s parameter tensors and only after that instantiating the module.
Consider this toy example: I want to encapsulate a rang-1 matrix decomposition:
class Rang1Matrix(nn.Module):
def __init__(self, vectors):
super().__init__()
self.vectors = vectors
def forward(self):
return t.matmul(self.vectors[0], self.vectors[1])
In this block of code, if I want to create a module with trainable parameters, then I just have to pass ParameterList
as a “vectors” argument. But what should I do, if I already have tensors “vector”, through which I want autodifferentiate?
In particular, I want Rang1Matrix
to fully support functions like .cuda()
, .cpu()
, .to()
, etc.