pytorch 在我的音频分类脚本中,类型为“ESC50Data”的对象没有len()

vfhzx4xs  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(118)

所以我遇到了类ESC50Data没有任何长度的错误。
第一个
这就是我得到错误的地方。使用Jypter笔记本作为旁注。

TypeError                                 Traceback (most recent call last)
Input In [47], in <cell line: 1>()
----> 1 train_loader = DataLoader(train_data, batch_size=16, shuffle=True)
      2 valid_loader = DataLoader(valid_data, batch_size=16, shuffle=True)

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/utils/data/dataloader.py:353, in DataLoader.__init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context, generator, prefetch_factor, persistent_workers, pin_memory_device)
    351 else:  # map-style
    352     if shuffle:
--> 353         sampler = RandomSampler(dataset, generator=generator)  # type: ignore[arg-type]
    354     else:
    355         sampler = SequentialSampler(dataset)  # type: ignore[arg-type]

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/utils/data/sampler.py:106, in RandomSampler.__init__(self, data_source, replacement, num_samples, generator)
    102 if not isinstance(self.replacement, bool):
    103     raise TypeError("replacement should be a boolean value, but got "
    104                     "replacement={}".format(self.replacement))
--> 106 if not isinstance(self.num_samples, int) or self.num_samples <= 0:
    107     raise ValueError("num_samples should be a positive integer "
    108                      "value, but got num_samples={}".format(self.num_samples))

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/utils/data/sampler.py:114, in RandomSampler.num_samples(self)
    110 @property
    111 def num_samples(self) -> int:
    112     # dataset size might change at runtime
    113     if self._num_samples is None:
--> 114         return len(self.data_source)
    115     return self._num_samples

TypeError: object of type 'ESC50Data' has no len()

对于可能发生的情况有什么想法吗?我创建了类ESC50Data,然后给它一个名为Dataset的子类,它将继承ESC50Data的属性。我还将数据与train和有效数据一起加载到pytorch中。

bvn4nwqk

bvn4nwqk1#

检查class ESC50Data代码中__len__(self)__getitem__(self, idx)方法的缩进。现在,这些方法似乎是在__init__方法 * 内部 * 定义的,而不是在类本身下定义的。
例如,参见this answer

相关问题