title: too many arguments in a Pytorch custom nn module forward function
body: <p>I'm trying to make a neural network in pytorch that has a variable number of layers. My problem is that apparently I am passing some sort of iterable with more than one item to a linear layer which can only take one argument. I just can't see why.</p>
  
  <p>So here is some code. First I created my own module and import it to my notebook later</p>
  
  <pre><code>import torch
  
  class NNet(torch.nn.Module):
      def __init__(self, layer_shapes, activation_functions):
          super(NNet, self).__init__()
          assert len(layer_shapes) == len(activation_functions) + 1
          self.layer_shapes = layer_shapes
          self.activation_functions = activation_functions
  
          linear_functions = list()
          for i in range(len(self.layer_shapes)-1):
              linear_functions.append(torch.nn.Linear(
                      self.layer_shapes[i], self.layer_shapes[i+1]))
  
          self.linear_functions = linear_functions
  
      def parameters(self):
          parameters = list()
          for function in self.linear_functions:
              parameters = parameters+list(function.parameters())
  
          return parameters 
  
      def forward(self, x):
          assert x.shape[1] == self.layer_shapes[0]
          y = x
          for i in range(len(self.layer_shapes)-1):
              lin = self.linear_functions[i](y)
              y = self.activation_functions[i](lin)
          return y
  </code></pre>
  
  <p>In the notebook, the error is in the <code>forward</code> function at <code>y = self.activation_functions[i](self.linear_functions[i](y))</code></p>
  
  <p>Now I try to use the MNIST dataset supplied by <code>torchvision</code> and use my own module.</p>
  
  <pre><code>batch_size = 100
  epochs = 500
  learning_rate = 0.001
  
  
  
  
  train_set = torchvision.datasets.MNIST(root = 
                                         '../../data',
                                          train=True,
                            transform=torchvision.transforms.ToTensor(),
                                          download=True)
  
  test_set = torchvision.datasets.MNIST(root = 
                                        '../../data',
                                        train=False,                                      
                            transform=torchvision.transforms.ToTensor(),
                                     download=True)
  
  train_loader = torch.utils.data.DataLoader(dataset=train_set, 
                                             batch_size=batch_size, 
                                             shuffle=True)
  
  test_loader = torch.utils.data.DataLoader(dataset=test_set, 
                                            batch_size=batch_size, 
                                            shuffle=False)
  
  model = nnet.NNet([784, 16, 10], [torch.nn.Tanh, 
                                    torch.nn.Softmax(dim=1)])
  
  loss_function = torch.nn.MSELoss()
  optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
  
  loss_items = list()
  
  for t in range(epochs):
      for i, (images, labels) in enumerate(train_loader):
          images = images.reshape(-1,28*28)
  
          outputs = model(images)
          loss = loss_function(outputs, labels)
          loss_items.append(loss.item())
  
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
  </code></pre>
  
  <p>This last for loop yields the error:</p>
  
  <pre><code>TypeError                      Traceback (most recent call last)
  &lt;ipython-input-6-4ccb4b105a41&gt; in &lt;module&gt;()
        5         images = images.reshape(-1,28*28)
        6 
  ----&gt; 7         outputs = model(images)
        8         loss = loss_function(outputs, labels)
        9         loss_items.append(loss.item())
  
  ~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in 
  __call__(self, *input, **kwargs)
      475             result = self._slow_forward(*input, **kwargs)
      476         else:
  --&gt; 477             result = self.forward(*input, **kwargs)
      478         for hook in self._forward_hooks.values():
      479             hook_result = hook(self, input, result)
  
  ~/Desktop/Archive/Computing/Projects/Python/ai/neural_network.py in 
  forward(self, x)
       28         for i in range(len(self.layer_shapes)-1):
       29             lin = self.linear_functions[i](y)
  ---&gt; 30             y = self.activation_functions[i](lin)
       31         return y
       32 
  
  TypeError: __init__() takes 1 positional argument but 2 were given
  </code></pre>
  
  <p>I'm sure someone could tell me why this is happening, but could someone please give me a helpful strategy for debugging here? I'm new to pytorch and I doubt this will be the last trouble that I have. So a strategy for investigating these things would be helpful. </p>
  
  <p>I would appreciate any input.</p>