mrftools.LogLinearModel module¶
Class to convert from log linear model to MRF
-
class
mrftools.LogLinearModel.LogLinearModel[source]¶ Bases:
mrftools.MarkovNet.MarkovNetLog linear model class. Able to convert from log linear features to pairwise MRF.
-
create_indicator_model(markov_net)[source]¶ Sets this object to be a log-linear model representation of a Markov Net to enable directly learning the potential values. Each feature vector is an indicator vector, such that each dimension of the weights corresponds to exactly one variable or edge.
Parameters: markov_net (MarkovNet) – Markov network to build the indicator model of Returns: None
-
create_matrices()[source]¶ Create matrix representations of the MRF structure and log-linear model to allow inference to be done via matrix operations. :return: None
-
load_factors_from_matrices()[source]¶ Load dictionary-based factors from current matrices. Since learning is often done by updating the matrix form, it’s important to call this method before using the dictionary-based views of the potentials. This method will also properly undo the padding that makes all potential vectors and matrices the same shape (with zero-probability states to pad the smaller cardinality variables).
Returns: None
-
set_all_unary_factors()[source]¶ Uses current weights and features to set the potential functions for all unary factors. Used for non-matrix mode only.
Returns: None
-
set_edge_factor(edge, potential)[source]¶ Set a factor by inputting the involved variables then the potential function. The potential function should be a np matrix.
This method implicitly creates a single-dimensional edge feature set to value 1.0.
Parameters: - edge – 2-tuple of the variables in the edge. Can be in any order.
- potential – k1 by k2 matrix of potential values for the joint state of the two variables
Returns: None
-
set_edge_features(edge, values)[source]¶ Set the log-linear feature for a particular edge. Used for non-matrix mode only. Currently does not work. :param edge: pair of variables representing the edge being set :param values: ndarray of feature values describing the edge :return: None
-
set_edge_weight_matrix(edge_weight_mat)[source]¶ Set edge weight matrix. Used for matrix mode.
See developer note in set_unary_weight_matrix.
Parameters: edge_weight_mat (ndarray) – ndarray of shape (self.max_edge_features, self.max_states ** 2) Returns: None
-
set_feature_matrix(feature_mat)[source]¶ Set matrix of features for all unary variables. Used for matrix mode.
Parameters: feature_mat – ndarray of shape (max_unary_features, len(variables)) with variables as ordered in self.variables. Each jth column is the jth variable’s feature vector. Returns: None
-
set_unary_features(var, values)[source]¶ Set the log-linear features for a particular variable. Used for non-matrix mode only. :param var: variable whose features to set :param values: ndarray feature vector describing the unary variable of any length :return: None
-
set_unary_weight_matrix(weight_mat)[source]¶ Set unary weight matrix. Convenience method that also checks the shape of the new matrix. Used for matrix mode.
Developer note: this method may not be necessary. It’s meant to copy the matrix into the original weight matrix, memory but that may create problems for autodiff software in the future.
Parameters: weight_mat (ndarray) – ndarray of shape (self.max_unary_features, self.max_states) Returns: None
-
set_unary_weights(var, weights)[source]¶ Set the log-linear weights for the unary features of var. Used for non-matrix mode only.
Parameters: - var – variable whose weights to set.
- weights – ndarray weight matrix of shape (self.num_states[var], len(self.unary_features[var]))
Returns: None
-
set_weights(weight_vector)[source]¶ Set the unary and edge weight matrices by splitting and reshaping a weight vector. Useful for optimization when the optimizer is searching for a vector value. Used for matrix mode.
Parameters: weight_vector – real vector of length self.max_unary_features * self.max_state + self.max_edge_features * self.max_states ** 2 Returns: None
-