在运行代码时遇到如下问题
D:/main.py
Traceback (most recent call last):
File "E:/main.py", line 184, in <module>
train_dataloader = DataLoader(
File "D:\pythonproject3.8\lib\site-packages\torch\utils\data\dataloader.py", line 353, in __init__
sampler = RandomSampler(dataset, generator=generator) # type: ignore[arg-type]
File "D:\pythonproject3.8\lib\site-packages\torch\utils\data\sampler.py", line 106, in __init__
if not isinstance(self.num_samples, int) or self.num_samples <= 0:
File "D:\pythonproject3.8\lib\site-packages\torch\utils\data\sampler.py", line 114, in num_samples
return len(self.data_source)
TypeError: object of type 'module' has no len()
报错部分代码如下:
data_source: Sized
replacement: bool
def __init__(self, data_source: Sized, replacement: bool = False,
num_samples: Optional[int] = None, generator=None) -> None:
self.data_source = data_source
self.replacement = replacement
self._num_samples = num_samples
self.generator = generator
if not isinstance(self.replacement, bool):
raise TypeError("replacement should be a boolean value, but got "
"replacement={}".format(self.replacement))
if not isinstance(self.num_samples, int) or self.num_samples <= 0:
raise ValueError("num_samples should be a positive integer "
"value, but got num_samples={}".format(self.num_samples))
@property
def num_samples(self) -> int:
# dataset size might change at runtime
if self._num_samples is None:
return len(self.data_source) #此处报错
return self._num_samples
def __iter__(self) -> Iterator[int]:
n = len(self.data_source)
if self.generator is None:
seed = int(torch.empty((), dtype=torch.int64).random_().item())
generator = torch.Generator()
generator.manual_seed(seed)
else:
generator = self.generator
if self.replacement:
for _ in range(self.num_samples // 32):
yield from torch.randint(high=n, size=(32,), dtype=torch.int64, generator=generator).tolist()
yield from torch.randint(high=n, size=(self.num_samples % 32,), dtype=torch.int64, generator=generator).tolist()
else:
for _ in range(self.num_samples // n):
yield from torch.randperm(n, generator=generator).tolist()
yield from torch.randperm(n, generator=generator).tolist()[:self.num_samples % n]
def __len__(self) -> int:
return self.num_samples
求问有什么解决方法吗?