Python 如何使用WeightedRandomSampler平衡Pytork中的不平衡数据?

Python 如何使用WeightedRandomSampler平衡Pytork中的不平衡数据?,python,machine-learning,pytorch,Python,Machine Learning,Pytorch,我有两级问题,我的数据不平衡。 0类有232550个样本,1类有13498个样本。 PyTorch文档和互联网告诉我在数据加载器中使用类WeightedRandomSampler 我尝试过使用WeightedRandomSampler,但不断出现错误 trainratio = np.bincount(trainset.labels) #trainset.labels is a list of float [0,1,0,0,0,...] classcount = tra

我有两级问题,我的数据不平衡。 0类有232550个样本,1类有13498个样本。 PyTorch文档和互联网告诉我在数据加载器中使用类WeightedRandomSampler

我尝试过使用WeightedRandomSampler,但不断出现错误

    trainratio = np.bincount(trainset.labels) #trainset.labels is a list of 
    float [0,1,0,0,0,...] 
    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)
我打印出的一些尺寸:

train_weights = tensor([4.3002e-06, 4.3002e-06, 4.3002e-06,  ..., 
4.3002e-06, 4.3002e-06, 4.3002e-06])

train_weights shape=  torch.Size([246048])
我不明白为什么会出现这样的错误:

UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  self.weights = torch.tensor(weights, dtype=torch.double)
我尝试过其他类似的解决方法,但到目前为止,所有的尝试都会产生一些错误。
我应该如何实现这一点来平衡我的培训、验证和测试数据?

因此,显然这是一个内部警告,而不是错误。据PyTorch的人说,我可以继续编码,而不必担心警告信息

如果您添加了一个链接,您可以从中获得此信息,这将非常有用;在它的当前形式中,它是非常无用的,因为您的帖子基本上只声明它是一个警告(通过查看您的问题已经知道了,其中您的错误为:
UserWarning