# How average all predictions when using kfold cross validation

``````total_preds = []
for model in models :
preds = []
temp = []
i = 0
inputs = batch['input_features'].float()
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