How to add nn.Embedding to model's parameters?

Hello! I want to know how to add nn.Embedding type variables to model’s parameters. My model is defined as below:

class Model(nn.Module):
    def __init__(self, a, b, hops):
        self.W1 = torch.nn.Embedding(a, b)
        self.W2 = torch.nn.Embedding(a, b)
        self.R_list = []
        for i in range(hops):
            R_list.append(torch.nn.Embedding(a, b))

When I check model.parameters(), only W1 and W2. are outputed, which means that R are not in model.paremeters. So I want to know how to add them. Thanks!!


I know torch.nn.Parameter() can put Tensor type in the model’s parameter.

do you mean something like this,

class Model(nn.Module):
    def __init__(self, a, b, hops):
        super().__init__()
        self.W1 = torch.nn.Embedding(a, b)
        self.W2 = torch.nn.Embedding(a, b)
        self.R_list = nn.ModuleList()
        for i in range(hops):
            self.R_list.append(torch.nn.Embedding(a, b))
1 Like

Yeah, nn.ModuleList() works for me! Thanks. :grinning: