Parfor: Variable indexed in different ways.

조회 수: 12 (최근 30일)
Ioannis Agalliadis
Ioannis Agalliadis 2017년 9월 28일
댓글: Greg 2017년 9월 29일
I want to parallelize the following for loop but the I have the following problem: In a parfor loop variable 'perf' is indexed in different ways
The problem is in the 3rd line from the bottom because I index in a different way the matrix perf.
How can I avoid this problem?
NET = cell(5,11);
TR = cell(5,11);
ave_perfs = zeros(1,11);
perf = zeros(5,11);
profile on;
parfor j = 1:11
for bin = 1:5
processed_train_Ext = load([ExtensionTrainPath,'/',Features{j},'/','train',num2str(bin),'_processed_Ext'];
processed_train_Flx = load([FlexionTrainPath,'/',Features{j},'/','train',num2str(bin),'_processed_Flx']);
processed_test_Ext = load([ExtensionTestPath,'/',Features{j},'/','test',num2str(bin),'_processed_Ext']);
processed_test_Flx = load([FlexionTestPath,'/',Features{j},'/','test',num2str(bin),'_processed_Flx']);
% concatenating the training data
train_data = [processed_train_Ext.finalMatrix(:,1:end-1); processed_train_Flx.finalMatrix(:,1:end-1)];
true_train_labels = [processed_train_Ext.finalMatrix(:,end); processed_train_Flx.finalMatrix(:,end)];
%concatenating the test data
test_data = [processed_test_Ext.finalMatrix(:,1:end-1) ; processed_test_Flx.finalMatrix(:,1:end-1)];
true_test_labels = [processed_test_Ext.finalMatrix(:,end); processed_test_Flx.finalMatrix(:,end)];
%fit network
net = fitnet(5);
net.divideParam.trainRatio = 0.85;
net.divideParam.valRatio = 0.15;
net.divideParam.testRatio = 0.0;
% train and validate train data
[net, tr] = train(net,train_data',true_train_labels','useParallel','yes');
NET{bin,j} = net;
TR{bin,j} = tr;
% test newly trained network with test data
outputs = NET{bin,j}(test_data','useParallel','yes');
% calculate accuracy on the test data
perf(bin,j) = 1 - mse(net, true_test_labels, outputs);
toc
beep on; beep;
end
% calculate averages of the errors
ave_perfs(1,j) = sum(perf(:,j))/5;
end
  댓글 수: 5
OCDER
OCDER 2017년 9월 29일
I actually didn't know what the issue was until I tried your solution, which did the trick for me.
Greg
Greg 2017년 9월 29일
Ahh, fair enough. My MATLAB machine is not my internet machine, so I'm rarely able to test the poster's code or my own suggestions.
I'm glad we got it solved and an accepted answer. Thanks for the assist.

댓글을 달려면 로그인하십시오.

채택된 답변

Greg
Greg 2017년 9월 28일
Try replacing this code (inside the loop):
% calculate averages of the errors
ave_perfs(1,j) = sum(perf(:,j))/5;
With this code (outside the parfor):
% calculate averages of the errors
ave_perfs = mean(perf,1);
Then you could even remove the pre-allocation of ave_perfs at the top.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by