完美解决Pytorch在Pycharm没有代码提示的问题

在Pytorch的旧版本中,没有__init__.pyi这个文件(Pycharm就从__init__.pyi读取函数的声明以及参数类型),因此在Pycharm中torch.sum、torch.abs等函数就没有代码提示。没有代码提示很不方便,经过一番折腾,完美地解决了Pytorch在Pycharm中没有代码提示的问题。

  • 把Pytorch的版本升级到1.0.1.post2。

torch.sum、torch.abs等函数应该是有提示了,但是torch.nn、torch.optim等却没有提示了。解决方案如下:

  • 升级Pycharm到版本2019.1.1。

  • site-packages/torch/__init__.pyi中增加两行代码。
    旧的__init__.pyi

    from typing import Tuple, Optional, Union
    import pickle
    
    class Tensor:
        def abs(self, *, out: Optional['Tensor']=None) -> 'Tensor': ...
        ...
    

    新的__init__.pyi

    from typing import Tuple, Optional, Union
    import pickle
    from torch import nn, cuda, ops, functional, optim, autograd, onnx, utils
    from torch import contrib, distributions, for_onnx, jit, multiprocessing
    
    class Tensor:
        def abs(self, *, out: Optional['Tensor']=None) -> 'Tensor': ...
    	...
    

又遇到了“class Tensor does not define __sub__, so the ‘-’ cannot be used in its instances”这样的错误,翻译过来就是Tensor类没有定义__sub__, ‘-’ 就不能用在实例之间。同样的还有加减乘除大于小于等符号都有类似的提示。于是继续修改__init__.pyi
旧的Tensor定义:

class Tensor:
    def abs(self, *, out: Optional['Tensor']=None) -> 'Tensor': ...
   	...

新的Tensor定义:

class Tensor:
    def __add__(self, other: Union['Tensor', 'int', 'float']): ...
    def __radd__(self, other: Union['Tensor', 'int', 'float']): ...
    def __iadd__(self, other: Union['Tensor', 'int', 'float']): ...

    def __sub__(self, other: Union['Tensor', 'int', 'float']): ...
    def __rsub__(self, other: Union['Tensor', 'int', 'float']): ...
    def __isub__(self, other: Union['Tensor', 'int', 'float']): ...

    def __mul__(self, other: Union['Tensor', 'int', 'float']): ...
    def __rmul__(self, other: Union['Tensor', 'int', 'float']): ...
    def __imul__(self, other: Union['Tensor', 'int', 'float']): ...

    def __truediv__(self, other: Union['Tensor', 'int', 'float']): ...
    def __rtruediv__(self, other: Union['Tensor', 'int', 'float']): ...
    def __itruediv__(self, other: Union['Tensor', 'int', 'float']): ...

    def __pow__(self, power: 'int', modulo=None) -> 'Tensor': ...
    def __rpow__(self, power: 'int', modulo=None) -> 'Tensor': ...
    def __ipow__(self, power: 'int', modulo=None) -> 'Tensor': ...

    def __ge__(self, other: Union['Tensor', 'int', 'float']): ...
    def __gt__(self, other: Union['Tensor', 'int', 'float']): ...
    def __le__(self, other: Union['Tensor', 'int', 'float']): ...
    def __lt__(self, other: Union['Tensor', 'int', 'float']): ...
    def __ne__(self, other: Union['Tensor', 'int', 'float']): ...
    def __eq__(self, other: Union['Tensor', 'int', 'float']): ...
    def __neg__(self, other: Union['Tensor', 'int', 'float']): ...

    def __and__(self, other: Union['Tensor', 'int', 'float']): ...
    def __rand__(self, other: Union['Tensor', 'int', 'float']): ...
    def __iand__(self, other: Union['Tensor', 'int', 'float']): ...

    def __or__(self, other: Union['Tensor', 'int', 'float']): ...
    def __ror__(self, other: Union['Tensor', 'int', 'float']): ...
    def __ior__(self, other: Union['Tensor', 'int', 'float']): ...

    def __xor__(self, other: Union['Tensor', 'int', 'float']): ...
    def __rxor__(self, other: Union['Tensor', 'int', 'float']): ...
    def __ixor__(self, other: Union['Tensor', 'int', 'float']): ...

    def __getitem__(self, item) -> 'Tensor': ...

    def __setitem__(self, key, value) -> 'Tensor': ...

    def __abs__(self): ...

    def __ceil__(self): ...

    def __floor__(self): ...

    def __bool__(self) -> 'Tensor': ...

    def size(self) -> 'torch.Size': ...

    def cpu(self) -> 'Tensor': ...

    def cuda(self, device:Optional['torch.device']=None, non_blocking: Optional['bool']=False) -> 'Tensor': ...

    @property
    def device(self): ...

    def numpy(self) -> 'numpy.ndarray': ...

    def dim(self) -> 'int': ...

    def ndimension(self)  -> 'int': ...

    def double(self) -> 'Tensor': ...

    def float(self) -> 'Tensor': ...

    def half(self) -> 'Tensor': ...

    def int(self) -> 'Tensor': ...

    def long(self) -> 'Tensor': ...

    def short(self) -> 'Tensor': ...

    def abs(self, *, out: Optional['Tensor']=None) -> 'Tensor': ...
    ...

就把加减乘除还有Tensor.size()函数都给它添加上了。说了这么多,其实就是缺啥定义就在__init__.pyi补啥定义。

我修改的__init__.pyi可从项目PythonResources下载(本博客不在更新)。下载完之后,替换掉site-packages/torch/中的__init__.pyi就行。

如果还有没有代码提示的地方,欢迎fork仓库,改进并提交给我合并。

发布了88 篇原创文章 · 获赞 132 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/DumpDoctorWang/article/details/89287956
今日推荐