AssertionError: Gather function not implemented for CPU tensors 错误解决

AssertionError: Gather function not implemented for CPU tensors 错误解决

在pytorch训练深度学习模型时,有时候会报关于cpu gpu的错,如下:

AssertionError: Gather function not implemented for CPU tensors

AssertionError: Gather function not implemented for CPU tensors 错误解决_第1张图片

这个错误的意思就是我们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信息

这样就解决了该问题。

你可能感兴趣的:(Python模型错误解决,深度学习,人工智能,python,pytorch)