Wrong output from network function, exported from nprtool

3 次查看(过去 30 天)
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.

采纳的回答

Umar
Umar 2024-10-10

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 个评论
Thomas
Thomas 2024-10-10
Thank you, didnt realise that it would be returning a probability, very neat
Umar
Umar 2024-10-10
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.

请先登录,再进行评论。

更多回答(2 个)

Aneela
Aneela 2024-10-10
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 2024-10-10
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!

类别

Help CenterFile Exchange 中查找有关 Pattern Recognition and Classification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by