如何一起使用k折的交叉验证和加权随机采样器?

发布于 2025-02-06 20:39:13 字数 617 浏览 2 评论 0原文

我正在尝试使用pytorch(data_loader)训练我的分类模型(3类) 因此,我的第一个问题是数据没有平衡,所以我使用了加权随机采样器

  class_weights = [5,1,1] 

  sample_weights = [0] * len(ds)

  for idx in enumerate(ds):
      class_weight = class_weights[idx[1]['targets']]
      sample_weights[idx[0]] = class_weight

  sampler = WeightedRandomSampler(sample_weights, num_samples=
                                    len(sample_weights), replacement=True)
  DataLoader( ds, batch_size=batch_size,  sampler=sampler, num_workers=2 )

但是现在我还有另一个问题,即过度拟合,所以我想使用 k折的交叉验证

有人可以帮助我,我真的不知道该怎么做!

I'm trying to train my classification model(3 classes) using PyTorch (data_loader)
so my first problem was that the data was not balanced so I used Weighted Random Sampler :

  class_weights = [5,1,1] 

  sample_weights = [0] * len(ds)

  for idx in enumerate(ds):
      class_weight = class_weights[idx[1]['targets']]
      sample_weights[idx[0]] = class_weight

  sampler = WeightedRandomSampler(sample_weights, num_samples=
                                    len(sample_weights), replacement=True)
  DataLoader( ds, batch_size=batch_size,  sampler=sampler, num_workers=2 )

but now I have another problem which is the overfitting so I want to use K-fold cross-validation

can someone help me cus I really don't know how to do it!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文