Unable to Generate Code out of Machine Learning Model
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am working on a machine learning model .
I followed the following steps after referring to a tutorial of Machine learning on YouTube by Shyamal Patel https://www.youtube.com/watch?v=k_BrPj3TcTE
Using the Classification Learner App: 1. Imported data and specified details of predictor and response variables 2. Selected Run train model option for all algorithms supported and checked the results for accuracy and ROC 3. Saved the required model as compact model. 4. Tried generation of C-Code from the option in the app. But here it is failing to do so, due to some unsupported function or calculations for code gen.
Could you please support on this part. Also need help on how to generate code in using some other alternatives, in case the specified functions within the classifier model Matlab function is not supported
0 commentaires
Réponses (2)
Walter Roberson
le 11 Déc 2017
The code generation is around 30 minutes in. If you watch through the step where they check Mex for problems, to the point they say the final step is to generate C++ code, then just after they do that click, the MATLAB source code they are generating against shows up on the screen. It does not train the classifier; it calls some routine whose name includes "codegen" that I would think likely loads the pre-trained classifier.
0 commentaires
Tom Lane
le 11 Déc 2017
Your screen shot shows generating MATLAB code from the app. That code does training. I think you want to export a compact model from the app and predict using that already-trained model, rather than using code that does training.
When you export a trained model, you get a struct that has an anonymous function inside.
>> trainedModel
trainedModel =
struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x))
RequiredVariables: {'meas1' 'meas2' 'meas3' 'meas4'}
ClassificationTree: [1×1 classreg.learning.classif.CompactClassificationTree]
About: 'This struct is a trained model exported from Classification Learner R2018b.'
HowToPredict: 'To make predictions on a new table, T, use: ↵ yfit = c.predictFcn(T) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵The table, T, must contain the variables returned by: ↵ c.RequiredVariables ↵Variable formats (e.g. matrix/vector, datatype) must match the original training data. ↵Additional variables are ignored. ↵ ↵For more information, see How to predict using an exported model.'
If I remember correctly, you cannot pass in an anonymous function into code to be processed by MATLAB Coder. However, for simple models without pre-processing such as via PCA, you can fetch the trained model out of the struct and use its predict method:
>> tree = trainedModel.ClassificationTree;
>> predict(tree,meas(1:30:end,:))
ans =
5×1 cell array
{'setosa' }
{'setosa' }
{'versicolor'}
{'versicolor'}
{'virginica' }
For MATLAB Coder, I think you want to export a compact model from the app to the workspace, save that compact model as a file, write a little function that will load the file and predict using the model it finds there, and run codegen on that little function. There is an example here:
I suggest you run this inside MATLAB to make sure it gives the right predictions. That is, make sure the compact model in the exported struct gives the same results as the anonymous function in that struct.
0 commentaires
Voir également
Catégories
En savoir plus sur Classification Learner App dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!