## seeking advice with rounding NN output and vec2ind

### John (view profile)

on 15 Jul 2012
Latest activity Answered by Greg Heath

### Greg Heath (view profile)

on 16 Apr 2016

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

## Products

No products are associated with this question.

### Walter Roberson (view profile)

on 15 Jul 2012

Replace the vec2ind() with matrix multiplication.

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

### Greg Heath (view profile)

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

### Greg Heath (view profile)

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)

Vikas

### Vikas (view profile)

on 15 Apr 2016

But in the case after using softmax/logsig transfer function there is a possibility to get equal values[0.5,0.5] for two class problem or [0.25, 0.25, 0.25, 0.25] for 4 class problems, in that case what vec2ind(output) will do? it is in the document that it will go in the first class. Is it not be incorrect, if modeller change the class order? Kindly share your wisdom. so vec2ind([0.25;0.25;0]) = 1; but vec2ind([0;0.25;0.25]) = 2 if we change the order of the class output.

### Greg Heath (view profile)

on 16 Apr 2016

There are two binary conditions (four combinations) to consider

1. Type of targets

```   a. Mutually exclusive targets:
{0,1} unit vector
b. Non-mutually exclusive targets:
unit sum non-negative element vector ```

2. Type of classification

```   a. Forced classification: At least
one class is always chosen
b. Conditional classification: No
class is chosen if the maximum
output is less than a specified
threshold.```

YOU have to consider which combination you will use.

Hope this helps.

Greg

#### Join the 15-year community celebration.

Play games and win prizes!

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