Save fitcsvms running in parfor loop

2 vues (au cours des 30 derniers jours)
Aleksander Tyczynski
Aleksander Tyczynski le 5 Août 2019
Commenté : Edric Ellis le 6 Août 2019
Hello,
I have a code which I am trying to get running faster, it has 3 fitcsvm function. To do so (I cannot use the GPU) I decided to perform a parfor loop.
However, I cannot use the save function and if the save is outside the loop a 'variable not found' error occurs
parfor i = 1:3
if i == 1
SVM_tt = fitcsvm(features_dt(:,1:10),features_dt(:,11),...
'Standardize',true,'KernelFunction','RBF','BoxConstraint', tt_C, 'KernelScale',tt_sigma);
end
if i == 2
SVM_bb = fitcsvm(features_bp(:,1:10),features_bp(:,11),...
'Standardize',true,'KernelFunction','RBF','BoxConstraint', bb_C, 'KernelScale',bb_sigma);
end
if i == 3
SVM_ss = fitcsvm(features_swa(:,1:10),features_swa(:,11),...
'Standardize',true,'KernelFunction','RBF','BoxConstraint', ss_C, 'KernelScale',ss_sigma);
end
end
save('SVM_dt.mat','SVM_tt');
save('SVM_bp.mat','SVM_bb');
save('SVM_swa.mat','SVM_ss');
Without the parfor loop the code wors perfectly fine.
Thank you,

Réponse acceptée

Edric Ellis
Edric Ellis le 6 Août 2019
Your outputs are not available after the parfor loop because the parfor variable classification considers them to be loop temporary variables. More details in the doc.
The simplest thing to do here is to make cell array inputs and outputs for your parfor loop.
% Step 1 - set up input cell arrays
features = {features_dt, features_bp, features_swa};
constraints = {tt_C, bb_C, ss_C};
scales = {tt_sigma, bb_sigma, ss_sigma};
parfor i = 1:3
% Step 2 - access cell array contents in parfor loop
f = features{i};
SVM{i} = fitcsvm(f(:, 1:10), f(:, 11), ...
'Standardize',true,...
'KernelFunction','RBF',...
'BoxConstraint', constraints{i}, ...
'KernelScale',scales{i});
end
% Step 3 - split output cell array
[SVM_tt, SVM_bb, SVM_ss] = deal(SVM{:});
This works because parfor sees the "sliced" assignment into SVM, and knows that you are performing independent computations, and that it can therefore make the value available after the loop completes.
  1 commentaire
Edric Ellis
Edric Ellis le 6 Août 2019
Moving answer from OP to comment:
Thank you so much!
It works perfectly. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by