Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Pytorch encounters wrong solution

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Pytorch encountered the wrong solution, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Pytorch running error: RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR

Solution:

Add to the code:

Torch.cuda.set_device (0) training RNN Network loss appears Nan solution

(1)。 The cause of gradient explosion can be solved by gradient adjudication.

GRAD_CLIP = 5loss.backward () torch.nn.utils.clip_grad_norm_ (model.parameters (), GRAD_CLIP) optimizer.step ()

(2) need to be used in testModel and evaluate

With torch.no_grad ():

(3) lower the learning rate.

RuntimeError: Expected object of device type cuda but got device type cpu for argument # 1 'self' in call to _ th_addmm

Cuda () conversion is required in three locations in the code:

Whether the model is put on CUDA model = model.to (device)

Whether the input data is put on CUDA data = data.to (device)

Whether the newly created tensor inside the model is put on CUDA p = torch.tensor ([1]) .to (device)

Model = model.to (device) in the first article is only valid for functions instantiated in _ _ init__ () in model, and model will not be placed in cuda if instantiated in forward and used directly.

Here is an error code:

Import torchimport torch.nn as nndata = torch.rand (1,10). Cuda () class TestMoule (nn.Module): def _ _ init__ (self): super (TestMoule, self). _ init__ () # self.linear = torch.nn.Linear (10,2) def forward (self, x): # return self.linear (x) return torch.nn.Linear (10) 2) (x) model = TestMoule () model = model.cuda () print (model (data)) RuntimeError: CUDA error: an illegal memory access was encountered

One of the above problems occurs when functions under some nn modules pass data of type gpu, as shown in the error code:

Import torchdata = torch.randn (1,10). Cuda () layernorm = torch.nn.LayerNorm (10) # layernorm = torch.nn.LayerNorm (10). Cuda () re_data = layernorm (data) print (re_data) RuntimeError: CUDA error: device-side assert triggered

The category target of the classification does not correspond to the value of the model output softmax one by one, such as the three classification problems:

Targets is a value of 1-3, but softmax calculates a value of 0-2, thus prompting the above error.

Df = pd.read_csv ('data/reviews.csv') def to_sentiment (score): score = int (score) if score

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report