Bug in loadLearnerForCoder from MATLAB coder

조회 수: 3 (최근 30일)
Diogo Tecelão
Diogo Tecelão 2020년 12월 1일
답변: Marou AD 2024년 2월 11일
Hello,
I've built a classification model (esemble), and saved it using saveLearnerForCoder. However, when compiling my code to C using MATLAB coder, an error is raised. I'm using MATLAB 2020a. This issue appears to have been previously reported here. Please, find detailed information below. I would very appreciate a workaround.
I'm loading the classifier as recommended:
cough_detection_model = loadLearnerForCoder('my_model'); % I tried with both ' and ", just in case
This triggers the following error:
??? The input to coder.const cannot be reduced to a constant: Unsupported value.
Error in ==> loadLearnerForCoder Line: 41 Column: 68
My classifier was created with the following code:
template_tree = templateTree('MaxNumSplits', 700, 'MinLeafSize', 20);
ensemble_model = fitcensemble(features, classes, ...
'Learners', template_tree, 'Method','RUSBoost', 'NumLearningCycles', 500, ...
'LearnRate', 0.1, 'ScoreTransform', 'None');
saveLearnerForCoder(ensemble_model, 'my_model')
I am able to load the classifier and use it normally. The error only happens when using the codegen.
I made some digging, and created a bypass function which avoids the problems from loadLearnerForCoder. Obviously it would crash somewhere, but I think it gave me some further information. This is the code I managed to create:
matFile = coder.load("my_model");
fromStructFcn = str2func('classreg.learning.classif.CompactClassificationEnsemble.fromStruct');
obj = fromStructFcn(matFile.compactStruct); % raises error
Which raises the following error:
??? Failed to compute constant value for nontunable property 'NumPredictors'. In code generation, nontunable
properties can only be assigned constant values.
I have tried to overpass this by doing:
matFile.compactStruct.DataSummary.NumPredictors = coder.const(117);
But this doesn't help either.
Can please anyone help me?
  댓글 수: 1
Diogo Tecelão
Diogo Tecelão 2020년 12월 1일
Update
By using the following code:
fromStructFcn = str2func('classreg.learning.classif.CompactClassificationEnsemble.fromStruct');
obj = fromStructFcn(coder.const(matFile.compactStruct));
I now get a different error:
??? Code generation does not support MATLAB class 'classreg.learning.coder.classif.CompactClassificationEnsemble'
in this context because the class does not implement toRedirect() and/or fromRedirect() methods.

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

답변 (2개)

Sriram Padavettan
Sriram Padavettan 2020년 12월 4일
편집: Sriram Padavettan 2020년 12월 4일
The problem is 2020A. I think there's a bug in that version. I messed around with this same issue all day yesterday on 2020A, tried 2020B instead, and it worked. Maybe even 2019b or earlier would work.
:)

Marou AD
Marou AD 2024년 2월 11일
It seems there is a bug in MATLAB 2020a that might be causing the problem. I had a similar issue, and after experimenting, I found that switching to MATLAB 2020b resolved the problem for me. It seems there might be a compatibility or bug fix in the later version. I recommend giving MATLAB 2020b a try , it will resolves your compilation issue.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by