Ask Your Question
0

Set number of output nodes of Artificial Neural Network to a value other than 1

asked 2016-06-05 07:44:48 -0600

Angulu gravatar image

I am working on a project where I need to classify images using Artificial Neural Network (ANN). I have successfully trained ANN with 1 output node as shown in code below. How can I set number of output nodes to another value other than 1 (e.g. 50). Kind regards

trainANN(Mat hists, vector<int> labels)
{
    int cols = hists.cols;//size of input layer must be equal to this number of cols
    int rows = hists.rows;
    Mat responses(labels);//create a matrix of responses from vector labels
    responses.convertTo(responses,  CV_32FC1);//convert responses (labels) from int to float
    Ptr<TrainData> trainData = TrainData::create(hists, ROW_SAMPLE, responses);//create training data
    Ptr<ANN_MLP> ann = ml::ANN_MLP::create();
    vector<int> layers = { cols,1000,500,1 };//layers
    ann->setLayerSizes(layers);//set the layer sizes(SIZES)
    ann->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
    cout << "Training ANN..." << endl;
    ann->train(trainData);
    bool trained = ann->isTrained();
    if (trained)
    {
        cout << "Artificial Neural Network Trained"<<endl;
        ann->save(".\\Trained Models\\ANN.xml");
    }
}
edit retag flag offensive close merge delete

Comments

think of how the sigmoid activation works, and you'll see, that only 0 and 1 are valid values for a label, and that you also will need N output nodes for N classes

berak gravatar imageberak ( 2016-06-06 00:02:41 -0600 )edit

Thank you Berak. I have 60 classes and 10 images per class. I have a one dimensional matrix of labels with values between 1 and 60. I have a clue: which is to make this labels matrix a 600 by 60 Mat with my labels as 1 or 0 in columns. I would like to request how help on how to make this binary matrix of labels

class1Image1 1 0 0 0 ..........0
class1Image2 1 0 0 0 ..........0
class2Image1 0 1 0 0...........0
class3Image1 0 0 1 0...........0
class3Image2 0 0 1 0...........0
classNImageN 0 0 0 0..........1
Angulu gravatar imageAngulu ( 2016-06-06 00:48:57 -0600 )edit

^yes, exactly. it's called "one-hot encoding".

berak gravatar imageberak ( 2016-06-06 01:22:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-06-06 01:08:21 -0600

berak gravatar image

updated 2016-06-06 01:14:17 -0600

for 60 classes, you will need 60 (binary) output nodes in your network, and your responses could be created like this:

int N = numImages; // (per class, 10 in your case)
int C = numClasses; // 60 here
Mat_<float> responses(N*C, C, 0.0f); //all 0 initially
for (int j=0; j<C; j++)
{
  for (int i=0; i<N; i++)
  {
     responses(j*N+i, j) = 1.0f;
  } 
}

for N=3 and C=6, it looks like this:

[1, 0, 0, 0, 0, 0;
 1, 0, 0, 0, 0, 0;
 1, 0, 0, 0, 0, 0;
 0, 1, 0, 0, 0, 0;
 0, 1, 0, 0, 0, 0;
 0, 1, 0, 0, 0, 0;
 0, 0, 1, 0, 0, 0;
 0, 0, 1, 0, 0, 0;
 0, 0, 1, 0, 0, 0;
 0, 0, 0, 1, 0, 0;
 0, 0, 0, 1, 0, 0;
 0, 0, 0, 1, 0, 0;
 0, 0, 0, 0, 1, 0;
 0, 0, 0, 0, 1, 0;
 0, 0, 0, 0, 1, 0;
 0, 0, 0, 0, 0, 1;
 0, 0, 0, 0, 0, 1;
 0, 0, 0, 0, 0, 1]
edit flag offensive delete link more

Comments

Thank you. It is working well. Kind regards

Angulu gravatar imageAngulu ( 2016-06-06 11:45:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-05 07:44:48 -0600

Seen: 270 times

Last updated: Jun 06 '16