首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RuntimeError:预期所有张量都在同一设备上,但是使用Google环境至少发现了两个设备,cuda:0和cpu。

RuntimeError:预期所有张量都在同一设备上,但是使用Google环境至少发现了两个设备,cuda:0和cpu。
EN

Stack Overflow用户
提问于 2022-12-01 20:25:33
回答 1查看 23关注 0票数 0

我现在正在使用笔记本电脑,使用Google环境。当我执行包含以下代码的块时

代码语言:javascript
运行
复制
with torch.no_grad():
    generated_images = vae.decode(generated_image_codes)

我得到了以下错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
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)

我试着评论之前的所有问题,并在类似的问题中寻找解决方案,但没有什么能帮助解决这个问题。有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-12-02 04:56:16

如果您按顺序执行代码块,那么generated_image_codesvae都应该位于同一个设备上,即CPU。

代码语言:javascript
运行
复制
generated_image_codes = torch.cat(generated_image_codes, axis=0).cpu()

代码语言:javascript
运行
复制
torch.cuda.empty_cache()
vae.cpu()

要再次检查,您可以运行

代码语言:javascript
运行
复制
print(generated_image_codes.device)
print(next(vae.parameters()).device)

预期产出为

代码语言:javascript
运行
复制
torch.device("cpu")
torch.device("cpu")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74647808

复制
相关文章

相似问题

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