**Hi there! **
I have a custom dataset in which (even though I have more or less the same number of samples for each class) it missclassified some of the data for some specific classes. So what I’m trying to do is oversample by using “sampler” in the dataloader and WeightedRandomSampler as follows:
from sklearn.utils.class_weight import compute_class_weight
def load_data(root_path, batch_size):
transform1 = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
transform2 = transforms.ToTensor();
data = datasets.ImageFolder(root=root_path, transform= transform1)
class_weights = compute_class_weight('balanced', np.unique(data.targets), data.targets)
class_weights = []
for root, subdir, files in os.walk(root_path):
    if len(files) > 0:
        class_weights.append(1/len(files))
sample_weights = [0]*len(data)

for idx, (data,label) in enumerate(data):
    class_weight = class_weights[label]
    sample_weights[idx] = class_weight
sampler = WeightedRandomSampler(sample_weights, num_samples=len(sample_weights), replacement=True)    
data_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, sampler=sampler)#, drop_last=False, num_workers=4)
return data_loader 

trainloader = load_data(root_path_train, batch_size)
testloader = load_data(root_path_test, batch_size)
validloader = load_data(root_path_valid, batch_size)
for x, y in trainloader:
print(y)
break
However, when running the code in the for x, y in trainloader: (where x = data and y = targets) I get an error:
/home/laura/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py:70: FutureWarning: targets as keyword args. From version 0.25 passing these as positional arguments will result in an error
FutureWarning)
Traceback (most recent call last):
File “/home/laura/Documentos/DatasetAppLGG3v6/Sin título0.py”, line 104, in 
for x,y in trainloader:
ValueError: too many values to unpack (expected 2)
or when i write instead: for i, (x,y) in trainloader: I get:
Traceback (most recent call last):
File “/home/laura/Documentos/DatasetAppLGG3v6/Sin título0.py”, line 104, in 
for x,y in trainloader:
File “/home/laura/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 345, in next
data = self._next_data()
File “/home/laura/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 385, in _next_data
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
File “/home/laura/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File “/home/laura/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in 
data = [self.dataset[idx] for idx in possibly_batched_index]
IndexError: index 2312 is out of bounds for dimension 0 with size 3
Can anybody help me please???