pytorch1.60 torch.nn在pycharm中無(wú)法自動(dòng)智能提示的解決
問(wèn)題描述
安裝了pytorch最新版本1.6之后,在pycharm中編輯python代碼時(shí),輸入torch.nn.看不到提示了,比如torch.nn.MSELoss()。
而在1.4及以前的版本中,直接輸入torch.nn.就會(huì)自動(dòng)提示出很多torch.nn.modules中的API。
該問(wèn)題的討論在前幾年有過(guò)不少,但都是基于老版本,經(jīng)過(guò)嘗試,對(duì)于1.6版本是無(wú)效的。
原因分析
pycharm的自動(dòng)提示是根據(jù)第三方包的每個(gè)文件夾下的__init__.pyi文件來(lái)顯示的,只有__init__.pyi中import了的API才會(huì)被pycharm自動(dòng)提示。
首先對(duì)pytorch.nn模塊要知道,問(wèn)題描述中提到的MSELoss等眾多函數(shù),真實(shí)位置是torch.nn.modules.MSELoss(),你直接調(diào)用這個(gè)真實(shí)位置是可以自動(dòng)提示的。
但是1.4及以前的版本中大家都熟悉了直接用nn.MSELoss()這樣調(diào)用,如何讓1.6版本也能像歷史版本一樣提示呢?
基于此,我對(duì)比了1.6和1.4的區(qū)別。
在torch 1.6版本包存放位置下,torch/nn/下是有__init__.pyi的,里面有一行from .modules import *,說(shuō)明nn模塊是可以直接調(diào)用子模塊modules中的API的,所以直接調(diào)用nn.MSELoss()不會(huì)報(bào)錯(cuò),只是不會(huì)自動(dòng)提示。
然后在進(jìn)入torch/nn/modules/發(fā)現(xiàn),1.6版本中缺少__init__.pyi文件,所以在pycharm輸入nn.的時(shí)候并不會(huì)提示子模塊modules中的API。
解決方案
從pytorch 1.4版本中復(fù)制一份__init__.pyi文件到1.6版本的依賴包的相同目錄下。
具體位置是:
{你的第三方包存放位置}/Lib/site-packages/torch/nn/modules/__init__.pyi
然后就可以在pycharm中愉快使用nn.自動(dòng)提示了。其他模塊不自動(dòng)提示的,解決方法類同。
補(bǔ)充
關(guān)于解決方案中第三方包存放位置不知道的,可以在pycharm左側(cè)項(xiàng)目目錄結(jié)構(gòu)中看到一項(xiàng)External Libraries,點(diǎn)開(kāi)它,你就能直接找到Lib/site-packages/torch/nn/modules/,從而不必去資源管理器找。
附件 __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
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch MSELoss計(jì)算平均的實(shí)現(xiàn)方法
這篇文章主要介紹了pytorch MSELoss計(jì)算平均的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05
python實(shí)現(xiàn)接口并發(fā)測(cè)試腳本
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)接口并發(fā)測(cè)試腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python關(guān)鍵字and和or用法實(shí)例
這篇文章主要介紹了python關(guān)鍵字and和or用法實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用詳解
一個(gè)矩陣我們想讓它通過(guò)編程,實(shí)現(xiàn)各種花樣的變化怎么辦呢?下面這篇文章主要給大家介紹了關(guān)于Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
教你使用Sublime text3搭建Python開(kāi)發(fā)環(huán)境及常用插件安裝另分享Sublime text3最新激活注冊(cè)碼
這篇文章主要介紹了使用Sublime text 3搭建Python開(kāi)發(fā)環(huán)境及常用插件安裝,并提供了最新Sublime text 3激活注冊(cè)碼需要的朋友可以參考下2020-11-11
Pygame游戲開(kāi)發(fā)之太空射擊實(shí)戰(zhàn)子彈與碰撞處理篇
相信大多數(shù)8090后都玩過(guò)太空射擊游戲,在過(guò)去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來(lái)自己動(dòng)手實(shí)現(xiàn)它,在編寫(xiě)學(xué)習(xí)中回顧過(guò)往展望未來(lái),下面開(kāi)始講解子彈與碰撞處理,在本課中,我們將添加玩家與敵人之間的碰撞,以及添加供玩家射擊的子彈2022-08-08

