Search code examples
deep-learninglstmpytorchbackpropagation

LSTMCell parameters is not shown Pytorch


I have the following code:

class myLSTM(nn.Module):
def __init__(self, input_size, output_size, hidden_size, num_layers):
    super(myLSTM, self).__init__()
    self.input_size = input_size + 1 
    self.output_size = output_size
    self.hidden_size = hidden_size
    self.num_layers = num_layers
    self.layers = []
    new_input_size = self.input_size
    for i in xrange(num_layers):
        self.layers.append(LSTMCell(new_input_size, hidden_size))
        new_input_size = hidden_size
    self.linear = nn.Linear(hidden_size, output_size)
    self.softmax = nn.Softmax()

def forwardLayers(self, input, hns, cns, layers):
    new_hns = []
    new_cns = []
    (hn, cn) = layers[0](input, (hns[0], cns[0]))
    new_hns.append(hn)
    new_cns.append(cn)
    for i in xrange(1, len(layers)):
        (hn, cn) = layers[i](hn, (hns[i], cns[i]))
        new_hns.append(hn)
        new_cns.append(cn)
    return hn, (new_hns, new_cns)

def forward(self, input, hx):
    actions = []
    hns, cns = hx
    action = torch.Tensor([[0.0]])
    for i in range(len(input)):
        new_input = input[i]
        new_input = new_input.view(1, -1)
        output, (hns, cns) = self.forwardLayers(new_input, hns, cns, self.layers)
        output = self.softmax(self.linear(output))

    return output

Now when I call the following code to see the parameters of my network:

for name, param in myLSTM_object.named_parameters():
        if param.requires_grad:
            print name, param.data

What I get is:

linear.weight tensor([[ 0.5042, -0.6984],
    [ 0.0721, -0.4060]])
linear.bias tensor([ 0.6968, -0.4649])

So, it completely misses the parameters of LSTMCell. Does this mean the parameters of LSTMCell is not trained. What should I do to see LSTMCell parameters?


Solution

  • This is to be expected - storing modules in list, dict, set or other python containers does not register them with the module owning said list, etc. To make your code work, use nn.ModuleList instead. It's as simple as modifying your __init__ code to use

    layers = []
    new_input_size = self.input_size
    for i in xrange(num_layers):
        layers.append(LSTMCell(new_input_size, hidden_size))
        new_input_size = hidden_size
    self.layers = nn.ModuleList(layers)