[0.4.1] 'LSTM' object has no attribute 'weight_ih_l'

Simple question. I’d like to see the initialized parameter of LSTM. How do I see it?
Do I need to always put lstm in the model to see the params?

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
​
torch.manual_seed(1)
torch.__version__
​
lstm = nn.LSTM(3, 3)  
​
lstm.weight_ih_l

AttributeError Traceback (most recent call last)
in ()
9 lstm = nn.LSTM(3, 3)
10
—> 11 lstm.weight_ih_l

~/anaconda3/envs/pytorch0.41/lib/python3.6/site-packages/torch/nn/modules/module.py in getattr(self, name)
516 return modules[name]
517 raise AttributeError(“‘{}’ object has no attribute ‘{}’”.format(
→ 518 type(self).name, name))
519
520 def setattr(self, name, value):

AttributeError: ‘LSTM’ object has no attribute ‘weight_ih_l’

You need to specify the layer index of your parameters.
I.e. if your LSTM has one layer (as in your example):

print(lstm.weight_ih_l0)
print(lstm.weight_hh_l0)
1 Like