Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this needs "one-hot encoded" labels, 2 colums like [1,0] or [0,1] for -1 and 1 resp.

this the ANN needs "one-hot encoded" labels, 2 colums (one for each class) like [1,0] or [0,1] for -1 and 1 resp.

you could add a loop before the training like:

Mat one_hot(labels.rows, 2, CV_32F, 0.0f); // 2 classes, all 0
for (int i=0; i<labels.rows; i++) {
    int k = labels.at<int>(i); // careful with the type here !
    if (k==-1) one_hot.at<float>(0) = 1;
    else       one_hot.at<float>(1) = 1;
}

cv::Ptr<cv::ml::TrainData> trainingData = cv::ml::TrainData::create(data, cv::ml::SampleTypes::ROW_SAMPLE, one_hot); // instead of "labels"

please also have a look at this sample

(and yea, "output training data should be a floating-point matrix" is a bit misleading, it gets better if you think of it as "ground truth responses for the output", maybe ...)

the ANN needs "one-hot encoded" labels, 2 colums (one for each class) like [1,0] or [0,1] for -1 and 1 resp.

you could add a loop before the training to translate your current labels like:

Mat one_hot(labels.rows, 2, CV_32F, 0.0f); // 2 classes, all 0
for (int i=0; i<labels.rows; i++) {
    int k = labels.at<int>(i); // careful with the type here !
    if (k==-1) one_hot.at<float>(0) = 1;
    else       one_hot.at<float>(1) = 1;
}

cv::Ptr<cv::ml::TrainData> trainingData = cv::ml::TrainData::create(data, cv::ml::SampleTypes::ROW_SAMPLE, one_hot); // instead of "labels"

please also have a look at this sample

(and yea, "output training data should be a floating-point matrix" is a bit misleading, it gets better if you think of it as "ground truth responses for the output", maybe ...)