Wrong output from network function, exported from nprtool

3 views (last 30 days)
Trained a neural pattern recognition network (nprtool) using 4x23458 Matrix as the predictors. The target is boolean. I was happy with the results and exported the function into workspace:
function [y1] = myNeuralNetworkFunction(x1)
Now its returning double instead of boolean if i feed it with a sample of the training data. Im quite sure there is an easy fix to this. Really a novice in using neural network functions in matlab.
Thank you.

Accepted Answer

Umar
Umar on 10 Oct 2024

Hi @Thomas,

In MATLAB, neural networks often output continuous values in the range of [0, 1], especially when dealing with binary classification problems. This means that your network is likely returning probabilities, which need to be converted into boolean values (0 or 1) for your specific application. You can apply a simple threshold to convert the output from double to boolean. A common approach is to use 0.5 as the cutoff:

   function [y1] = myNeuralNetworkFunction(x1)
       % Get output from neural network
       output = neuralNetworkModel(x1); % Assuming neuralNetworkModel is your 
        trained model
       % Convert to boolean based on threshold
       y1 = output >= 0.5; % This will return true (1) for values >= 0.5 and 
       false (0) 
       otherwise
   end

If you prefer, you can explicitly convert the output to a logical type:

   y1 = logical(output >= 0.5);

For more information on logical function, please refer to

https://www.mathworks.com/help/matlab/ref/logical.html?s_tid=doc_ta

Make sure that your neural network is appropriately configured for binary classification. If you're using a sigmoid activation function in the final layer, this should naturally yield outputs between 0 and 1.

Hope this helps.

  2 Comments
Thomas
Thomas on 10 Oct 2024
Thank you, didnt realise that it would be returning a probability, very neat
Umar
Umar on 10 Oct 2024
Hi @Thomas,
Thank you for your thoughtful feedback. I’m glad to hear that you found the probability return feature intriguing. It’s always rewarding to know that our work resonates well with users. If you have any further questions or need additional clarification, please don’t hesitate to reach out. Your insights are invaluable as we continue to improve our offerings.

Sign in to comment.

More Answers (2)

Aneela
Aneela on 10 Oct 2024
Hi Thomas,
When you train a neural network for binary classification using MATLAB's Neural Pattern Recognition Tool (nprtool), the network outputs a continuous value. This value typically ranges between 0 and 1, representing the probability that the input belongs to the positive class.
To convert the continuous output to a Boolean value, you can apply a threshold.
output = myNeuralNetworkFunction(x1);
booleanOutput = output >= 0.5;
This operation will return 1 for values(probabilities) greater than or equal to 0.5 and 0 otherwise.
You can refer to the documentation below for more information on “nprtool”: https://www.mathworks.com/help/deeplearning/gs/pattern-recognition-with-a-shallow-neural-network.html

praguna manvi
praguna manvi on 10 Oct 2024
Hi @Thomas,
As I understand it, you are trying to predict the class using the trained patternnet loaded from workspace. To convert the double values from the network to class labels, you can use the vec2ind function and “ind2vec” to obtain a sparse vector output. Here is an example from the documentation of “patternnet:
y = net(x);
tind = vec2ind(t); % test outputs
yind = vec2ind(y);
percentErrors = sum(tind ~= yind) / numel(tind);`
For more information on how to use patternnet from the command line, please refer to this link:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!