首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >期望所有的张量都在同一个设备上,但是至少找到了两个设备,cpu和cuda:0!论DataLore

期望所有的张量都在同一个设备上,但是至少找到了两个设备,cpu和cuda:0!论DataLore
EN

Stack Overflow用户
提问于 2022-03-11 18:35:40
回答 1查看 481关注 0票数 0

我正在与IntelliJ DataLore合作训练基本的VGG16 CNN,但是当我尝试使用GPU机器来完成它时,我会得到以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  at block 20, line 1
  at /data/workspace_files/train/trainer/training.py, line 115, in train(self, max_epochs)
  at /data/workspace_files/train/trainer/training.py, line 46, in train_epoch(self, train_loader)
  at /data/workspace_files/train/trainer/training.py, line 94, in forward_to_loss(self, step_images, step_labels)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/modules/module.py, line 1102, in _call_impl(self, *input, **kwargs)
  at /data/workspace_files/models/vgg.py, line 49, in forward(self, x)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/modules/module.py, line 1102, in _call_impl(self, *input, **kwargs)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/modules/container.py, line 141, in forward(self, input)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/modules/module.py, line 1102, in _call_impl(self, *input, **kwargs)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/modules/linear.py, line 103, in forward(self, input)
  at /opt/python/envs/default/lib/python3.8/site-packages/torch/nn/functional.py, line 1848, in linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat1 in method wrapper_addmm)

这是我的代码好让你们检查一下。

代码语言:javascript
复制
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = model.to(device)

在这段代码中,我使用self.device,因为我将设备作为参数传递给类列车

代码语言:javascript
复制
for _, (data, target) in tqdm(enumerate(train_loader, 1)):
            self.optimizer.zero_grad()
            step_images, step_labels = data.to(
                self.device), target.to(self.device)
            step_output, loss = self.forward_to_loss(step_images, step_labels)

我以前没有遇到过这个问题,所以我不知道DataLore上是否遗漏了什么,或者我的代码是否错了。

希望你能帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-11 21:29:08

你能试试这个吗?

代码语言:javascript
复制
step_output, loss = self.forward_to_loss(step_images.to(self.device), step_labels.to(self.device))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71443214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档