Is it possible to translate a trained Neural Network to C++? I have this code below with one .xlsx input file. I would like to build the trained ANN model in my C++ code. Thanks guys for help! Balazs

4 Ansichten (letzte 30 Tage)
inputs = xlsread('Alloy 600 CGR data_1.xlsx','sheet3','Q8:X163');
targets = xlsread('Alloy 600 CGR data_1.xlsx','sheet3','Y8:Y163');
inputs=inputs';
targets=targets';
net = feedforwardnet(16);
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net.trainFcn = 'trainlm'; % Levenberg-Marquardt
net.performFcn = 'mse'; % Mean squared errorn
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
'plotregression', 'plotfit'};
% Train the Network
[net,tr] = train(net,inputs,targets); %ĘąÓĂ´¦ŔíşóµÄĘäČë±äÁżşÍĘäłö±äÁżŃµÁ·ÍřÂç
% Test the Network
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);
% Recalculate Training, Validation and Test Performance
trainTargets = targets .* tr.trainMask{1};
valTargets = targets .* tr.valMask{1};
testTargets = targets .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs);
valPerformance = perform(net,valTargets,outputs);
testPerformance = perform(net,testTargets,outputs);

Antworten (1)

Sourabh
Sourabh am 19 Feb. 2025
Yes, it is possible to generate C++ code from an already trained neural network using MATLAB Coder.
Follow the steps below:
1. Create a function in MATLAB that loads the pre-trained network and predicts the output.
function out = my_predict(in) %#codegen
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork(net); %assuming your pre-trained network is stored in “net” variable
end
% pass the input “in”
out = predict(mynet,in);
2. To produce C++ code, set the Code Generation Configuration object.
cfg = coder.config('lib');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig(TargetLibrary='none');
3. Finally, run the codegen command. Use the -config option to specify the configuration object and -args option to specify the input.
codegen -config cfg my_predict -args {myInput}
Kindly refer the following MATLAB Coder documentation:
I hope this helps you!

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by