TypeError although same shape: if not (target.size() == input.size()): 'int' object is not callable

This is the error message I get. In the first line, I output the shapes of predicted and target. From my understanding, the error arises from those shapes not being the same but here they clearly are.

torch.Size([6890, 3]) torch.Size([6890, 3])

Traceback (most recent call last):
  File "train.py", line 251, in <module>
    main()
  File "train.py", line 230, in main
    train(net, training_dataset, targets, device, criterion, optimizer, epoch, args.epochs)
  File "train.py", line 101, in train
    loss = criterion(predicted, target.detach().cpu().numpy())
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 443, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2244, in mse_loss
    if not (target.size() == input.size()):
TypeError: 'int' object is not callable

I hope all the relevant context information is given. Thanks for any suggestions!

You are passing the target as a numpy array, which works differently for size() (it returns the number of elements as an int, thus raising this error).
Remove the numpy() call and the code should work fine.

2 Likes