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!