I wrote a function to reproduce the neural network operation 'sim', why is my result inconsistent with sim's result?

3 次查看(过去 30 天)
load("file.mat")
net=results.Network;
xpoint=[1;2;3];
a1=sim(net,xpoint)
a1 = 2x1
5.0415 4.9795
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a2=useNetwork(net,xpoint)
a2 = 2x1
2.4724 1.7471
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function output=useNetwork(net,input)
l=tansig(net.IW{1}*input+net.b{1});
output=net.LW{2,1}*l+net.b{2};
end

回答(1 个)

Jaimin
Jaimin 2024-9-5
Hello @策 陈
In MATLAB, the neural network utilizes pre-processing and post-processing functions to handle the input and output data. You can adjust your parsing code as follows:
load("file.mat")
net=results.Network;
xpoint=[1;2;3];
a1=sim(net,xpoint)
a2=useNetwork(net,xpoint)
function output = useNetwork(net, input)
net_iw=net.IW{1,1};
net_lw=net.LW{2,1};
net_b1=net.b{1};
net_b2=net.b{2};
normalized_inputn_test = mapminmax('apply', input, net.inputs{1}.processSettings{1}); %we are applying the same pre-processing as in the net
hidden_layer_input =net_iw* normalized_inputn_test + repmat(net_b1, 1, size(input', 1)); % input layer to hidden layer
hidden_layer_output=tansig(hidden_layer_input);
output_layer_input = net_lw*hidden_layer_output+ repmat(net_b2, 1, size(hidden_layer_output', 1)); % hidden layer to output layer
output = mapminmax('reverse', output_layer_input, net.outputs{2}.processSettings{1});%we are applying the same post-processing as in the net
end
For further information, please consult the following MATLAB answer, which addresses a similar issue:
I hope this will be helpful.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by