void mmEM::training(param myParam) { vector_2str wordX; vector_2str wordY; bool stillTrain = true; // reading file // readFileXY(myParam, myParam.inputFilename, &wordX, &wordY); // initialization // cout << "Initialization ... "; initialization(myParam, wordX, wordY); // maximization // cout << "Maximization ... " << endl; maximization(myParam); cout << endl; int atIter = 0; // still training // while (stillTrain) { atIter++; cout << "Iteration " << atIter << endl; // for each x and y pair // cout << "Expectation ... " ; for (int i = 0; i < wordX.size(); i++) { // expectation // expectation(myParam, wordX[i], wordY[i]); } cout << "Maximization ... "; long double totalChange = maximization(myParam); cout << "Total probability change = " << totalChange << endl; // stop by the probability change condition // if ((totalChange <= myParam.cutOff) && (myParam.cutOff < 1)) { stillTrain = false; } // stop by the number of iteration condition // if ((myParam.cutOff >= 1) && (atIter >= myParam.cutOff)) { stillTrain = false; } } cout << endl; }
void mmEM::computeDataProbs(param myParam) { vector_2str wordX; vector_2str wordY; vector_Double pair_probs; bool stillTrain = true; // reading file // readFileXY(myParam, myParam.inputFilename, &wordX, &wordY); vector<double> probs_vector(global_index), counts_vector(global_index), newprobs_vector(global_index),alpha_vector(global_index); cout << "Computing pair probs" << endl; // for each x and y pair // cout << "Expectation ... " ; cout << "There are " << wordX.size() << " pairs to be for which the probability is to be computed" << endl; string output_probs_file = myParam.outputFilename + ".probs"; cout << "Write the output probs to "<<output_probs_file<<endl; double alignCount = 0; double noAlignCount = 0; for (int i = 0; i < wordX.size(); i++) { // computing the p(y|x) for the particular xy pair// pair_probs.push_back(computePairProb(myParam, wordX[i], wordY[i])); } cout << "Write aligned data to : " << myParam.outputFilename << endl; cout << "Write un-aligned data to : " << myParam.outputFilename << ".err" << endl; ofstream PROBS; PROBS.open(output_probs_file.c_str()); for (int i = 0; i < wordX.size(); i++) { PROBS << join(wordX[i],0,wordX[i].size(),"") <<"\t"<< join(wordY[i],0,wordY[i].size(),"") <<"\t"<< pair_probs[i]<<endl; } PROBS.close(); }
void mmEM::createAlignments(param myParam) { vector_2str wordX; vector_2str wordY; double lessNbest = 0; //reading input file// readFileXY(myParam, myParam.inputFilename, &wordX, &wordY); cout << "There are " << wordX.size() << " pairs to be aligned" << endl; cout << "Write aligned data to : " << myParam.outputFilename << endl; cout << "Write un-aligned data to : " << myParam.outputFilename << ".err" << endl; ofstream ALIGNED, NOALIGNED; ALIGNED.open(myParam.outputFilename.c_str()); NOALIGNED.open(string(myParam.outputFilename + ".err").c_str()); double alignCount = 0; double noAlignCount = 0; for (unsigned int i = 0; i < wordX.size(); i++) { //vector<string> alignX, alignY; vector_2str nAlignX, nAlignY; vector<long double> nScore; nScore = nViterbi_align(myParam, wordX[i], wordY[i], nAlignX, nAlignY); //score = viterbi_align(myParam, wordX[i], wordY[i], &alignX, &alignY); if (nScore.size() > 0) { // found n-best alignments alignCount++; // count number of examples that have less than n alignment candidates // if (nScore.size() < myParam.nBest) { lessNbest++; cout << join(wordX[i],0, wordX[i].size(), "") << "\t" << join(wordY[i],0,wordY[i].size(), "") << "\t has " << nScore.size() << " alignments" << endl; } for (int nbest = 0; nbest < nScore.size(); nbest++) { for (unsigned int k = 0; k < nAlignX[nbest].size(); k++) { ALIGNED << nAlignX[nbest][k] << myParam.sepChar; } ALIGNED << "\t"; for (unsigned int k=0; k < nAlignY[nbest].size(); k++) { ALIGNED << nAlignY[nbest][k] << myParam.sepChar; } if (myParam.printScore) { ALIGNED << "\t" << nbest+1; ALIGNED << "\t" << nScore[nbest]; } ALIGNED << endl; } } else { // can't be aligned noAlignCount++; if (myParam.errorInFile) { ALIGNED << "NO ALIGNMENT \t" << endl; } NOALIGNED << join(wordX[i],0,wordX[i].size()," ") << "\t" << join(wordY[i], 0, wordY[i].size(), " ") << endl; } } ALIGNED.close(); NOALIGNED.close(); cout << "Aligned " << alignCount << " pairs" << endl; if (noAlignCount > 0) { cout << "No aligned " << noAlignCount << " pairs" << endl; } cout << "There are " << lessNbest << " example pairs having less than " << myParam.nBest << " alignment candidates" << endl; }
void mmEM::training(param myParam) { vector_2str wordX; vector_2str wordY; bool stillTrain = true; // reading file // readFileXY(myParam, myParam.inputFilename, &wordX, &wordY); // initialization // cout << "Initialization ... "; initialization(myParam, wordX, wordY); // maximization // cout << "Maximization ... " << endl; maximization(myParam); // lhuang: M-step after initialize (count = 1) cout << endl; int atIter = 0; // still training // global_index--; vector<double> probs_vector(global_index), counts_vector(global_index), newprobs_vector(global_index),alpha_vector(global_index); while (stillTrain) { atIter++; cout << "Iteration " << atIter << endl; // for each x and y pair // cout << "Expectation ... " ; for (int i = 0; i < wordX.size(); i++) { // expectation // expectation(myParam, wordX[i], wordY[i]); } if (!myParam.ashish) { cout << "Maximization ... "; long double totalChange = maximization(myParam); // lhuang: real M-step cout << "Total probability change = " << totalChange << endl; // stop by the probability change condition // if ((totalChange <= myParam.cutOff) && (myParam.cutOff < 1)) { stillTrain = false; } } else { cout << "Ashish" ; double sum_probs = 0; for(hash_2StrDouble::iterator pos = index.begin(); pos != index.end(); pos++) { for (hash_StrDouble::iterator pos2 = (pos->second).begin(); pos2 != (pos->second).end(); pos2++) { counts_vector[index[pos->first][pos2->first]-1] = counts[pos->first][pos2->first]; // cout << pos->first << " "<< pos2->first << " " // << counts_vector[index[pos->first][pos2->first]-1] << endl; probs_vector[index[pos->first][pos2->first]-1] = probs[pos->first][pos2->first]; alpha_vector[index[pos->first][pos2->first]-1] = alphas[pos->first][pos2->first]; sum_probs += probs_vector[index[pos->first][pos2->first]-1]; } } cout << "sum " << sum_probs <<endl; // getchar(); sum_probs = 0; for (int i=0; i<newprobs_vector.size(); i++) { // cout << counts_vector[i] << " " << probs_vector[i] << " "; sum_probs += probs_vector[i]; } cout << "sum vector " << sum_probs <<endl; // getchar(); projectedGradientDescentWithArmijoRule(counts_vector, probs_vector, newprobs_vector,alpha_vector,myParam.beta,myParam.pgdIter); for(hash_2StrDouble::iterator pos = index.begin(); pos != index.end(); pos++) { for (hash_StrDouble::iterator pos2 = (pos->second).begin(); pos2 != (pos->second).end(); pos2++) { // cout << pos->first << " "<< pos2->first << " " // << probs[pos->first][pos2->first] << " " // << counts[pos->first][pos2->first] << " " // << newprobs_vector[index[pos->first][pos2->first]-1] << endl; probs[pos->first][pos2->first] = newprobs_vector[index[pos->first][pos2->first]-1]; } } // for (int i=0; i<newprobs_vector.size(); i++) // { // cout << newprobs_vector[i] << " " ; // } cout<< endl; // getchar(); // clean counts // counts.clear(); } // stop by the number of iteration condition // if ((myParam.cutOff >= 1) && (atIter >= myParam.cutOff)) { stillTrain = false; } } cout << endl; }