Pytorch没有显示LSTMCell参数

问题描述 投票:0回答:1

我有以下代码:

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

现在,当我调用以下代码来查看我的网络参数时:

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

我得到的是:

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

因此,它完全错过了LSTMCell的参数。这是否意味着LSTMCell的参数未经过训练。我该怎么做才能看到LSTMCell参数?

deep-learning lstm pytorch backpropagation
1个回答
2
投票

这是可以预期的 - 在listdictset或其他python容器中存储模块不会将它们注册到拥有所述list等的模块。要使代码工作,请使用nn.ModuleList。它就像修改你的__init__代码一样简单

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)
© www.soinside.com 2019 - 2024. All rights reserved.