Save fitcsvms running in parfor loop

2 ビュー (過去 30 日間)
Aleksander Tyczynski
Aleksander Tyczynski 2019 年 8 月 5 日
コメント済み: Edric Ellis 2019 年 8 月 6 日
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,

採用された回答

Edric Ellis
Edric Ellis 2019 年 8 月 6 日
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 件のコメント
Edric Ellis
Edric Ellis 2019 年 8 月 6 日
Moving answer from OP to comment:
Thank you so much!
It works perfectly. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by