Do you know the `Dataset beforehand, i.e. are you creating it?
If so, you could store it as a member variable and just get it:
class MyDataset(Dataset):
    def __init__(self, data, targets):
        self.data = data
        self.targets = targets
        self.max_label = torch.max(targets)
        
    def __getitem__(self, index):
        pass
    
    def __len__(self):
        pass

n_classes = 10
data = torch.randn(100, 2)
targets = torch.LongTensor(100).random_(10)

dataset = MyDataset(data, targets)
loader = DataLoader(dataset)
loader.dataset.max_label