在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 pickleclass 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, multiprocessingclass 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': ...@propertydef 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仓库,改进并提交给我合并。

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

  1. eclipse 新建java无scr_解决eclipse中没有js代码提示的问题

    自学js,发现eclipse中不管js文件.html文件.jsp文件没有都没js代码的提示,对于js代码也不报错,有时候就因为单词敲错却查了很久没查出来,很烦很难受. 在网上找了很多方法,都没有解决, ...

  2. 解决eclipse中jsp没有代码提示问题

    转载自   解决eclipse中jsp没有代码提示问题 刚装的可以开发jsp的eclipse,结果写jsp的时候没有代码提示,在网上找了半天,各种解决办法都不行,后来在stackoverflow上看到 ...

  3. js函数提示 vscode_VSCode + JSDoc 完美实现(almost)JavaScript代码提示

    弱类型脚本语言的代码提示功能一直是开发者一个隐隐的痛点,没有它也不是不能干活,但是经常因为出现拼写错误或不经意的修改导致的变量丢失而耗费无畏的时间在与业务逻辑无关的地方.VSCode的出现大有统一轻量 ...

  4. VSCode + JSDoc 完美实现(almost)JavaScript代码提示

    弱类型脚本语言的代码提示功能一直是开发者一个隐隐的痛点,没有它也不是不能干活,但是经常因为出现拼写错误或不经意的修改导致的变量丢失而耗费无畏的时间在与业务逻辑无关的地方.VSCode的出现大有统一轻量 ...

  5. jetbrains IDEA/pycharm修改代码提示框配色

    修改位置 Editor -> Color Scheme -> General -> Popups and Hint 修改后的效果

  6. 解决eclipse中没有js代码提示的问题

    打开eclipse点击window旁边的Help选项---> Eclipse Marketplace---> 在find中查找AngularJS Eclipse---> ---> ...

  7. Pycharm如何关闭 代码补全 / 代码提示 功能

    Pycharm是一款非常好用的Python的IDE.但就是因为它太好用了,所以对于Python新手来说,反而会导致脱离了代码提示功能,就完全不记得了函数名,大小写等,这对于新手来说是不太利于成长的. ...

  8. android,键盘遮挡了输入框,完美解决android键盘遮挡问题

    登录界面经常会出现输入框被键盘遮挡的情况,这里处理的方式是当软件盘出现的时候在界面的最底部空出键盘的高度.需要代码和xml布局一起修改来实现. 1.xml布局通过ScrollView包裹住内容,内容使 ...

  9. 一文帮你完美解决Json和Xml相互转换

    目录 1.导入用到的jar包 2.xml格式转换为json格式 3.json格式转换为xml格式 我们经常会碰到Json和Xml相互转换,但是转换过程中经常出现各种奇葩问题,比如多节点了或少节点了,或 ...

最新文章

  1. 机器学习工程师 - Udacity 可视化 CNN
  2. Fragment之底层关键操作函数moveToState
  3. Complete Guide to Parameter Tuning in XGBoost (with codes in Python)
  4. 北斗导航 | 惯性导航中的IMU与GPS融合之无人机位姿估计(IMU+GPS:附Matlab源代码)
  5. 10张逻辑思维图,迅速提升你的工作能力
  6. C++Objective-c
  7. 三十岁以前不必在乎的29件事
  8. RTS与CTS的含义
  9. tensorflow-object-detection
  10. python如何调用函数三次_python中如何调用函数
  11. 标题:《东邪西毒》台词全本(国语版) (转)
  12. PIE-engine 教程 ——提取黄河流域/山西省1980—2018年流域降水量并对比分析
  13. Hadoop之HDFS的使用
  14. [解决问题]Android Studio报错:some kotlin libraries attached to this project were compiled with a newer kot
  15. 生兔子问题(递归算法)
  16. 毕业季基于ssm框架的管理系统设计与实现如何写开题报告,怎么完成设计
  17. 怎么在Windows电脑更新 DirectX ?
  18. 从壹开始[ 做贡献 ]之三 || 北京.Net俱乐部活动——DNT精英论坛开幕
  19. 【漫画,Java微服务实践百度网盘
  20. 工作5年,我的互联网工具箱(30个提升办公效率的神器)

热门文章

  1. JavaScript通过API调取信息,AJAX请求,局部刷新
  2. 解决xxx.exe-无法找到入口,无法定位程序输入点......于动态链接库
  3. Python爬虫实例(一)
  4. C# 中文GBK编码转化
  5. 深度学习 笔记(线性神经网络)
  6. 群发邮件怎么发?外贸群发营销邮件哪个平台好?
  7. C++语言程序设计基础
  8. ubuntu 安装微信wechat、截图工具flameshot、拼音输入法、todesk,百度网盘教程,亲测有效且简洁容易操作
  9. Android架构及四大组件
  10. PMP备考指南之第九章:项目资源管理