Float HierarchicalClustering::computeClusterVariance( const ClusterInfo &cluster, const MatrixFloat &data ){ VectorFloat mean(N,0); VectorFloat std(N,0); //Compute the mean UINT numSamples = cluster.getNumSamplesInCluster(); for(UINT j=0; j<N; j++){ for(UINT i=0; i<numSamples; i++){ UINT index = cluster[i]; mean[j] += data[ index ][j]; } mean[j] /= Float( numSamples ); } //Compute the std dev for(UINT j=0; j<N; j++){ for(UINT i=0; i<numSamples; i++){ std[j] += grt_sqr( data[ cluster[i] ][j] - mean[j] ); } std[j] = grt_sqrt( std[j] / Float( numSamples-1 ) ); } Float variance = 0; for(UINT j=0; j<N; j++){ variance += std[j]; } return variance/N; }
Float VectorFloat::getStdDev() const { Float mean = getMean(); Float stdDev = 0.0; const size_type N = this->size(); const Float *data = getData(); for(size_type i=0; i<N; i++){ stdDev += grt_sqr(data[i]-mean); } stdDev = grt_sqrt( stdDev / Float(N-1) ); return stdDev; }
Float KMeans::calculateTheta(const MatrixFloat &data){ Float theta = 0; Float sum = 0; UINT m,n,k = 0; for(m=0; m < numTrainingSamples; m++){ k = assign[m]; sum = 0; for(n=0; n < numInputDimensions; n++){ sum += grt_sqr(clusters[k][n] - data[m][n]); } theta += grt_sqrt(sum); } theta /= numTrainingSamples; return theta; }
bool SelfOrganizingMap::train_( MatrixFloat &data ){ //Clear any previous models clear(); const UINT M = data.getNumRows(); const UINT N = data.getNumCols(); numInputDimensions = N; numOutputDimensions = numClusters; Random rand; //Setup the neurons neurons.resize( numClusters ); if( neurons.size() != numClusters ){ errorLog << "train_( MatrixFloat &data ) - Failed to resize neurons Vector, there might not be enough memory!" << std::endl; return false; } for(UINT j=0; j<numClusters; j++){ //Init the neuron neurons[j].init( N, 0.5 ); //Set the weights as a random training example neurons[j].weights = data.getRowVector( rand.getRandomNumberInt(0, M) ); } //Setup the network weights switch( networkTypology ){ case RANDOM_NETWORK: networkWeights.resize(numClusters, numClusters); //Set the diagonal weights as 1 (as i==j) for(UINT i=0; i<numClusters; i++){ networkWeights[i][i] = 1; } //Randomize the other weights UINT indexA = 0; UINT indexB = 0; Float weight = 0; for(UINT i=0; i<numClusters*numClusters; i++){ indexA = rand.getRandomNumberInt(0, numClusters); indexB = rand.getRandomNumberInt(0, numClusters); //Make sure the two random indexs are the same (as this is a diagonal and should be 1) if( indexA != indexB ){ //Pick a random weight between these two neurons weight = rand.getRandomNumberUniform(0,1); //The weight betwen neurons a and b is the mirrored networkWeights[indexA][indexB] = weight; networkWeights[indexB][indexA] = weight; } } break; } //Scale the data if needed ranges = data.getRanges(); if( useScaling ){ for(UINT i=0; i<M; i++){ for(UINT j=0; j<numInputDimensions; j++){ data[i][j] = scale(data[i][j],ranges[j].minValue,ranges[j].maxValue,0,1); } } } Float error = 0; Float lastError = 0; Float trainingSampleError = 0; Float delta = 0; Float minChange = 0; Float weightUpdate = 0; Float weightUpdateSum = 0; Float alpha = 1.0; Float neuronDiff = 0; UINT iter = 0; bool keepTraining = true; VectorFloat trainingSample; Vector< UINT > randomTrainingOrder(M); //In most cases, the training data is grouped into classes (100 samples for class 1, followed by 100 samples for class 2, etc.) //This can cause a problem for stochastic gradient descent algorithm. To avoid this issue, we randomly shuffle the order of the //training samples. This random order is then used at each epoch. for(UINT i=0; i<M; i++){ randomTrainingOrder[i] = i; } std::random_shuffle(randomTrainingOrder.begin(), randomTrainingOrder.end()); //Enter the main training loop while( keepTraining ){ //Update alpha based on the current iteration alpha = Util::scale(iter,0,maxNumEpochs,alphaStart,alphaEnd); //Run one epoch of training using the online best-matching-unit algorithm error = 0; for(UINT i=0; i<M; i++){ trainingSampleError = 0; //Get the i'th random training sample trainingSample = data.getRowVector( randomTrainingOrder[i] ); //Find the best matching unit Float dist = 0; Float bestDist = grt_numeric_limits< Float >::max(); UINT bestIndex = 0; for(UINT j=0; j<numClusters; j++){ dist = neurons[j].getSquaredWeightDistance( trainingSample ); if( dist < bestDist ){ bestDist = dist; bestIndex = j; } } //Update the weights based on the distance to the winning neuron //Neurons closer to the winning neuron will have their weights update more for(UINT j=0; j<numClusters; j++){ //Update the weights for the j'th neuron weightUpdateSum = 0; neuronDiff = 0; for(UINT n=0; n<N; n++){ neuronDiff = trainingSample[n] - neurons[j][n]; weightUpdate = networkWeights[bestIndex][j] * alpha * neuronDiff; neurons[j][n] += weightUpdate; weightUpdateSum += neuronDiff; } trainingSampleError += grt_sqr( weightUpdateSum ); } error += grt_sqrt( trainingSampleError / numClusters ); } //Compute the error delta = fabs( error-lastError ); lastError = error; //Check to see if we should stop if( delta <= minChange ){ converged = true; keepTraining = false; } if( grt_isinf( error ) ){ errorLog << "train_(MatrixFloat &data) - Training failed! Error is NAN!" << std::endl; return false; } if( ++iter >= maxNumEpochs ){ keepTraining = false; } trainingLog << "Epoch: " << iter << " Squared Error: " << error << " Delta: " << delta << " Alpha: " << alpha << std::endl; } numTrainingIterationsToConverge = iter; trained = true; return true; }
bool GMM::train_(ClassificationData &trainingData){ //Clear any old models clear(); if( trainingData.getNumSamples() == 0 ){ errorLog << "train_(ClassificationData &trainingData) - Training data is empty!" << std::endl; return false; } //Set the number of features and number of classes and resize the models buffer numInputDimensions = trainingData.getNumDimensions(); numClasses = trainingData.getNumClasses(); models.resize(numClasses); if( numInputDimensions >= 6 ){ warningLog << "train_(ClassificationData &trainingData) - The number of features in your training data is high (" << numInputDimensions << "). The GMMClassifier does not work well with high dimensional data, you might get better results from one of the other classifiers." << std::endl; } //Get the ranges of the training data if the training data is going to be scaled ranges = trainingData.getRanges(); if( !trainingData.scale(GMM_MIN_SCALE_VALUE, GMM_MAX_SCALE_VALUE) ){ errorLog << "train_(ClassificationData &trainingData) - Failed to scale training data!" << std::endl; return false; } //Fit a Mixture Model to each class (independently) for(UINT k=0; k<numClasses; k++){ UINT classLabel = trainingData.getClassTracker()[k].classLabel; ClassificationData classData = trainingData.getClassData( classLabel ); //Train the Mixture Model for this class GaussianMixtureModels gaussianMixtureModel; gaussianMixtureModel.setNumClusters( numMixtureModels ); gaussianMixtureModel.setMinChange( minChange ); gaussianMixtureModel.setMaxNumEpochs( maxIter ); if( !gaussianMixtureModel.train( classData.getDataAsMatrixFloat() ) ){ errorLog << "train_(ClassificationData &trainingData) - Failed to train Mixture Model for class " << classLabel << std::endl; return false; } //Setup the model container models[k].resize( numMixtureModels ); models[k].setClassLabel( classLabel ); //Store the mixture model in the container for(UINT j=0; j<numMixtureModels; j++){ models[k][j].mu = gaussianMixtureModel.getMu().getRowVector(j); models[k][j].sigma = gaussianMixtureModel.getSigma()[j]; //Compute the determinant and invSigma for the realtime prediction LUDecomposition ludcmp( models[k][j].sigma ); if( !ludcmp.inverse( models[k][j].invSigma ) ){ models.clear(); errorLog << "train_(ClassificationData &trainingData) - Failed to invert Matrix for class " << classLabel << "!" << std::endl; return false; } models[k][j].det = ludcmp.det(); } //Compute the normalize factor models[k].recomputeNormalizationFactor(); //Compute the rejection thresholds Float mu = 0; Float sigma = 0; VectorFloat predictionResults(classData.getNumSamples(),0); for(UINT i=0; i<classData.getNumSamples(); i++){ VectorFloat sample = classData[i].getSample(); predictionResults[i] = models[k].computeMixtureLikelihood( sample ); mu += predictionResults[i]; } //Update mu mu /= Float( classData.getNumSamples() ); //Calculate the standard deviation for(UINT i=0; i<classData.getNumSamples(); i++) sigma += grt_sqr( (predictionResults[i]-mu) ); sigma = grt_sqrt( sigma / (Float(classData.getNumSamples())-1.0) ); sigma = 0.2; //Set the models training mu and sigma models[k].setTrainingMuAndSigma(mu,sigma); if( !models[k].recomputeNullRejectionThreshold(nullRejectionCoeff) && useNullRejection ){ warningLog << "train_(ClassificationData &trainingData) - Failed to recompute rejection threshold for class " << classLabel << " - the nullRjectionCoeff value is too high!" << std::endl; } //cout << "Training Mu: " << mu << " TrainingSigma: " << sigma << " RejectionThreshold: " << models[k].getNullRejectionThreshold() << std::endl; //models[k].printModelValues(); } //Reset the class labels classLabels.resize(numClasses); for(UINT k=0; k<numClasses; k++){ classLabels[k] = models[k].getClassLabel(); } //Resize the rejection thresholds nullRejectionThresholds.resize(numClasses); for(UINT k=0; k<numClasses; k++){ nullRejectionThresholds[k] = models[k].getNullRejectionThreshold(); } //Flag that the models have been trained trained = true; return true; }