我正在与IntelliJ DataLore合作训练基本的VGG16 CNN,但是当我尝试使用GPU机器来完成它时,我会得到以下错误:
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)这是我的代码好让你们检查一下。
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = model.to(device)在这段代码中,我使用self.device,因为我将设备作为参数传递给类列车
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上是否遗漏了什么,或者我的代码是否错了。
希望你能帮我!
发布于 2022-03-11 21:29:08
你能试试这个吗?
step_output, loss = self.forward_to_loss(step_images.to(self.device), step_labels.to(self.device))https://stackoverflow.com/questions/71443214
复制相似问题