title: PyTorch : How to properly create a list of nn.Linear()
body: <p>I have created a class that has nn.Module as subclass.</p>
  
  <p>In my class, I have to create N number of linear transformation, where N is given as class parameters.</p>
  
  <p>I therefore proceed as follow :</p>
  
  <pre><code>    self.list_1 = []
  
      for i in range(N):
          self.list_1.append(nn.Linear(self.x, 1, bias=mlp_bias))
  </code></pre>
  
  <p>In the forward method, i call these matrices (with list_1[i]) and concat the results.</p>
  
  <p>Two things :</p>
  
  <p>1)</p>
  
  <p>Even though I use model.cuda(), these Linear transform are used on cpu and i get the following error : </p>
  
  <blockquote>
    <p>RuntimeError: Expected object of type Variable[torch.cuda.FloatTensor] but found type Variable[torch.FloatTensor] for argument #1 'mat2'</p>
  </blockquote>
  
  <p>I have to do </p>
  
  <pre><code>self.list_1.append(nn.Linear(self.x, 1, bias=mlp_bias).cuda())
  </code></pre>
  
  <p>This is not required if instead, i do :</p>
  
  <pre><code>self.nn = nn.Linear(self.x, 1, bias=mlp_bias)
  </code></pre>
  
  <p>and then use self.nn directly.</p>
  
  <p>2)</p>
  
  <p>For more obvious reason, when I print(model) in my main, the Linear matrices in my list arent printed.</p>
  
  <p>Is there any other way. maybe using bmm ? I find it less easy, and i actually want to have my N results separately.</p>
  
  <p>Thank you in advance,</p>
  
  <p>M</p>