gpt4 book ai didi

machine-learning - 在PyTorch中使用DataLoaders进行k折交叉验证

转载 作者:行者123 更新时间:2023-12-03 16:55:42 24 4
gpt4 key购买 nike

我将训练数据集分为80%的训练和20%的验证数据,并创建了DataLoader,如下所示。但是,我不想限制模型的训练。因此,我想到了将数据拆分为K(可能是5)折叠并执行交叉验证的方法。但是,我不知道如何在拆分后将数据集组合到我的数据加载器中。

train_size = int(0.8 * len(full_dataset))
validation_size = len(full_dataset) - train_size
train_dataset, validation_dataset = random_split(full_dataset, [train_size, validation_size])

full_loader = DataLoader(full_dataset, batch_size=4,sampler = sampler_(full_dataset), pin_memory=True)
train_loader = DataLoader(train_dataset, batch_size=4, sampler = sampler_(train_dataset))
val_loader = DataLoader(validation_dataset, batch_size=1, sampler = sampler_(validation_dataset))

先感谢您 !

最佳答案

我刚刚编写了一个与数据加载器和数据集一起使用的交叉验证功能。
这是我的代码,希望对您有所帮助。

# define a cross validation function
def crossvalid(model=None,criterion=None,optimizer=None,dataset=None,k_fold=5):

train_score = pd.Series()
val_score = pd.Series()

total_size = len(dataset)
fraction = 1/k_fold
seg = int(total_size * fraction)
# tr:train,val:valid; r:right,l:left; eg: trrr: right index of right side train subset
# index: [trll,trlr],[vall,valr],[trrl,trrr]
for i in range(k_fold):
trll = 0
trlr = i * seg
vall = trlr
valr = i * seg + seg
trrl = valr
trrr = total_size
# msg
# print("train indices: [%d,%d),[%d,%d), test indices: [%d,%d)"
# % (trll,trlr,trrl,trrr,vall,valr))

train_left_indices = list(range(trll,trlr))
train_right_indices = list(range(trrl,trrr))

train_indices = train_left_indices + train_right_indices
val_indices = list(range(vall,valr))

train_set = torch.utils.data.dataset.Subset(dataset,train_indices)
val_set = torch.utils.data.dataset.Subset(dataset,val_indices)

# print(len(train_set),len(val_set))
# print()

train_loader = torch.utils.data.DataLoader(train_set, batch_size=50,
shuffle=True, num_workers=4)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=50,
shuffle=True, num_workers=4)
train_acc = train(res_model,criterion,optimizer,train_loader,epoch=1)
train_score.at[i] = train_acc
val_acc = valid(res_model,criterion,optimizer,val_loader)
val_score.at[i] = val_acc

return train_score,val_score


train_score,val_score = crossvalid(res_model,criterion,optimizer,dataset=tiny_dataset)


为了对我们所做的事情有一个正确的直觉,请参阅以下输出:
train indices: [0,0),[3600,18000), test indices: [0,3600)
14400 3600

train indices: [0,3600),[7200,18000), test indices: [3600,7200)
14400 3600

train indices: [0,7200),[10800,18000), test indices: [7200,10800)
14400 3600

train indices: [0,10800),[14400,18000), test indices: [10800,14400)
14400 3600

train indices: [0,14400),[18000,18000), test indices: [14400,18000)
14400 3600

关于machine-learning - 在PyTorch中使用DataLoaders进行k折交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com