pytorch实现k折交叉验证

k折交叉验证白话解释:

一共100个数据集,5折交叉验证就是把这些数据分成5份,每份20个,分别为ABCDE

然后循环5次训练和预测

第一次用ABCD训练,用E预测

第二次用ABCE训练,用D预测

……


一些之前困扰我的问题marking

1、训练集、验证集、测试集都是啥?

因为网上很多说训练、验证、测试,可能我没有系统学过机器学习、所以这一度让我很蒙圈。

其实一般情况下会把整个数据按比例分成训练集、验证集、测试集,比如6:2:2;

这样做的意思是,把测试集直接单独划出来,不管训练和验证怎么折腾,测试集的数据都不参与,最后你这个模型训练好了,再来预测我这个测试集,进行评价。

为什么要设置验证集,因为测试集不参与训练了,那就得有个验证集替这个模型把把关。看看训练到啥时候了,可以停止了,主要是对超参数的优化,这块我还要再合计一下,有人懂的的话麻烦多多指正。

那为什么不可以直接用测试集来把关,因为经过不断的训练,这个模型会特别适合区分测试集的数据,但是一旦换了别的数据,就会gg了。所以提前把测试集拿出来,不参与训练和验证的纷争,可以更公平的展示这个模型的效果。

2、k折交叉验证是用在哪个数据集上,训练?验证?测试?

一般是用在,训练验证集上,为的是提高模型的性能,因为如果不设置交叉验证,一直用同样的数据作为验证集,那这个模型就会特别适合区分这一部分数据,一到测试集上就gg了,所以要大家都来验证一把,都来掺和一脚。

所以可以把整个数据集,按照一定比例分成训练验证集和测试集,然后把训练验证集里的数据进行k折交叉验证。

例:100个数据,划分训练验证集80个,测试集20个

把80个训练验证集进行5折交叉验证,所以分成5份,每份16个数据,分别为ABCDE

第一步训练ABCD,用E验证;第二步训练ABCE,用D验证……(一共5步)选择最优的超参数模型,然后进行epoch轮数训练,保存训练好的模型,最后去预测测试集的那20个数据。

交叉验证可能是更好地优化模型,一是从超参数上优化,二是从综合评估上,就比如小样本,需要每个部分都去参加一下测试集,才能说明这个的网络的优势,也许是这样吧。(先理解到这,后续有在更新)


如何在pytorch上实现

这是我参考的文章,文章写得很清楚,但是有几个点是我实现过程中出现的问题:手把手教你用pytorch实现k折交叉验证,解决类别不平衡_pytorch 交叉验证_一个菜鸟的奋斗的博客-CSDN博客icon-default.png?t=N176https://blog.csdn.net/u013685264/article/details/126488633

tips:

上述文章中的MyDataSet中,path=self.imgs[idx],得到的是batch_size个路径,所以在img=Image.open(f)会报错AttributeError: Caught AttributeError in DataLoader worker process 0. 这可能是上述文章的train和validata函数是有再提的步骤,但是作者没有放出来,可能是因为和我自己的train函数不匹配而已。

 之所以会出现batch_size个路径,把路径转换成矩阵,但是它上面在imgs[idx]那,得到的是一列,如果想拿到第一个,得是imgs[idx][0],这种应该是一种可能行的方式。

另一种解决方式:把对应的那两行代码改成下面的,转成List形式

for i, (train_idx, val_idx) in enumerate(skf.split(imgs, labels)):
    trainset, valset = [imgs[s] for s in train_idx], [imgs[s] for s in val_idx]
    traintag, valtag = [labels[s] for s in train_idx], [labels[s] for s in val_idx]

用到train和validate这两个函数的地方

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Sure! Here's an example of how you can implement k-fold cross-validation using PyTorch: ```python import torch from torch.utils.data import Subset, DataLoader # Define your dataset and model dataset = YourDataset() model = YourModel() # Set hyperparameters num_epochs = 10 batch_size = 64 k_folds = 5 # Calculate the size of each fold fold_size = len(dataset) // k_folds # Define the loss function and optimizer criterion = torch.nn.CrossEntropyLoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.001) # Perform k-fold cross-validation for fold in range(k_folds): # Create the training and validation sets for this fold start_index = fold * fold_size end_index = (fold + 1) * fold_size # Prepare the data loaders for training and validation train_indices = list(set(range(len(dataset))) - set(range(start_index, end_index))) train_sampler = SubsetRandomSampler(train_indices) train_loader = DataLoader(dataset, batch_size=batch_size, sampler=train_sampler) val_indices = list(range(start_index, end_index)) val_sampler = SubsetRandomSampler(val_indices) val_loader = DataLoader(dataset, batch_size=batch_size, sampler=val_sampler) # Train the model for this fold for epoch in range(num_epochs): model.train() for inputs, targets in train_loader: optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() # Evaluate the model on the validation set for this fold model.eval() total_loss = 0 correct = 0 total = 0 with torch.no_grad(): for inputs, targets in val_loader: outputs = model(inputs) loss = criterion(outputs, targets) total_loss += loss.item() _, predicted = torch.max(outputs.data, 1) total += targets.size(0) correct += (predicted == targets).sum().item() # Print the validation accuracy for this fold print(f"Fold {fold + 1}, Epoch {epoch + 1}: Validation Accuracy = {100 * correct / total}%") ``` In this code, you would need to replace `YourDataset` and `YourModel` with your own dataset and model classes. The code performs k-fold cross-validation by creating separate training and validation sets for each fold using the `SubsetRandomSampler`. The model is trained on the training set and evaluated on the validation set for each fold. Finally, the validation accuracy for each fold is printed. Remember to adjust the hyperparameters (`num_epochs`, `batch_size`, `k_folds`, etc.) according to your specific needs.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bAdwoMan*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值