Skip to content

MNIST loader odd implementation #577

Closed
@dizcza

Description

@dizcza

Wouldn't it be better to change

if self.train:
    self.train_data, self.train_labels = torch.load(
        os.path.join(self.root, self.processed_folder, self.training_file))
else:
    self.test_data, self.test_labels = torch.load(
        os.path.join(self.root, self.processed_folder, self.test_file))

...

if self.train:
    img, target = self.train_data[index], self.train_labels[index]
else:
    img, target = self.test_data[index], self.test_labels[index]

...

def __len__(self):
    if self.train:
        return len(self.train_data)
    else:
        return len(self.test_data)

to

if self.train:
    self.data_file = self.training_file
else:
    self.data_file = self.test_file
self.data, self.labels = torch.load(os.path.join(self.root, self.processed_folder, self.data_file))

...

img, target = self.data[index], self.labels[index]

...

def __len__(self):
    return len(self.data)

Apart from boling down the code complexity, it's not a good practice to have dynamic field names depending on the passed arguments. Benefits are clear for both users and developers who want to subclass MNIST.

torchvision==0.2.1 (same in master)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions