Converting for loop to GPU friendly code/ Cross Validation with GPU
Show older comments
Hi does anyone have an idea how to convert the following for loop to GPU friendly code? I have tried to do so with arrayfun, crossval and fitrlinear functions. But to no avail. I have converted the objects to gpu arrays but im not sure how to carry out cross validation using the GPU, specifically without invoking the for loop. Thanks.
function [output] = cross_val(featMatGPU,labelMatGPU,foldGrouping)
G1 = unique(foldGrouping(:));
G = gpuArray(G1);
estimate=[];
gpuestimate = gpuArray(estimate);
pooledTargets =[];
gpupooledTargets = gpuArray(pooledTargets);
g = [];
gpug = gpuArray(g);
testingOrder = [];
gputestingOrder = gpuArray(testingOrder);
for i=1:length(G)
fprintf('\t\tFold %d of %d\n',i,length(G));
idx1 = find(foldGrouping~=G(i)); % Training set
idx2 = find(foldGrouping==G(i)); % Test set
gputestingOrder = [gputestingOrder, idx2(:)];
% train
w = featMatGPU(idx1,:) \ labelMatGPU(idx1);
% test
estimateTemp = featMatGPU(idx2,:)*w;
pooledTargets = [pooledTargets,labelMatGPU(idx2)];
estimate = [estimate,estimateTemp(:)];
end
end
Or rather, the question can be simplified to how do you perform leave one out cross validation for the code below in a GPU friendly manner:
featMatGPU = gpuArray(featMat);
labelMatGPU = gpuArray(labelMat)
function [results] = test_script_crossval(x,y)
[results] = x \ y(:,1);
end
results = test_script_crossval(featMatGPU,labelMatGPU)
10 Comments
Joss Knight
on 4 Nov 2019
Your loop looks extremely vectorizable. For a start you should be able to get your train and test indices out of unique, and use pagefun to do your mldivide operation with multiple system matrices. But it's hard to tell exactly what to do because I don't know what the input data is. What are the sizes and datatypes of the input arguments? Is foldGrouping a vector or a matrix?
Joel Lo
on 4 Nov 2019
Joel Lo
on 4 Nov 2019
Walter Roberson
on 4 Nov 2019
Indexing a GPU array is not efficient. It would be more efficient to be testing G1(i) than G(i)
Joss Knight
on 4 Nov 2019
Your new code is even more confusing. Now you're indexing training in pagefun, whereas the whole point of pagefun is to operate on multiple matrices at once.
So, please confirm: training and target are 260252x260252 matrices? And foldGrouping is a 119x2187 matrix? Or is it a 1x260253 vector as you said?
Joel Lo
on 4 Nov 2019
Walter Roberson
on 4 Nov 2019
idx1 = foldGrouping~=G(i); % Training set
idx2 = foldGrouping==G(i); % Test set
C = pagefun(mldivide,training(idx1,:),targets(idx1));
You do not use idx2 after you build it. Should that third line be targets(idx2) ?
Joel Lo
on 5 Nov 2019
Walter Roberson
on 5 Nov 2019
I do not think it should be involved in this step. If I were to involve it, I would take the results of the mldivide and multiply them by training(idx2,:) and compare against targets(idx2) in order to determine how well the training did.
Joss Knight
on 6 Nov 2019
Edited: Joss Knight
on 6 Nov 2019
So, it looks like each matrix sent to mldivide is the same rows of training but with a different row removed each time, and similarly for targets.
[~, IG] = unique(foldGrouping);
A_all = training(IG,:);
B_all = targets(IG);
% Create a version of A and B replicated along dim 3, but
% with a different row removed from each page
numSolves = numel(IG);
numCols = size(A_all,2);
A = repmat(A_all,1,1,numSolves);
B = repmat(B_all,1,1,numSolves);
selectorB = reshape(~eye(numSolves),numSolves,1,[]);
selectorA = repmat(selectorB,1,numCols);
A = reshape(A(selectorA),numSolves-1,numCols,[]);
B = reshape(b(selectorB),numSolves-1,1,[]);
% Solve the multiple systems
C = pagefun(@mldivide,A,B);
However, the problem is that pagefun mldivide doesn't support rectangular matrices yet. You can get round this for now by using a pseudo-inverse solution (solving the normal equations). For a well-conditioned problem this shouldn't be too bad.
function X = pagefunMldivide(A,B)
% Solve AX = B for an under-determined problem in batch,
% via the normal equations:
% X = ((AA')A) \ (AA')B
At = pagefun(@transpose,A);
AAt = pagefun(@mtimes,A,At);
AAtA = pagefun(@mtimes,AAt,A);
AAtB = pagefun(@mtimes,AAt,B);
X = pagefun(@mldivide,AAtA,AAtB);
end
I haven't checked any of this so no doubt there are bugs.
Answers (0)
Categories
Find more on Arithmetic Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!