I am confused about the relationship between assign a module as another module’s class member vs. reused the module. Supposed I want to share word embedding between two networks, I used to write code as the following:

class Model(nn.Module):
    def __init__():
        self.embedding = nn.Embedding(10000, 200)
        self.net_a = SubModule(self.embedding)
        self.net_b = SubModule(self.embedding)

   def forward(input):
       return self.net_a(input) + self.net_b(input)

class SubModule(nn.Module):
    def __init___(embedding):
        self.embedding = embedding
        self.fc = nn.Linear(200, 200)

   def forward(self, input):
        return self.fc(self.embedding(input))


In the above code, is the embedding correctly shared between net_a and net_b? It seems that the constructor of SubModule create another copy of the embedding so that there are three embedding matrix stored in the model?