public class GraphicalModel
extends java.lang.Object
A basic graphical model representation: Factors and Variables. This should be a fairly familiar interface to anybody who's taken a basic PGM course (eg https://www.coursera.org/course/pgm). The key points: - Stitching together feature factors - Attaching metadata to everything, so that different sections of the program can communicate in lots of unplanned ways. For now, the planned meta-data is a lot of routing and status information to do with LENSE.
This is really just the data structure, and inference lives elsewhere and must use public interfaces to access these models. We just provide basic utility functions here, and barely do that, because we pass through directly to maps wherever appropriate.
Modifier and Type | Class and Description |
---|---|
static class |
GraphicalModel.Factor
A single factor in this graphical model.
|
Modifier and Type | Field and Description |
---|---|
java.util.Set<GraphicalModel.Factor> |
factors |
java.util.Map<java.lang.String,java.lang.String> |
modelMetaData |
java.util.List<java.util.Map<java.lang.String,java.lang.String>> |
variableMetaData |
Constructor and Description |
---|
GraphicalModel() |
Modifier and Type | Method and Description |
---|---|
GraphicalModel.Factor |
addFactor(ConcatVectorTable featureTable,
int[] neighborIndices)
Creates an instantiated factor in this graph, with neighborIndices representing the neighbor variables by integer
index.
|
GraphicalModel.Factor |
addFactor(int[] neighborIndices,
int[] neighborDimensions,
java.util.function.Function<int[],ConcatVector> assignmentFeaturizer)
This is the preferred way to add factors to a graphical model.
|
GraphicalModel |
cloneModel()
The point here is to allow us to save a copy of the model with a current set of factors and metadata mappings,
which can come in super handy with gameplaying applications.
|
java.util.Map<java.lang.String,java.lang.String> |
getModelMetaDataByReference() |
GraphicalModelProto.GraphicalModel.Builder |
getProtoBuilder() |
java.util.Map<java.lang.String,java.lang.String> |
getVariableMetaDataByReference(int variableIndex)
Gets the metadata for a variable.
|
int[] |
getVariableSizes() |
static GraphicalModel |
readFromProto(GraphicalModelProto.GraphicalModel proto)
Recreates an in-memory GraphicalModel from a proto serialization, recursively creating all the ConcatVectorTable's
and ConcatVector's in memory as well.
|
static GraphicalModel |
readFromStream(java.io.InputStream stream)
Static function to deserialize a graphical model from an input stream.
|
java.lang.String |
toString()
Displays a list of factors, by neighbor.
|
boolean |
valueEquals(GraphicalModel other,
double tolerance)
Check that two models are deeply value-equivalent, down to the concat vectors inside the factor tables, within
some tolerance.
|
void |
writeToStream(java.io.OutputStream stream)
Writes the protobuf version of this graphical model to a stream.
|
public java.util.Map<java.lang.String,java.lang.String> modelMetaData
public java.util.List<java.util.Map<java.lang.String,java.lang.String>> variableMetaData
public java.util.Set<GraphicalModel.Factor> factors
public java.util.Map<java.lang.String,java.lang.String> getModelMetaDataByReference()
public java.util.Map<java.lang.String,java.lang.String> getVariableMetaDataByReference(int variableIndex)
variableIndex
- the variable number, 0 indexed, to retrievepublic GraphicalModel.Factor addFactor(int[] neighborIndices, int[] neighborDimensions, java.util.function.Function<int[],ConcatVector> assignmentFeaturizer)
IMPORTANT: assignmentFeaturizer must be REPEATABLE and NOT HAVE SIDE EFFECTS This is because it is actually stored as a lazy closure until the full featurized vector is needed, and then it is created, used, and discarded. It CAN BE CALLED MULTIPLE TIMES, and must always return the same value in order for behavior of downstream systems to be defined.
neighborIndices
- the names of the variables, as indicesneighborDimensions
- the sizes of the neighbor variables, corresponding to the order in neighborIndicesassignmentFeaturizer
- a function that maps from an assignment to the variables, represented as an array of
assignments in the same order as presented in neighborIndices, to a ConcatVector of
features for that assignment.public GraphicalModel.Factor addFactor(ConcatVectorTable featureTable, int[] neighborIndices)
featureTable
- the feature table to use to drive the value of the factorneighborIndices
- the indices of the neighboring variables, in orderpublic int[] getVariableSizes()
public void writeToStream(java.io.OutputStream stream) throws java.io.IOException
stream
- the output stream to write tojava.io.IOException
- passed through from the streampublic static GraphicalModel readFromStream(java.io.InputStream stream) throws java.io.IOException
stream
- the stream to read from, assuming protobuf encodingjava.io.IOException
- passed through from the streampublic GraphicalModelProto.GraphicalModel.Builder getProtoBuilder()
public static GraphicalModel readFromProto(GraphicalModelProto.GraphicalModel proto)
proto
- the proto to readpublic boolean valueEquals(GraphicalModel other, double tolerance)
other
- the graphical model to compare against.tolerance
- the tolerance to accept when comparing concat vectors for value equality.public java.lang.String toString()
toString
in class java.lang.Object
public GraphicalModel cloneModel()