AttributeError: 'LSTM' object has no attribute 'weight_ih_l'

From documentation, LSTM module carries variable weight_ih_l[k]. How can I access the variable? I tried things like rnn.weight_ih_l[0], in which rnn is the name of my LSTM module, and it throws error:

Traceback (most recent call last):
  File "", line 194, in <module>
  File "", line 165, in train
    loss += add_dimension_glasso(model.rnn.weight_ih_l[0], 0)
  File "/Users/evan/anaconda2/envs/py35/lib/python3.5/site-packages/torch/nn/modules/", line 262, in __getattr__
    type(self).__name__, name))
AttributeError: 'LSTM' object has no attribute 'weight_ih_l'

It is easy to reproduce the error. I use skeleton from, and in, I add the following function:

def add_dimension_glasso(var, axis=0):
    return var.pow(2).sum(axis=axis).add(1e-8).pow(1/2.).sum()

and modified train() a little bit, with only 1 line added:

        loss = criterion(output.view(-1, ntokens), targets)
        loss += add_dimension_glasso(model.rnn.weight_ih_l[0], 0) # added code!

Can anyone help me with this? I simply want to get weight_ih_l. Thanks!


$ python -c "import torch; print(torch.__version__)"

Edit: solved.

I print model.rnn.state_dict() to find out I should use weight_ih_l0 and weight_ih_l1 (given 2 layers lstm). I feel the way documentation shows is somewhat misleading.