src/yang/weiwei/mtm/MTM.java
/**
	 * Sample for given number of iterations
	 * @param numIters Number of iterations
	 * @throws IOException IOException
	 */
Sample for given number of iterations
@param numIters Number of iterations
@throws IOException IOException
Sample for given number of iterations @param numIters Number of iterations @throws IOException IOException
public void sample(int numIters) throws IOException
	{
		for (int iteration=1; iteration<=numIters; iteration++)
		{
			for (int lang=0; lang<param.numLangs; lang++)
			{
				for (int doc=0; doc<numDocs[lang]; doc++)
				{
					sampleDoc(doc, lang, iteration>50);
				}
				computeLogLikelihood(lang);
				perplexity[lang]=Math.exp(-logLikelihood[lang]/numTestWords[lang]);
				if (param.verbose)
				{
					if (lang==0)
					{
						IOUtil.print("<"+iteration+">");
					}
					IOUtil.println("\tLang "+lang+" Log-LLD: "+format(logLikelihood[lang])+
							"\tPPX: "+format(perplexity[lang]));
				}
				if (param.updateAlpha && iteration%param.updateAlphaInterval==0 && type==TRAIN)
				{
					updateHyperParam(lang);
				}
				if (type==TRAIN)
				{
					computeOmega(lang);
					computeWordWeights(lang);
					updateRho(lang);
					computeTOmega(lang);
				}
			}
			if (param.verbose && type==TRAIN)
			{
				for (int l1=0; l1<param.numLangs; l1++)
				{
					for (int l2=0; l2<param.numLangs; l2++)
					{	
						if (l1==l2) continue;
						double avgEntropy=0.0;
						for (int t2=0; t2<param.numTopics[l2]; t2++)
						{
							double entropy=0.0;
							for (int t1=0; t1<param.numTopics[l1]; t1++)
							{
								if (rho[l1][l2][t1][t2]>0.0)
								{
									entropy-=rho[l1][l2][t1][t2]*Math.log(rho[l1][l2][t1][t2]);
								}
							}
							avgEntropy+=entropy;
						}
						IOUtil.println("\tAvg Entropy: "+avgEntropy/param.numTopics[l2]);
						double avgScore=0.0;
						double avgTopScore=0.0;
						for (int t1=0; t1<param.numTopics[l1]; t1++)
						{
							double max=Double.MIN_VALUE;
							for (int t2=0; t2<param.numTopics[l2]; t2++)
							{
								avgScore+=rho[l1][l2][t1][t2];
								if (rho[l1][l2][t1][t2]>max)
								{
									max=rho[l1][l2][t1][t2];
								}
							}
							avgTopScore+=max;
						}
						avgScore/=(double)param.numTopics[l1]*param.numTopics[l2];
						avgTopScore/=(double)param.numTopics[l1];
						IOUtil.println("\tAvg Link Score of Languages "+l1+" and "+l2+": "+avgScore);
						IOUtil.println("\tAvg Top Link Score of Languages "+l1+" and "+l2+": "+avgTopScore);
						IOUtil.println("\tRatio of Top to All of Languages "+l1+" and "+l2+": "+Math.abs(avgTopScore/avgScore));
					}
				}
			}
		}
		if (type==TRAIN && param.verbose)
		{
			for (int lang=0; lang<param.numLangs; lang++)
			{
				for (int topic=0; topic<param.numTopics[lang]; topic++)
				{
					IOUtil.println(topWordsByFreq(lang, topic, param.numTopWords));
				}
			}
		}
	}
public void sample ( int numIters ) throws IOException { for ( int iteration = 1 ; iteration <= numIters ; iteration ++ ) { for ( int lang = 0 ; lang < param . numLangs ; lang ++ ) { for ( int doc = 0 ; doc < numDocs [ lang ] ; doc ++ ) { sampleDoc ( doc , lang , iteration > 50 ) ; } computeLogLikelihood ( lang ) ; perplexity [ lang ] = Math . exp ( - logLikelihood [ lang ] / numTestWords [ lang ] ) ; if ( param . verbose ) { if ( lang == 0 ) { IOUtil . print ( "<" + iteration + ">" ) ; } IOUtil . println ( "\tLang " + lang + " Log-LLD: " + format ( logLikelihood [ lang ] ) + "\tPPX: " + format ( perplexity [ lang ] ) ) ; } if ( param . updateAlpha && iteration % param . updateAlphaInterval == 0 && type == TRAIN ) { updateHyperParam ( lang ) ; } if ( type == TRAIN ) { computeOmega ( lang ) ; computeWordWeights ( lang ) ; updateRho ( lang ) ; computeTOmega ( lang ) ; } } if ( param . verbose && type == TRAIN ) { for ( int l1 = 0 ; l1 < param . numLangs ; l1 ++ ) { for ( int l2 = 0 ; l2 < param . numLangs ; l2 ++ ) { if ( l1 == l2 ) continue ; double avgEntropy = 0.0 ; for ( int t2 = 0 ; t2 < param . numTopics [ l2 ] ; t2 ++ ) { double entropy = 0.0 ; for ( int t1 = 0 ; t1 < param . numTopics [ l1 ] ; t1 ++ ) { if ( rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] > 0.0 ) { entropy -= rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] * Math . log ( rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] ) ; } } avgEntropy += entropy ; } IOUtil . println ( "\tAvg Entropy: " + avgEntropy / param . numTopics [ l2 ] ) ; double avgScore = 0.0 ; double avgTopScore = 0.0 ; for ( int t1 = 0 ; t1 < param . numTopics [ l1 ] ; t1 ++ ) { double max = Double . MIN_VALUE ; for ( int t2 = 0 ; t2 < param . numTopics [ l2 ] ; t2 ++ ) { avgScore += rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] ; if ( rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] > max ) { max = rho [ l1 ] [ l2 ] [ t1 ] [ t2 ] ; } } avgTopScore += max ; } avgScore /= ( double ) param . numTopics [ l1 ] * param . numTopics [ l2 ] ; avgTopScore /= ( double ) param . numTopics [ l1 ] ; IOUtil . println ( "\tAvg Link Score of Languages " + l1 + " and " + l2 + ": " + avgScore ) ; IOUtil . println ( "\tAvg Top Link Score of Languages " + l1 + " and " + l2 + ": " + avgTopScore ) ; IOUtil . println ( "\tRatio of Top to All of Languages " + l1 + " and " + l2 + ": " + Math . abs ( avgTopScore / avgScore ) ) ; } } } } if ( type == TRAIN && param . verbose ) { for ( int lang = 0 ; lang < param . numLangs ; lang ++ ) { for ( int topic = 0 ; topic < param . numTopics [ lang ] ; topic ++ ) { IOUtil . println ( topWordsByFreq ( lang , topic , param . numTopWords ) ) ; } } } }
Sample for given number of iterations
 @param numIters Number of iterations
 @throws IOException IOException
Sample for given number of iterations @param numIters Number of iterations @throws IOException IOException