I wrote a function to reproduce the neural network operation 'sim', why is my result inconsistent with sim's result?
3 views (last 30 days)
Show older comments
load("file.mat")
net=results.Network;
xpoint=[1;2;3];
a1=sim(net,xpoint)
a2=useNetwork(net,xpoint)
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 Comment
Answers (1)
Jaimin
on 5 Sep 2024
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.
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!