我有2类问题,我的数据高度不平衡。我有一类的232550个样本,第二类的13498。 PyTorch文档和互联网告诉我为DataLoader使用类WeightedRandomSampler。

我已经尝试过使用WeightedRandomSampler,但是我一直收到错误消息。

    trainratio = np.bincount(trainset.labels)
    classcount = trainratio.tolist()
    train_weights = 1./torch.tensor(classcount, dtype=torch.float)
    train_sampleweights = train_weights[trainset.labels]
    train_sampler = WeightedRandomSampler(weights=train_sampleweights,
    num_samples = len(train_sampleweights))
    trainloader = DataLoader(trainset, sampler=train_sampler,
    shuffle=False)


我看不到为什么在初始化WeightedRandomSampler类时出现此错误?

我尝试了其他类似的解决方法,但到目前为止,所有尝试均会产生一些错误。
我应该如何实施以平衡训练,验证和测试数据?

当前出现此错误:


  train__sampleweights = train_weights [trainset.labels] ValueError:也
  许多维度“ str”

最佳答案

问题出在火车组的类型上。
要解决该错误,可以将train set.labels转换为float

关于python - 如何在PyTorch中平衡(过采样)不平衡数据(使用WeightedRandomSampler)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54415345/

10-12 23:24