在跑这段代码的时候报错
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返回的结果改成一致的,就可以解决问题了。