void Backpropagation::trainOnlineCV(Mlp& network, MATRIX& trainingInputs, VECTOR& trainingTargets, MATRIX& testInputs, VECTOR& testTargets) { VECTOR trainingOutputs(trainingTargets.size(), 0.0); VECTOR testOutputs(testTargets.size(), 0.0); while(error > tolerance && testCount < maxTestCount) { VECTOR::iterator output = trainingOutputs.begin(); VECTOR::iterator target = trainingTargets.begin(); for(MATRIX::iterator input = trainingInputs.begin(); input != trainingInputs.end(); ++input, ++target, ++output) { *output = network(*input); double err = *output - *target; getWeightUpdates(network, *input, err); applyWeightUpdates(network); ++iteration; if(iteration >= maxIterations) break; } ++epoch; error = mse(trainingTargets, trainingOutputs); // Early-stopping using test (cross-validation) error testOutputs = network(testInputs); testError = mse(testTargets, testOutputs); if(testError < minTestError) { // Preserve test error and network weights minTestError = testError; W = network.W; V = network.V; biasW = network.biasW; biasV = network.biasV; testCount = 0; } else { ++testCount; } } network.W = W; network.V = V; network.biasW = biasW; network.biasV = biasV; testError = minTestError; }
void AutoSubClassingClassifier::train( const vector<Mat>& segmentedHands, const vector<int>& expectedClass) { assert(segmentedHands.size() == expectedClass.size()); this->subclassesOfTrainingBase.clear(); int numberOfClasses = *max_element(expectedClass.begin(), expectedClass.end()) + 1; vector<Mat> samplesPerClass(numberOfClasses); for (int i = 0; i < segmentedHands.size(); i++) { Mat currentCaracteristicVector = this->caracteristicVector(segmentedHands[i]); if (samplesPerClass[expectedClass[i]].empty()) { samplesPerClass[expectedClass[i]] = currentCaracteristicVector; } else { vconcat(samplesPerClass[expectedClass[i]], currentCaracteristicVector, samplesPerClass[expectedClass[i]]); } } int accumulatedNumberOfClasses = 0; for (int i = 0; i < samplesPerClass.size(); i++) { Mat labels; int clusterCount = this->numberOfSubclasses[i]; kmeans(samplesPerClass[i], clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 0.0001, 10000), 5, KMEANS_RANDOM_CENTERS); this->subclassesOfTrainingBase.push_back(labels + (Mat::ones(samplesPerClass[i].rows, 1, CV_32S) * accumulatedNumberOfClasses)); accumulatedNumberOfClasses += this->numberOfSubclasses[i]; } Mat trainingInputs(segmentedHands.size(), this->caracteristicVectorLength(), CV_32F); Mat trainingOutputs(segmentedHands.size(), 1, CV_32F); int firstIndex = 0; for (int i = 0; i < samplesPerClass.size(); i++) { int lastIndex = firstIndex + samplesPerClass[i].rows; Mat samples = samplesPerClass[i]; Mat range = trainingInputs.rowRange(firstIndex, lastIndex); samples.copyTo(range); Mat outputs = this->subclassesOfTrainingBase[i]; outputs.copyTo(trainingOutputs.rowRange(firstIndex, lastIndex)); firstIndex = lastIndex; } this->statisticalModel->train(trainingInputs, trainingOutputs); }