Clear Filters
Clear Filters

How to do cross validation for a minibatch queue?

2 views (last 30 days)
9 subjects are used for training and 1 subject is used for testing. In this example both training and testing data are saved in different folders. Here input is given to the network as minibatches. I have a similar situation where I need to use cross validation. How to use cross validation when input to the network is as minibatch queue? I need to do the cross-validation in such a way that any one subject can be used for testing and all others are used for training.

Answers (1)

Ganesh on 3 Jan 2024
I understand that you would like to set up your system for cross validation when input is given as a minibatch queue.
A simple way you could achieve this is by manipulating the data you feed to the network within a loop. At every iteration you can create a folder structure which contains the "train folder" of the 9 chosen data points and the "test folder" containing the last data point. Here is conceptual working of the same:
mainFolder = 'master_folder_path'; % Replace with the path to your main folder
subfolders = dir(mainFolder);
subfolders = subfolders([subfolders.isdir]);
subfolders = subfolders(~ismember({}, {'.', '..'}));
numSubfolders = numel(subfolders);
combinations = nchoosek(1:numSubfolders, numSubfolders-1); % As we need 1 data for test set, can be modified
tDir = tempdir;
for i = 1:size(combinations, 1)
combinationFolder = fullfile(tDir, sprintf('Batch_%d', i));
if ~exist(combinationFolder, 'dir')
trainFolders = combinations(i, :);
testFolder = setdiff(1:numSubfolders, trainFolders);
for j = 1:numSubfolders
if ismember(j, trainFolders)
folderName = 'train';
folderName = 'test';
subfolderPath = fullfile(combinationFolder, folderName, subfolders(j).name);
if ~exist(subfolderPath, 'dir')
trainFol = combinationFolder+"\train";
testFol = combinationFolder+"\test";
% -----------------------------------
% Train using the newly created folder
% -----------------------------------
rmdir(combinationFolder,'s') % Delete the folder once used
Further, you may modify the function "nchoosek()" to use training data of other sizes.
Hope this helps!




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!