title: How can I fix the “TypeError: 'Tensor' object is not callable” error in Pytorch?
body: <p>I am trying to compute a linear function an image's pixels, followed by log softmax (it's for a classification task). I am not sure how to do this without getting errors. Here is the relevant code:</p>
  <pre><code>...
  torch.nn.functional.nll_loss(output, target) # error happens here
  ...
  
  def __init__(self):
      super(NetLin, self).__init__()
      self.in_out = torch.nn.Linear(28, 2)
  
  def forward(self, input):
      out_sum = self.in_out(input)
      output = torch.nn.LogSoftmax(out_sum)
      return output
  </code></pre>
  <p>and the full error message I get is:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;copy.py&quot;, line 98, in &lt;module&gt;
      main()
    File &quot;copy.py&quot;, line 94, in main
      train(args, net, device, train_loader, optimizer, epoch)
    File &quot;copy.py&quot;, line 21, in train
      loss = torch.nn.functional.nll_loss(output, target)
    File &quot;/usr/local/lib/python3.7/site-packages/torch/nn/functional.py&quot;, line 2107, in nll_loss
      dim = input.dim()
  TypeError: 'Tensor' object is not callable
  </code></pre>
  <p>I have tried a few different solutions to this based on other answers online but they just result in different error messages. Clearly I am doing something fundamentally wrong here but I haven't used Pytorch before so I'm not sure what it is. Thank you</p>
  <p><strong>Edit:</strong></p>
  <p>My code is now:</p>
  <pre><code>def train(args, model, device, train_loader, optimizer, epoch):
      if args.net == 'lin':
          model = NetLin()
      model.train()
      loss = nn.NLLLoss()
      for batch_idx, (data, target) in enumerate(train_loader):
          data.requires_grad = True
          data, target = data.to(device), target.to(device)
          optimizer.zero_grad()
          output = loss(model(input), target)
          F.nll_loss(output, target)
          loss.backward()
          optimizer.step()
          if batch_idx % 100 == 0:
              print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                  epoch, batch_idx * len(data), len(train_loader.dataset),
                  100. * batch_idx / len(train_loader), loss.item()))
  
  class NetLin(nn.Module):
      def __init__(self):
          super(NetLin, self).__init__()
          self.in_out = torch.nn.Linear(28 * 28, 2)
  
      def forward(self, input):
          input = input.view(-1, 28 * 28)
          out_sum = self.in_out(input)
          output = torch.nn.LogSoftmax(out_sum, dim=1)
          return output
  </code></pre>
  <p>and my error message is now:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;copy.py&quot;, line 102, in &lt;module&gt;
      main()
    File &quot;copy.py&quot;, line 98, in main
      train(args, net, device, train_loader, optimizer, epoch)
    File &quot;copy.py&quot;, line 24, in train
      output = loss(model(input), target)
    File &quot;/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 550, in __call__
      result = self.forward(*input, **kwargs)
    File &quot;/Users/.../copy.py&quot;, line 15, in forward
      input = input.view(-1, 28 * 28)
  AttributeError: 'builtin_function_or_method' object has no attribute 'view'
  </code></pre>
  <p>As you can kind of see the <code>data</code> and <code>target</code> are read in from a file (they are from KMNIST actually) so I can't control their format exactly, but I do know the image sizes are all [1,28,28], i.e. a 28*28 greyscale image. Also the batch size is 64 in case that matters.</p>