Solved by ptrblck in post #6 
              
              
                Assuming pipe is a DataLoader object, you could iterate it once and collect all targets via: 
targets = []
for _, target in pipe:
    targets.append(target)
targets = torch.stack(targets)

and calculate the class distribution later. 
I hope that the target tensors are not too big to fit into your RA…