Parfor: Variable indexed in different ways.
Mostrar comentarios más antiguos
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 comentarios
Not sure it will help the parfor (but it might actually), but 2 things:
% calculate averages of the errors
ave_perfs(1,j) = sum(perf(:,j))/5;
1) Why calculate a mean manually? Use the mean function.
2) Why do it inside the loop?
Try putting this outside the parfor:
% calculate averages of the errors
ave_perfs = mean(perf,1);
Then you wouldn't even need to pre-allocate ave_perfs at the top.
OCDER
el 28 de Sept. de 2017
Greg, your comment should be in the answer section.
Ioannis, the issue is caused because you are accessing perf via perf(bin,j) AND perf(:,j) within the parfor loop. Matlab can't determine how to slice perf by column or by element, giving that error. By moving the ave_perf = mean(perf,1) outside the parfor, it's now clear perf can be sliced by element and thus no more error occurs.
As I stated in the first line, I wasn't sure it was actually an answer. I was hoping Ioannis would respond if it did indeed solve the problem, at which point I can move it to answer.
Thank you for clarifying that it was the source of the problem.
OCDER
el 29 de Sept. de 2017
I actually didn't know what the issue was until I tried your solution, which did the trick for me.
Greg
el 29 de Sept. de 2017
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!