Hi everyone, I need to stop the execution of a function if finish a specific time. Specifically the part of the program where i need to implement it is when i call a function to create a regressor:
[trainedModel, validationRMSE] = trainRegressionModelLinear(T, KFolds);
I have left the program running and it never finish the function so i wanted to exit the function and continue the rest of the program. The function works because i change the data "T" that i insert in the function and i get a trainedModel with its value but with long data T i think no works.
There is a example that i need to do but with try- catch doesnt work:
try
[trainedModel, validationRMSE] = trainRegressionModelLinear(T, KFolds);
catch (*time out finish*)
validationRMSE = "Error";
trainedModel = [];
end
I find some information how said that i need to use parallel pool, batch(), parfeval() or a timer but i dont understand what function is useful for my situation. I already use parallel pool with 4 workers because is better to train regressor but to apply the time out no idea.
Thanks so much for the help and sorry if I have explained badly because the english is not my native language.

 채택된 답변

Edric Ellis
Edric Ellis 2021년 4월 19일

2 개 추천

You could use parfeval to do this, but I'm not sure how you might fit that in with the rest of your parpool usage. Here's the simplest case where you use wait with a single future:
fut = parfeval(@pause, 0, 20); % Dummy function - you'd use your training function here
ok = wait(fut, 'finished', 4); % Wait for up to 4 seconds for 'fut' to finish
if ~ok
disp('Did not finish in time.')
else
disp('Did finish in time.')
end
You could use fetchNext to do something more sophisticated with multiple futures, as that also takes a timeout parameter.

댓글 수: 6

Javier Checa
Javier Checa 2021년 4월 21일
편집: Javier Checa 2021년 4월 21일
Thank you very much, it helped me.
Jan Kappen
Jan Kappen 2022년 3월 16일
편집: Jan Kappen 2022년 3월 17일
I'd like to add, that even without parallel computing toolbox this works:
fut = parfeval(backgroundPool, @pause, 0, 5); % Dummy function - you'd use your training function here
disp('jo ahhah')
ok = wait(fut, 'finished', 4); % Wait for up to 4 seconds for 'fut' to finish
if ~ok
disp('Did not finish in time.')
else
disp('Did finish in time.')
end
Note the backgroundPool parameter.
And you can run arbitrary code before calling the wait function, i.e. Matlab remains responsive which is super awesome.
Vincent
Vincent 2023년 9월 11일
편집: Vincent 2023년 9월 11일
I get the following error @Jan Kappen:
'parfeval' requires Parallel Computing Toolbox.
Jan Kappen
Jan Kappen 2023년 9월 12일
@Vincent then you have an old Matlab version. I do not have Parallel Computing Toolbox installed and it works on R2023a. Be sure to use the backgroundPool.
Vincent
Vincent 2023년 9월 13일
@Jan Kappen I am using matlab 2019a
Jan Kappen
Jan Kappen 2023년 9월 13일

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

추가 답변 (1개)

Giovanni Soleti
Giovanni Soleti 2024년 9월 11일

0 개 추천

A similar solution that worked in my case is the following:
% you do what u need
tic
try
s = my_fcn();
catch
disp("out of time");
end
function s = my_fcn()
if toc > timeout
err("you are out of time");
end
% your function ...
% ,,,,,,,,,,,
end

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2021년 4월 16일

답변:

2024년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by