class_sample_count = np.repeat(0,num_classes)  # dataset has 10 class-1 samples, 1 class-2 samples, etc.
train_Data = get_train_data()
for train_data_row in train_Data:
    index = int(train_data_row[1])
    class_sample_count[index]=class_sample_count[index]+1
class_sample_count=class_sample_count/len(train_Data)
class_sample_count=1/class_sample_count
This below worked for me.

weights = []
for train_Data_row in train_Data:
    weight=class_sample_count[int(train_data_row[1])]
    weights.append(weight)
weights=torch.Tensor(weights)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, len(weights))
trainloader = torch.utils.data.DataLoader(train_data_set, batch_size=batch_size, sampler=sampler)