MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by John on 15 Jul 2012

Hi there,

I am using the following code to classify data with a neural network. I first classify the data with the network, then round the output and then convert the vector to an integer (i.e. 1,2,3).

output = sim(net, tmparray); outputrounded = round(output); result = vec2ind(outputrounded);

However, sometimes the NN cannot match the input to a particular target,so when the output is rounded they are all zeros, so when it is converted from a vector to an integer there is no output.

If this happens is there anyway to force the result to be a zero?

The reason I ask is because when you are classifying thousands of samples one after another and one sample cannot be classified and there is no result , it is very difficult to identify which input resulted in this.

Thank you

*No products are associated with this question.*

Answer by Walter Roberson on 15 Jul 2012

Replace the vec2ind() with matrix multiplication.

mvec = 1 : size(outputrounded,1); result = mvec(:) * outputrounded;

Answer by Greg Heath on 15 Jul 2012

For classification of c mutually exclusive classes, use a target matrix consisting of the columns of the c-dimensional unit matrix eye(c).

Use softmax as the output activation function. Then all outputs are in the OPEN interval (0,1) and sum to 1.

classindices = [ 4 2 3 1 2]

target = ind2vec(classindices)

target = full(target)

rng(0)

output = softmax(target+0.3*randn(4,5))

predclass = vec2ind(output)

Err = predclass~=classindices

If the classes are NOT mutually exclusive (e.g., tall,dark,handsome), use logsig as the output activation function. Then all outputs are in the OPEN interval (0,1) but do NOT sum to 1.

Outputs of exactly 0 or 1 are the results of floating point error.

rng(0)

target = round(rand(3,5))

output = logsig(target+0.4*randn(3,5))

predclasses = round(output)

Err = predclasses-target

Nerrs = sum(Err,2)

Hope this helps.

Greg

Greg Heath on 15 Jul 2012

Outputs of exactly 0 or 1 are the results of floating point ROUNDOFF.

Classification thresholds:

rng(0)

target = round(rand(3,5))

output = logsig(target+0.4*randn(3,5))

thresholds = [0.4 0.5 0.6]'

predclasses = bsxfun(@ge,output,thresholds)

Err = predclasses~=target

Nerrs = sum(Err,2)

## 0 Comments