RuntimeError:期望所有张量都在同一设备上,但发现至少两个设备,cuda:0和cpu,使用Google Col



我现在正在使用这个笔记本电脑,使用谷歌Colab GPU环境。当我执行包含以下代码

的块时
with torch.no_grad():
generated_images = vae.decode(generated_image_codes)

我得到了以下错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-36-56287126db2f> in <module>
1 with torch.no_grad():
----> 2     generated_images = vae.decode(generated_image_codes)
3 frames
/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
2197         # remove once script supports set_grad_enabled
2198         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 2199     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
2200 
2201 
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper__index_select)

我试着注释前面所有的块,并在类似的问题中寻找解决方案,但没有任何帮助解决问题。有人能帮我一下吗?

如果按顺序执行代码块,则generated_image_codesvae应该在同一设备上,即CPU上。

generated_image_codes = torch.cat(generated_image_codes, axis=0).cpu()

torch.cuda.empty_cache()
vae.cpu()

要再次检查,可以运行

print(generated_image_codes.device)
print(next(vae.parameters()).device)

期望输出

torch.device("cpu")
torch.device("cpu")

相关内容

最新更新