I’m so confused. Here is what I did and its result:
batch_size = 6
weight = 1. / class_sample_count
samples_weight = weight[target]
sampler = WeightedRandomSampler(samples_weight, batch_size, replacement=False)
print('samlper= ', list(sampler))

samlper=  [8857, 190, 210, 8028, 10662, 1685]
train_dl = torch.utils.data.DataLoader(train_ds, batch_size=batch_size, sampler=sampler, num_workers = 16)
valid_dl = torch.utils.data.DataLoader(valid_ds, batch_size=batch_size, shuffle=True, num_workers = 16)

inputs, targets = next(iter(train_dl))     # Get a batch of training data
print('targets= ', targets)

targets=  tensor([4, 5, 5, 0, 3, 3])
Loss after 15 Epochs:

Epoch [ 1/15], Step [  1, 1825], Loss: 1.5785
Epoch [ 2/15], Step [  1, 1825], Loss: 1.9562
Epoch [ 3/15], Step [  1, 1825], Loss: 1.8681
Epoch [ 4/15], Step [  1, 1825], Loss: 2.0667
Epoch [ 5/15], Step [  1, 1825], Loss: 1.9168
Epoch [ 6/15], Step [  1, 1825], Loss: 1.8286
Epoch [ 7/15], Step [  1, 1825], Loss: 1.9063
Epoch [ 8/15], Step [  1, 1825], Loss: 1.8187
Epoch [ 9/15], Step [  1, 1825], Loss: 1.6252
Epoch [10/15], Step [  1, 1825], Loss: 2.3157
Epoch [11/15], Step [  1, 1825], Loss: 1.7716
Epoch [12/15], Step [  1, 1825], Loss: 2.1706
Epoch [13/15], Step [  1, 1825], Loss: 1.7085
Epoch [14/15], Step [  1, 1825], Loss: 1.7478
Epoch [15/15], Step [  1, 1825], Loss: 2.0860

evaluate_model(model, valid_dl)

accuracy: 19.58
evaluate_model(model, train_dl)

accuracy: 0.00
It seems that something goes wrong!