AssertionError: Gather function not implemented for CPU tensors 错误解决
在pytorch训练深度学习模型时,有时候会报关于cpu gpu的错,如下:
AssertionError: Gather function not implemented for CPU tensors

这个错误的意思就是我们tensor类型的数据类型在传给model进行训练时,没有放在相应的gpu上,无论我们用几块gpu卡进行训练,在传入model前,都应该将tensor类型的数据放在gpu上。
解决方法:
如上所述,在传入model训练之前,将tensor数据放到gpu上,如下:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
segment_ids = segment_ids.to(device)
也就是将你的tensor数据进行如下操作:
你的tensor数据.to(device) # device是你提前设置好的gpu信息
这样就解决了该问题。
本文介绍在使用PyTorch进行深度学习模型训练时遇到的GPU相关错误AssertionError: Gather function not implemented for CPU tensors。此错误提示数据未放置在正确的设备上。文章提供了简单的解决方案:确保所有输入张量都在指定的GPU设备上。
694

被折叠的 条评论
为什么被折叠?



