torch1.6.0的版本在pycharm,nn.
后没有自动补全的相关提示网上都说对于1.6.0版本的pytorch再pycharm里是没有办法自动补全的,因此这算是一个暂时恒定的bug。
pycharm的自动提示是根据第三方包的每个文件夹下的__init__.pyi
文件来显示的,只有__init__.pyi
中import了的API才会被pycharm自动提示。
需要生成__init__.pyi,
文件内容如下:
from .module import Module as Module
from .activation import CELU as CELU, ELU as ELU, GLU as GLU, GELU as GELU, Hardshrink as Hardshrink, \
Hardtanh as Hardtanh, LeakyReLU as LeakyReLU, LogSigmoid as LogSigmoid, LogSoftmax as LogSoftmax, PReLU as PReLU, \
RReLU as RReLU, ReLU as ReLU, ReLU6 as ReLU6, SELU as SELU, Sigmoid as Sigmoid, Softmax as Softmax, \
Softmax2d as Softmax2d, Softmin as Softmin, Softplus as Softplus, Softshrink as Softshrink, Softsign as Softsign, \
Tanh as Tanh, Tanhshrink as Tanhshrink, Threshold as Threshold
from .adaptive import AdaptiveLogSoftmaxWithLoss as AdaptiveLogSoftmaxWithLoss
from .batchnorm import BatchNorm1d as BatchNorm1d, BatchNorm2d as BatchNorm2d, BatchNorm3d as BatchNorm3d, \
SyncBatchNorm as SyncBatchNorm
from .container import Container as Container, ModuleDict as ModuleDict, ModuleList as ModuleList, \
ParameterDict as ParameterDict, ParameterList as ParameterList, Sequential as Sequential
from .conv import Conv1d as Conv1d, Conv2d as Conv2d, Conv3d as Conv3d, ConvTranspose1d as ConvTranspose1d, \
ConvTranspose2d as ConvTranspose2d, ConvTranspose3d as ConvTranspose3d
from .distance import CosineSimilarity as CosineSimilarity, PairwiseDistance as PairwiseDistance
from .dropout import AlphaDropout as AlphaDropout, Dropout as Dropout, Dropout2d as Dropout2d, Dropout3d as Dropout3d, \
FeatureAlphaDropout as FeatureAlphaDropout
from .fold import Fold as Fold, Unfold as Unfold
from .instancenorm import InstanceNorm1d as InstanceNorm1d, InstanceNorm2d as InstanceNorm2d, \
InstanceNorm3d as InstanceNorm3d
from .linear import Bilinear as Bilinear, Identity as Identity, Linear as Linear
from .loss import BCELoss as BCELoss, BCEWithLogitsLoss as BCEWithLogitsLoss, CTCLoss as CTCLoss, \
CosineEmbeddingLoss as CosineEmbeddingLoss, CrossEntropyLoss as CrossEntropyLoss, \
HingeEmbeddingLoss as HingeEmbeddingLoss, KLDivLoss as KLDivLoss, L1Loss as L1Loss, MSELoss as MSELoss, \
MarginRankingLoss as MarginRankingLoss, MultiLabelMarginLoss as MultiLabelMarginLoss, \
MultiLabelSoftMarginLoss as MultiLabelSoftMarginLoss, MultiMarginLoss as MultiMarginLoss, NLLLoss as NLLLoss, \
NLLLoss2d as NLLLoss2d, PoissonNLLLoss as PoissonNLLLoss, SmoothL1Loss as SmoothL1Loss, \
SoftMarginLoss as SoftMarginLoss, TripletMarginLoss as TripletMarginLoss
from .module import Module as Module
from .normalization import CrossMapLRN2d as CrossMapLRN2d, GroupNorm as GroupNorm, LayerNorm as LayerNorm, \
LocalResponseNorm as LocalResponseNorm
from .padding import ConstantPad1d as ConstantPad1d, ConstantPad2d as ConstantPad2d, ConstantPad3d as ConstantPad3d, \
ReflectionPad1d as ReflectionPad1d, ReflectionPad2d as ReflectionPad2d, ReplicationPad1d as ReplicationPad1d, \
ReplicationPad2d as ReplicationPad2d, ReplicationPad3d as ReplicationPad3d, ZeroPad2d as ZeroPad2d
from .pixelshuffle import PixelShuffle as PixelShuffle
from .pooling import AdaptiveAvgPool1d as AdaptiveAvgPool1d, AdaptiveAvgPool2d as AdaptiveAvgPool2d, \
AdaptiveAvgPool3d as AdaptiveAvgPool3d, AdaptiveMaxPool1d as AdaptiveMaxPool1d, \
AdaptiveMaxPool2d as AdaptiveMaxPool2d, AdaptiveMaxPool3d as AdaptiveMaxPool3d, AvgPool1d as AvgPool1d, \
AvgPool2d as AvgPool2d, AvgPool3d as AvgPool3d, FractionalMaxPool2d as FractionalMaxPool2d, \
FractionalMaxPool3d as FractionalMaxPool3d, LPPool1d as LPPool1d, LPPool2d as LPPool2d, MaxPool1d as MaxPool1d, \
MaxPool2d as MaxPool2d, MaxPool3d as MaxPool3d, MaxUnpool1d as MaxUnpool1d, MaxUnpool2d as MaxUnpool2d, \
MaxUnpool3d as MaxUnpool3d
from .rnn import GRU as GRU, GRUCell as GRUCell, LSTM as LSTM, LSTMCell as LSTMCell, RNN as RNN, RNNBase as RNNBase, \
RNNCell as RNNCell, RNNCellBase as RNNCellBase
from .sparse import Embedding as Embedding, EmbeddingBag as EmbeddingBag
from .upsampling import Upsample as Upsample, UpsamplingBilinear2d as UpsamplingBilinear2d, \
UpsamplingNearest2d as UpsamplingNearest2d
若不会生成.pyi文件的可以在以下连接下载:
链接:https://pan.baidu.com/s/1BGQync-daqTA3eW-iItKMA
提取码:xqva
要注意,.pyi
文件是PEP484
提案规定的一种用于 Python 代码类型提示(Type Hints)的文件
这个是和.py文件不一样的。但是为了大家能在pycharm里就生成.pyi文件,给出以下方法
1、创建一个空的__init__.py文件
2、在pycharm工程下的terminal处(假设此时工程处于某种环境下),在Terminal出下载mypy包:
pip install mypy
待下载完成后,再输入
stubgen __init__.py
就会在工程文件栏看到生成的__init__.py文件
3、再将以上提到的__init__.pyi文件里应该有的的代码复制到__init__.pyi中
4、将该文件复制到拥有nn模块的文件下:D:\Anaconda\envs\torch\Lib\site-packages\torch\nn(就是需要环境下的torch包中的nn模块)
5、现在就可以有代码提示了
参考:Python自动生成代码提示.pyi_XerCis的博客-CSDN博客_python代码自动提示
我的pytorch在pycharm不能自动补全代码? - 知乎