4.pytorch1.60 torch.nn在pycharm中无法自动智能提示

pytorch1.60 torch.nn在pycharm中无法自动智能提示

  • 一、解决的方法有两个,第一个方法来源于一个博客,第二个方法是根据这个博客产生的启发(目前没有问题)
  • 二、原因分析

一、解决的方法有两个,第一个方法来源于一个博客,第二个方法是根据这个博客产生的启发(目前没有问题)

  1. 方法二:导包时,有原来的import torch.nn as nn改为import torch.nn.modules as nn,这样就可以愉快的使用nn.XXX后面的内容了,重新过上有提示的好日子。

  2. 方法一:从pytorch 1.4版本中复制一份__init__.pyi文件到1.6版本的依赖包的相同目录下。具体位置是{你的第三方包存放位置}/Lib/site-packages/torch/nn/modules/init.pyi
    然后就可以在pycharm中愉快使用nn.自动提示了。其他模块不自动提示的,解决方法类同。

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


二、原因分析

下面这段解释引用自其它博客:

这里根据他人博客借鉴的内容,进行如下总结:
pycharm的自动提示是根据第三方包的每个文件夹下的__init__.pyi文件来显示的,只有__init__.pyi中import了的API才会被pycharm自动提示。

首先对pytorch.nn模块要知道,问题描述中提到的MSELoss等众多函数,真实位置是torch.nn.modules.MSELoss(),你直接调用这个真实位置是可以自动提示的。但是1.4及以前的版本中大家都熟悉了直接用nn.MSELoss()这样调用,如何让1.6版本也能像历史版本一样提示呢?

在torch 1.6版本包存放位置下,torch/nn/下是有__init__.pyi的,里面有一行from .modules import *,说明nn模块是可以直接调用子模块modules中的API的,所以直接调用nn.MSELoss()不会报错,只是不会自动提示。
然后在进入torch/nn/modules/发现,1.6版本中缺少__init__.pyi文件,所以在pycharm输入nn.的时候并不会提示子模块modules中的API。

原博客地址:https://blog.csdn.net/winter2121/article/details/108799776?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

你可能感兴趣的:(pytorch,pytorch)