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