votes up 5

var is of incorrect size

Package:
torch
github stars 50580
Exception Class:
ValueError

Raise code

        # This is also a homoscedastic case.
        # e.g. input.size = (10, 2, 3), var.size = (10, 2, 1)
        elif input.size()[:-1] == var.size()[:-1] and var.size(-1) == 1:  # Heteroscedastic case
            pass

        # If none of the above pass, then the size of var is incorrect.
        else:
            raise ValueError("var is of incorrect size")

    # Check validity of reduction mode
    if reduction != 'none' and reduction != 'mean' and reduction != 'sum':
        raise ValueError(reduction + " is not valid")

    # Entries of var must be non-negative
    if torch.any(var < 0):
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

Summary:

This exception is thrown when calling the gaussian_nll_loss function. This function takes in 3 parameters: input, target, and vars. Each of these parameters must be torch tensors. An assertion is performed to ensure that the size of input and var is the same. If they are different sizes you will get the exception, so to avoid it, ensure input and var are of the same shape and size. Assuming the two tensors have the same number of data points, you can use the reshape function to make them match.

Code to Reproduce the Error (Wrong):

from torch.nn.functional import gaussian_nll_loss

inp = torch.arange(24).reshape(12,2)
target = torch.arange(24).reshape(12,2)
var = torch.arange(24).reshape(8,3)
gaussian_nll_loss(inp, target, var)

Error Code:

ValueError                                Traceback (most recent call last)
<ipython-input-96-7f2f88eb5eed> in <module>()
      5 var = torch.arange(24).reshape(8,3)
      6 
----> 7 gaussian_nll_loss(inp, target, var)

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in gaussian_nll_loss(input, target, var, full, eps, reduction)
   2650         # If none of the above pass, then the size of var is incorrect.
   2651         else:
-> 2652             raise ValueError("var is of incorrect size")
   2653 
   2654     # Check validity of reduction mode

ValueError: var is of incorrect size

Working Version (Right):

from torch.nn.functional import gaussian_nll_loss

inp = torch.arange(24).reshape(12,2)
target = torch.arange(24).reshape(12,2)
var = torch.arange(24).reshape(8,3)
gaussian_nll_loss(inp, target, var)
Jul 15, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix