1 回答

TA貢獻1785條經驗 獲得超4個贊
您應該能夠很好地循環訪問 Subset,因為它實現了從源代碼中看到的方法:__getitem__
class Subset(Dataset):
r"""
Subset of a dataset at specified indices.
Arguments:
dataset (Dataset): The whole Dataset
indices (sequence): Indices in the whole set selected for subset
"""
def __init__(self, dataset, indices):
self.dataset = dataset
self.indices = indices
def __getitem__(self, idx):
return self.dataset[self.indices[idx]]
def __len__(self):
return len(self.indices)
因此,以下方法應該有效:
for image, label in train_dataset:
print(image, label)
或者,您可以從子集創建數據加載器:
train_dataloader = DataLoader(train_dataset, batch_size, shuffle)
for images, labels in train_dataloader:
print(images, labels)
與 相同。validation_dataset
添加回答
舉報