问题现场:
在训练语言模型时,将经过dataloader转换后的数据,进行模型训练,将整个batch转换到gpu上时,报错,意思是tuple数据结构,没有cuda属性,就是不能直接往gpu上传送。
'tuple' object has no attribute 'cuda'
解决:
1.理解batch的数据结构:tuple。元组中的每一个元素代表什么。我这里代表了(训练数据,训练标签)
2.拆分元组后,分别送往gpu。
for step, batch in enumerate(train_dataloader):
batch[0] = torch.LongTensor(batch[0]).to(config.device)
batch[1] = torch.LongTensor(batch[1]).to(config.device)
...
参考:
1.https://stackoverflow.com/questions/62147378/tuple-object-has-no-attribute-cuda