zoukankan      html  css  js  c++  java
  • 使用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返回的结果改成一致的,就可以解决问题了。

  • 相关阅读:
    Programming Contest Ranking(题解)
    Alphabet Cookies
    hpu 1267 Cafeteria (01背包)
    Triangles 正多边形分割锐角三角形
    ACdream 1067:Triangles
    hdu 1253 胜利大逃亡 (代码详解)解题报告
    最短路
    POJ- 1511 Invitation Cards
    E
    HDU
  • 原文地址:https://www.cnblogs.com/liwill/p/14944430.html
Copyright © 2011-2022 走看看