PyTorch — Weighted Random Sampler Tutorial Note

Yanwei Liu
May 21, 2021
==================================================================== 1.使用Loss functionRandaugment is one of the best  augmentation method, it will improve your model performance and I was using weight normalization like thisnSamples = [346,168,106] # class samples
normedWeights = [1 - (x / sum(nSamples)) for x in nSamples]
normedWeights = torch.FloatTensor(normedWeights).to(device)
print(normedWeights)
nn.CrossEntropyLoss(weight=normedWeights)
====================================================================
2.影片中介紹的WeightedRandomSampler
class_weights = [1, 50] # equal to [1/50, 1]
sample_weights = [0] * len(dataset)
for idx, (data, label) in enumerate(dataset):
class_weight = class_weights[label]
sample_weights[idx] = class_weight
sampler = WeightedRandomSampler(sample_weights,num_samples=len(sample_weights), replacement=True)loader = DataLoader(dataset, batch_size=batch_size, sampler=sampler)==================================================================== 3. 自動計算class_weights
在2.當中的class_weights是人工給予的,如果想要透過程式自動運算,可以透過sklearn的compute_class_weight這個function
from sklearn.utils.class_weight import compute_class_weightclass_weights =
compute_class_weight('balanced',np.unique(train_label),train_label)
====================================================================

--

--