How can I fix the “TypeError: 'Tensor' object is not callable” error in Pytorch?
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:
...
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

and the full error message I get is:
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

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
Edit:
My code is now:
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

and my error message is now:
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'

As you can kind of see the data and target 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.

Did you remember to set your model to training mode in your train loop with model.train()? Also, nll_loss takes in 2 tensors, but the first entry (the input tensor) needs to have requires_grad=True before it goes through the model, which is also why you need to set model.train() before training.
So you would have something like this:
model = NetLin()
model.train()
loss = nn.NLLLoss()
input = torch.randn(7, 4, requires_grad=True) # your input image (tensor)
target = torch.tensor([1, 0]) # image label for image belonging to first class
output = loss(model(input), target)

I am also a bit concerned about your self.in_out = torch.nn.Linear(28, 2). This says that your linear layer is expecting 28 features, implying that your input images are either 7x4, 14x2 or 28x1, which doesn't seem right in my opinion? Aren't you using images of size 28x28 (very typical size in this context)? In which case, you would have your linear layer modified as self.in_out = torch.nn.Linear(28*28, 2), and your forward pass will have to be modified as follows:
def forward(self, input):
    input = input.view(-1, 28*28)
    out_sum = self.in_out(input)
    output = torch.nn.LogSoftmax(out_sum)
    return output

