double Sample::getError(const Feature &feature) const {
    return (getPrediction(feature) != isMale()) * getWeight();
}
Ejemplo n.º 2
0
void Trainer::finalize(int state, std::string& fileName,  bool isTest)
 {
     Method bestMethod = Method::PITCH;

     const char* genderStr = NULL ;
     double mfccSum =std::accumulate(resultMFFC.begin(),resultMFFC.end(),0.0);
     double pitchSum =std::accumulate(resultPitch.begin(),resultPitch.end(),0.0);
     double pGramsSum =std::accumulate(resultGrams.begin(),resultGrams.end(),0.0);
     double pGeneralSum =std::accumulate(resultGeneral.begin(),resultGeneral.end(),0.0);

     for (int k = 0; k < NUMBER_OF_PEOPLE; k++)
     {
         resultMFFC[k] = resultMFFC[k] / mfccSum;
         resultPitch[k] = resultPitch[k] / pitchSum;
         resultGrams[k] = resultGrams[k] / pGramsSum;
         resultGeneral[k] = resultGeneral[k] / pGeneralSum;
     }
     auto maxPitch = std::max_element(resultPitch.begin(), resultPitch.end());
     bool isFemale = !isMale(maxPitch);


     auto maxMFCC = isFemale ? std::max_element(resultMFFC.begin(), resultMFFC.end()) :
                        std::max_element(resultMFFC.begin()+3, resultMFFC.end());

     auto maxGrams = isFemale ? std::max_element(resultGrams.begin(), resultGrams.end()) :
                              std::max_element(resultGrams.begin()+3, resultGrams.end());

     auto maxGeneral = isFemale ? std::max_element(resultGeneral.begin(), resultGeneral.end()) :
                              std::max_element(resultGeneral.begin()+3, resultGeneral.end());

     if (isTest)
     {
         totalGuess++;
         auto pr = std::max_element(methodTruth[state].begin(), methodTruth[state].end(),
               [](const std::pair<Method, size_t>& p1, const std::pair<Method, size_t>& p2) {
                 return p1.second < p2.second; });
         bestMethod = pr->first;

         if (bestMethod == Method::MFCC)
         {
             genderStr = determineGender(resultMFFC.begin(), maxMFCC, state);
         }
         else if (bestMethod == Method::PITCH)
         {
             genderStr = determineGender(resultPitch.begin(), maxPitch, state);
         }
         else if (bestMethod == Method::PGRAMS)
         {
             genderStr = determineGender(resultGrams.begin(), maxGrams, state);
         }
         else if (bestMethod == Method::ALL_TRAINER)
         {
             genderStr = determineGender(resultGeneral.begin(), maxGrams, state);
         }
     }
     else
     {
         if (std::distance(resultMFFC.begin(), maxMFCC) == state)
         {
             methodTruth[state][Method::MFCC]++;
         }
         if (std::distance(resultPitch.begin(), maxPitch) == state)
         {
             methodTruth[state][Method::PITCH]++;
         }
         if (std::distance(resultGrams.begin(), maxGrams) == state)
         {
             methodTruth[state][Method::PGRAMS]++;
         }
         if (std::distance(resultGeneral.begin(), maxGeneral ) == state)
         {
             methodTruth[state][Method::ALL_TRAINER]++;
         }
     }



     outputFile << "*********************************************" << std::endl;
     outputFile <<  "The real excepted was state: " << state << "fileName " << fileName << std::endl;
     if (isTest)
     {
         outputFile <<  "Best method for this file is " << method2Str(bestMethod) << std::endl;
         if (genderStr != NULL)
             outputFile <<  "Gender is " << genderStr << std::endl;
     }

     outputFile <<  "MFCC Result was: " << std::distance(resultMFFC.begin(), maxMFCC) << std::endl;
     outputFile <<  "Pitch Result was: " << std::distance(resultPitch.begin(), maxPitch) << std::endl;
     outputFile <<  "PGRAM Result was: " << std::distance(resultGrams.begin(), maxGrams) << std::endl;
     outputFile <<  "Learner Result was: " << std::distance(resultGeneral.begin(), maxGeneral) << std::endl;

     outputFile << "*** All results ***" << std::endl;
     for (auto elem : resultMFFC)
         outputFile << elem << std::endl;

     outputFile << "*** Pitch ***" << std::endl;
     for (auto elem : resultPitch)
         outputFile << elem << std::endl;

     outputFile << "*** PGrams ***" << std::endl;
     for (auto elem : resultGrams)
         outputFile << elem << std::endl;

     outputFile << "*** Learner ***" << std::endl;
     for (auto elem : resultGeneral)
         outputFile << elem << std::endl;


     outputFile << "*********************************************" << std::endl;
     outputFile.flush();
 }