Special PyTorch validation questions in PyCharm

Has anyone been able to work around PyTorch validation issues in PyCharm? Previous posts for non-PyTorch issues suggest an update to PyCharm, but I'm currently on the latest version. One option is, of course, to disable some of the checks entirely, but I would rather avoid that.

Example: torch.LongTensor(x)

gives me "Unexpected argument ..." whereas both call signatures (with and without x

) are supported.

+3


source to share


1 answer


I believe because it torch.LongTensor

doesn't have a method __init__

to find pycharm.

According to this source I found thanks to this SO post :

Use __ new __ when you need to control the creation of a new instance. Use __ init __ when you need to control the initialization of a new instance.

__ new __ is the first step of instantiation. It's called first, and is responsible for returning a new instance of your class. In contrast, __ init __ returns nothing; it is only responsible for initializing the instance after it has been created.

In general, you don't need to override __ new __ unless you are subclassing an immutable type like str, int, unicode, or tuple.

Since they Tensor

are types, it only makes sense to define new

no init

.

You can experiment with this by testing the following classes:

torch.LongTensor(1)  # Unexpected arguments

      



Throws a warning, not the following.

class MyLongTensor(torch.LongTensor):
    def __init__(self, *args, **kwargs):
        pass

MyLongTensor(1)  # No error

      

To confirm that absence __init__

is the culprit for the attempt:

class Example(object):
    pass

Example(0)  # Unexpected arguments

      

To find out for yourself, use pycharm for Ctrl+click

on LongTensor

, then _TensorBase

and look at specific methods.

+1


source







All Articles