How to make my output binary in pattern recognition ANN?
5 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a code for a pattern recognition ANN. The output is binary with 0 and 1 or 1 and 0 (two answers). The results of the model are not 0 and 1; they are decimal numbers (for example 0.3 and 0.6). How do I make my model outputs 0 and 1?
Here is the code:
x = It;
t = Ot;
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainscg'; % Scaled conjugate gradient backpropagation.
% This is the section of code that creates the layers. It is a shallow network because there is only 1 hidden layer
% Create a Pattern Recognition Network
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize, trainFcn);
% You can turn this into a deep learning network by adding more hidden layers. For example, this code would create a 3-layer network.
% Three hidden layer NN
hiddenLayerSize1 = 15;
hiddenLayerSize2 = 20;
hiddenLayerSize3 = 20;
hiddenLayerSize4 = 20;
hiddenLayerSize5 = 20;
hiddenLayerSize6 = 20;
hiddenLayerSize7 = 20;
hiddenLayerSize8 = 20;
hiddenLayerSize9 = 20;
net = patternnet([hiddenLayerSize1 hiddenLayerSize2 hiddenLayerSize3 hiddenLayerSize4 hiddenLayerSize5 hiddenLayerSize6 hiddenLayerSize7 hiddenLayerSize8 hiddenLayerSize9], trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
tind = vec2ind(t);
yind = vec2ind(y);
percentErrors = sum(tind ~= yind)/numel(tind);
0 件のコメント
採用された回答
Pratyush Swain
2024 年 2 月 9 日
Hi Mostafa,
It is not necessary for the outputs to contain only binary numbers, since the class depends upon the higher number among the decimal pair. For example [0.35 0.65] actually corresponds to [0 1].
The function "vec2ind" function converts these one-hot encoded vectors to scalar indices. It finds the position of the highest element in each column of the input matrix. It does not matter if the values are not exactly 0 or 1, "vec2ind" is only concerned with which element is the largest.
vec2ind([0.1 0.8; 0.9 0.2])
Finally we compare the "vec2ind" output of actual target class(t) and predicted class(y) to calculate percentage error.
If you want to explicitly convert the predicted output in form of 0 and 1, you can refer the workflow as follows:
% Test the Network %
% Predicted Output
y = net(x);
% Threshold parameter to separate the larger and smaller pair values which sum up to 1.
y = y > 0.5;
The above step will result in converting a sample output matrix like
[ 0.0040 0.9407 ... ===> [ 0 1 ...
0.9960 0.0593 ... ] 1 0 ... ]
For more information on pattern recognition ANN in MATLAB and "vec2ind" function, please refer to
Hope this helps.
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!