How can get the code in c by Matlab coder ?
조회 수: 1 (최근 30일)
이전 댓글 표시
Dear Expert,
I want to convert ANN model to C code file. I develpoed a ANN model that predict one output correspondin to some input, so we have weight, bias, network and input parameters. could you suggest me how can i convert this ANN model to C code by Matlab coder.
I tried tp convert it but error is coming: 1.spreadsheetimportoption is not supported for code generation 2. setvaropts is not supported 3. readable is not supported 4. training is not supported 5. The test is not supported 6. fitness is not supported (3) Additionally, when I attempt to upload the Matlab function in "c coder," the input parameter needs to be manually selected, as the coder is not automatically extracting it from the code.
Code is:
function Nu = NUprediction(Re, Pr, alpha_g)
double Re,
double Pr,
double alpha_g,
NUprediction = readtable("C:\Users\hp\Desktop\Code_VTU_Read\Final_Nu\New folder\NU_prediction.xlsx", opts, "UseExcel", false);
M= NUprediction;
Input=table2array([M(:,1) M(:,2) M(:,3)]);
Output=table2array(M(:,4));
c = cvpartition(length(Output),"Holdout",0.20);
trainingIdx = training(c); % Indices for the training set
XTrain = Input(trainingIdx,:);
YTrain = ((Output(trainingIdx)));
testIdx = test(c); % Indices for the test set
XTest = (Input(testIdx,:));
YTest = (Output(testIdx));
%%
Mdl = fitrnet(XTrain,YTrain,"Standardize",true, "LayerSizes",[30 30 40 10]);
%%
testMSE = loss(Mdl,XTest,YTest)
%%
testPredictions = predict(Mdl,XTest);
%%
trainMSE = loss(Mdl,XTrain,YTrain)
trainPredictions = predict(Mdl,XTrain);
%%
CFD=M;
Input_CFD=table2array([M(:,1) M(:,2) M(:,3)]);
Output_CFD=table2array([M(:,4)]);
% Predict the output
predicted_output = predict(Mdl, Input_CFD);
댓글 수: 1
Walter Roberson
2024년 1월 4일
double Re,
double Pr,
double alpha_g,
MATLAB itself does not have declarations of variables.
Those lines are not illegal, but they do not do anything useful. Those lines are equivalent to
double('Re')
double('Pr')
double('alpha_g')
which cause the internal character codes to be displayed -- so you would get stray
82 101
80 114
97 108 112 104 97 95 103
displayed every time the function was executed.
답변 (1개)
Venu
2024년 1월 4일
편집: Venu
2024년 1월 4일
In the given code, functions like 'readtable', 'spreadsheetimportoption', 'setvaropts' are not supported for code generation. You need to load your data outside of the MATLAB function and pass it directly as an argument to the function.
Machine learning functions such as 'fitrnet', 'cvpartition', 'loss', and 'predict' are not directly supported for code generation. You need to export the trained model using 'savecompactmodel' and then write a custom prediction function using the model's weights and biases.
In a brief, to proceed with code generation, you would need to:
- Train your ANN model outside the code generation process.
- Save the trained model parameters (weights and biases) in a format that you can use in a custom prediction function.
- Rewrite the 'NUprediction' function to accept the trained model parameters and input data directly, without performing any unsupported operations.
- Implement the prediction logic manually, using matrix operations that are supported by MATLAB Coder.
The input arguments 'Re', 'Pr', and 'alpha_g' for 'NUprediction' function are indeed defined with their types specified as double. However, in the context of MATLAB Coder, you need to provide additional information about the size and shape of the inputs when they are arrays or matrices.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!