title: How can I access layers in a pytorch module by index?
body: <p>I am trying to write a pytorch module with multiple layers. Since I need the intermediate outputs I cannot put them all in a Sequantial as usual. On the other hand, since there are many layers, what I have in mind is to put the layers in a list and access them by index in a loop. Below describe what I am trying to achieve:</p>
  <pre><code>import torch
  import torch.nn as nn
  import torch.optim as optim
  
  class MyModel(nn.Module):
      def __init__(self):
          super(MyModel, self).__init__()
  
          self.layer_list = []
  
          self.layer_list.append(nn.Linear(2,3))
          self.layer_list.append(nn.Linear(3,4))
          self.layer_list.append(nn.Linear(4,5))
  
      def forward(self, x):
          res_list = [x]
          for i in range(len(self.layer_list)):
              res_list.append(self.layer_list[i](res_list[-1]))
          return res_list
  
  
  model = MyModel()
  x = torch.randn(4,2)
  y = model(x)
  
  print(y)
  
  optimizer = optim.Adam(model.parameters())
  </code></pre>
  <p>The forward method works fine, but when I want to set an optimizer the program says</p>
  <pre><code>ValueError: optimizer got an empty parameter list
  </code></pre>
  <p>It appears that the layers in the list are not registered here. What can I do?</p>