使用DataLoader报错AttributeError: 'int' object has no attribute 'numel'
在跑这段代码的时候报错
concat_dataset = ConcatDataset([train_dataset, new_set]) train_data = DataLoader(concat_dataset, batch_size=batch_size, shuffle=True, num_workers=2, pin_memory=True) for data, y in train_data: print(data, y)
Original Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
return self.collate_fn(data)
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/collate.py", line 84, in default_collate
return [default_collate(samples) for samples in transposed]
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/collate.py", line 84, in <listcomp>
return [default_collate(samples) for samples in transposed]
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/collate.py", line 53, in default_collate
numel = sum([x.numel() for x in batch])
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/collate.py", line 53, in <listcomp>
numel = sum([x.numel() for x in batch])
AttributeError: 'int' object has no attribute 'numel'
使用了ConcatDataset合并两个dataset后,进行数据加载的时候报错了,主要原因是合并的两个dataset返回的数据类型不一致,遍历两个dataset可以看到一个返回的是'int',一个返回的是'tensor'。
将两个dataset返回的结果改成一致的,就可以解决问题了。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步