cc.mallet.grmm.inference.gbp.ClusterVariationalRegionGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mallet Show documentation
Show all versions of mallet Show documentation
MALLET is a Java-based package for statistical natural language processing,
document classification, clustering, topic modeling, information extraction,
and other machine learning applications to text.
The newest version!
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
http://www.cs.umass.edu/~mccallum/mallet
This software is provided under the terms of the Common Public License,
version 1.0, as published by http://www.opensource.org. For further
information, see the file `LICENSE' included with this distribution. */
package cc.mallet.grmm.inference.gbp;
import java.util.*;
import java.util.logging.Logger;
import cc.mallet.grmm.types.Factor;
import cc.mallet.grmm.types.FactorGraph;
import cc.mallet.grmm.types.UndirectedGrid;
import cc.mallet.grmm.types.Variable;
import cc.mallet.util.CollectionUtils;
import cc.mallet.util.MalletLogger;
/**
* Created: Jun 1, 2005
*
* @author = region.vars.size ()) {
if (r2.vars.containsAll (region.vars)) {
it.remove ();
break;
}
}
}
}
}
public static void addAllFactors (FactorGraph mdl, List regions)
{
for (Iterator it = regions.iterator (); it.hasNext ();) {
Region region = (Region) it.next ();
for (Iterator pIt = mdl.factorsIterator (); pIt.hasNext();) {
Factor ptl = (Factor) pIt.next ();
if (region.vars.containsAll (ptl.varSet ())) {
region.factors.add (ptl);
}
}
}
}
public static interface BaseRegionComputer {
/**
* Returns a list of top-level regions for use in the cluster variational method.
* @param mdl An undirected model.
* @return A list of regions. No region in the list may subsume another.
*/
List computeBaseRegions (FactorGraph mdl);
}
/**
* Region computer where each top-level region consists of a single factor node.
* If the model is pairwise, this is equivalent to using the Bethe free energy.
*/
public static class ByFactorRegionComputer implements BaseRegionComputer {
public List computeBaseRegions (FactorGraph mdl)
{
List regions = new ArrayList (mdl.factors ().size ());
for (Iterator it = mdl.factorsIterator (); it.hasNext ();) {
Factor ptl = (Factor) it.next ();
regions.add (new Region (ptl));
}
removeSubsumedRegions (regions);
addAllFactors (mdl, regions);
return regions;
}
}
public static class Grid2x2RegionComputer implements BaseRegionComputer {
public List computeBaseRegions (FactorGraph mdl)
{
List regions = new ArrayList ();
UndirectedGrid grid = (UndirectedGrid) mdl;
for (int x = 0; x < grid.getWidth() - 1; x++) {
for (int y = 0; y < grid.getHeight() - 1; y++) {
Variable[] vars = new Variable[] {
grid.get (x, y),
grid.get (x, y+1),
grid.get (x+1, y+1),
grid.get (x+1, y),
};
regions.add (new Region (vars, new Factor[0]));
}
}
addAllFactors (mdl, regions);
return regions;
}
}
}