How average all predictions when using kfold cross validation

total_preds = []
for model in models :
    dataloader = DataLoader(test_data,batch_size=1,shuffle=False,num_workers=2)
    preds = []
    temp = []
    i = 0
    for batch in dataloader :
        inputs = batch['input_features'].float()
        with torch.no_grad() :
            p = model(inputs)
            preds.append(torch.squeeze(p))
    
    total_preds.append(preds)

im using kfold cross validation so i have multiple models and i need to average out the predictions of every model.
In the code above preds if of dimension (730,3) and total_preds is the list of all predictions in every fold. I want to retain the dimensions after averaging.
How do i go about it without using any extra loop?

You could calculate the mean in the “kfold” dimension as given in this code snippet:

total_preds = []
for _ in range(4): # kfold loop
    preds = []
    for _ in range(730): # data loop
        p = torch.randn(3)
        preds.append(p)
    preds = torch.stack(preds)
    total_preds.append(preds)

total_preds = torch.stack(total_preds)
print(total_preds.shape) # [4, 730, 3]
mean_preds = total_preds.mean(0)
print(mean_preds.shape) # [730, 3]
1 Like

That worked!!
Thanks