Change labels in Data Loader

(Shangeth Rajaa) #1

I have a data set of images, labels . I took a subset of it and want to change the labels of the whole subset to a single label.

eg: MNIST 0,1,2,3,4,5,6,7,8,9 ; lets say i want labels of 5,6,7,8,9 be 5. so final data labels be 0,1,2,3,4,5.

How to do it?


You could set the new value using a condition on your targets:

dataset = datasets.MNIST(

dataset.targets[dataset.targets > 5] = 5
> tensor([0, 1, 2, 3, 4, 5])

(Shangeth Rajaa) #3

AttributeError: ‘MNIST’ object has no attribute ‘targets’


In older torchvision versions, you had to use train_labels or test_labels depending if the train argument was set to True or False, respectively.