【问题标题】:Oversampling the dataset with pytorch使用 pytorch 对数据集进行过采样
【发布时间】:2020-11-26 15:47:31
【问题描述】:

我对 PyTorch 和 python 还是很陌生。我有一个二元分类问题,其中一个类的样本比另一个类多,所以我决定通过对其进行 more 扩充来对样本数量较少的类进行过采样,例如我会生成一类样本中的 7 个图像,而对于另一类,我将从一个样本中生成 3 个图像。我正在使用 imguag 来增强 PyTorch,所以我不确定哪个更好,首先增强我的数据集,然后将其传递给 torch.utils.data.Dataset 类,或者读取数据并在 中进行增强Dataset 类的 init 函数。

【问题讨论】:

    标签: python pytorch oversampling


    【解决方案1】:

    我觉得处理不平衡数据还有另外一种方法,nn.BCELoss是二分类问题的常用选择,可以设置一个pos_weight来平衡正负样本。如果这样做,您可以对所有样本应用相同的增强。代码如下:

    # defines the augmentation
    transform = transforms.Compose([transforms.RandomRotation(20),
                                transforms.Resize((32, 32)),
                                transforms.ToTensor(),
                                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
    # initializes the data set
    dataset = Dataset(train_data_path, transforms=transform)
    # defines the loss function
    criterion = torch.nn.BCELoss(torch.tensor([10.]))
    

    【讨论】:

    • 我想这和过采样不同,我想先尝试过采样,然后也许我会尝试改变权重。
    猜你喜欢
    • 2018-07-09
    • 2018-10-29
    • 2020-12-30
    • 2019-05-09
    • 2018-01-13
    • 2015-01-31
    • 2021-11-07
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多