Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 174
Edge Featured Potts Model #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fgregg
wants to merge
17
commits into
pystruct:master
Choose a base branch
from
fgregg:rand_loss
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d83eb29
simple potts model
fgregg 5f0a8e9
potts model
fgregg 89227cf
remove commented out code
fgregg 16cc62b
whitespace
fgregg 483472c
pairwise loss augmentation
fgregg 4c9609c
potts models
fgregg 9d3603e
potts model in init
fgregg 4e192ba
simple potts model
fgregg a1c167a
potts model
fgregg 36bc9b3
remove commented out code
fgregg 421c5c2
whitespace
fgregg e3a7dcb
pairwise loss augmentation
fgregg 2ad1fa3
potts models
fgregg 74064de
simplify potts model
fgregg 977337c
pull in upstream
fgregg 3812e94
think I fixed relaxed inference psi
fgregg 4eedf4f
cleaning things up
fgregg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import numpy as np | ||
|
|
||
| from .edge_feature_graph_crf import EdgeFeatureGraphCRF | ||
| from .utils import loss_augment_unaries | ||
| from ..inference import inference_dispatch | ||
|
|
||
| class PottsEdgeFeatureGraphCRF(EdgeFeatureGraphCRF): | ||
| """Potts CRF with features/strength associated to each edge. | ||
|
|
||
| Edge potentials are 0 if neighbors share the same state, some | ||
| other value otherwise. | ||
|
|
||
| Node features and edge features are given as a tuple of shape (n_nodes, | ||
| n_features) and (n_edges, n_edge_features) respectively. | ||
|
|
||
| An instance ``x`` is represented as a tuple ``(node_features, edges, | ||
| edge_features)`` where edges is an array of shape (n_edges, 2), | ||
| representing the graph. | ||
|
|
||
| Labels ``y`` are given as array of shape (n_features) | ||
|
|
||
| Parameters | ||
| ---------- | ||
| n_states : int, default=2 | ||
| Number of states for all variables. | ||
|
|
||
| n_features : int, default=None | ||
| Number of features per node. None means n_states. | ||
|
|
||
| n_edge_features : int, default=1 | ||
| Number of features per edge. | ||
|
|
||
| inference_method : string, default="ad3" | ||
| Function to call do do inference and loss-augmented inference. | ||
| Possible values are: | ||
|
|
||
| - 'qpbo' for QPBO + alpha expansion. | ||
| - 'dai' for LibDAI bindings (which has another parameter). | ||
| - 'lp' for Linear Programming relaxation using GLPK. | ||
| - 'ad3' for AD3 dual decomposition. | ||
|
|
||
| class_weight : None, or array-like | ||
| Class weights. If an array-like is passed, it must have length | ||
| n_classes. None means equal class weights. | ||
|
|
||
| """ | ||
| def __init__(self, n_states=None, n_features=None, n_edge_features=None, | ||
| inference_method=None, class_weight=None, | ||
| markers=None): | ||
|
|
||
| self.markers = markers | ||
|
|
||
| EdgeFeatureGraphCRF.__init__(self, n_states, | ||
| n_features, | ||
| n_edge_features, | ||
| inference_method, | ||
| class_weight) | ||
|
|
||
| def _set_size_psi(self): | ||
| if not None in [self.n_states, self.n_features, self.n_edge_features]: | ||
| self.size_psi = (self.n_states * self.n_features | ||
| + self.n_edge_features) | ||
|
|
||
|
|
||
| def _get_pairwise_potentials(self, x, w): | ||
| """Computes pairwise potentials for x and w. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : tuple | ||
| Instance Representation. | ||
|
|
||
| w : ndarray, shape=(size_psi,) | ||
| Weight vector for CRF instance. | ||
|
|
||
| Returns | ||
| ------- | ||
| pairwise : ndarray, shape=(n_states, n_states) | ||
| Pairwise weights. | ||
| """ | ||
| self._check_size_w(w) | ||
| self._check_size_x(x) | ||
| edge_features = self._get_edge_features(x) | ||
| n_edges = edge_features.shape[0] | ||
| pairwise = np.asarray(w[self.n_states * self.n_features:]) | ||
| pairwise = pairwise.reshape(self.n_edge_features, -1) | ||
| pairwise = np.dot(edge_features, pairwise) | ||
|
|
||
| matrix_potentials = np.repeat(pairwise, | ||
| self.n_states ** 2).reshape(n_edges, | ||
| self.n_states, | ||
| self.n_states) | ||
| for i in xrange(n_edges) : | ||
| for j in xrange(self.n_states) : | ||
| matrix_potentials[i, j, j] = 0 | ||
|
|
||
| return matrix_potentials | ||
|
|
||
|
|
||
| def psi(self, x, y): | ||
| """Feature vector associated with instance (x, y). | ||
|
|
||
| Feature representation psi, such that the energy of the configuration | ||
| (x, y) and a weight vector w is given by np.dot(w, psi(x, y)). | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : tuple | ||
| Input representation. | ||
|
|
||
| y : ndarray or tuple | ||
| Either y is an integral ndarray, giving | ||
| a complete labeling for x. | ||
| Or it is the result of a linear programming relaxation. In this | ||
| case, ``y=(unary_marginals, pariwise_marginals)``. | ||
|
|
||
| Returns | ||
| ------- | ||
| p : ndarray, shape (size_psi,) | ||
| Feature vector associated with state (x, y). | ||
|
|
||
| """ | ||
| self._check_size_x(x) | ||
| features, edges = self._get_features(x), self._get_edges(x) | ||
| n_nodes = features.shape[0] | ||
| edge_features = self._get_edge_features(x) | ||
|
|
||
| if isinstance(y, tuple): | ||
| # y is result of relaxation, tuple of unary and pairwise marginals | ||
| unary_marginals, pw = y | ||
| unary_marginals = unary_marginals.reshape(n_nodes, self.n_states) | ||
|
|
||
| mask = np.ones((self.n_states, self.n_states)) - np.eye(self.n_states) | ||
| mask = mask.ravel() | ||
|
|
||
| pw = np.sum(pw * mask, axis=1) | ||
|
|
||
| pw = np.dot(edge_features.T, pw) | ||
|
|
||
|
|
||
| else: | ||
| y = y.reshape(n_nodes) | ||
| gx = np.ogrid[:n_nodes] | ||
|
|
||
| #make one hot encoding | ||
| unary_marginals = np.zeros((n_nodes, self.n_states), dtype=np.int) | ||
| gx = np.ogrid[:n_nodes] | ||
| unary_marginals[gx, y] = 1 | ||
|
|
||
| class_pair_ind = y[edges[:, 0]] != y[edges[:, 1]] | ||
| pw = np.sum(edge_features[class_pair_ind], axis=0) | ||
|
|
||
| unaries_acc = np.dot(unary_marginals.T, features) | ||
|
|
||
| psi_vector = np.hstack([unaries_acc.ravel(), pw.ravel()]) | ||
| return psi_vector | ||
|
|
||
| def loss_augmented_inference(self, x, y, w, relaxed=False, | ||
| return_energy=False): | ||
| """Loss-augmented Inference for x relative to y using parameters w. | ||
|
|
||
| Finds (approximately) | ||
| armin_y_hat np.dot(w, psi(x, y_hat)) + loss(y, y_hat) | ||
| using self.inference_method. | ||
|
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| x : tuple | ||
| Instance of a graph with unary evidence. | ||
| x=(unaries, edges) | ||
| unaries are an nd-array of shape (n_nodes, n_features), | ||
| edges are an nd-array of shape (n_edges, 2) | ||
|
|
||
| y : ndarray, shape (n_nodes,) | ||
| Ground truth labeling relative to which the loss | ||
| will be measured. | ||
|
|
||
| w : ndarray, shape=(size_psi,) | ||
| Parameters for the CRF energy function. | ||
|
|
||
| relaxed : bool, default=False | ||
| Whether relaxed inference should be performed. | ||
| Only meaningful if inference method is 'lp' or 'ad3'. | ||
| By default fractional solutions are rounded. If relaxed=True, | ||
| fractional solutions are returned directly. | ||
|
|
||
| return_energy : bool, default=False | ||
| Whether to return the energy of the solution (x, y) that was found. | ||
|
|
||
| Returns | ||
| ------- | ||
| y_pred : ndarray or tuple | ||
| By default an inter ndarray of shape=(n_nodes) | ||
| of variable assignments for x is returned. | ||
| If ``relaxed=True`` and inference_method is ``lp`` or ``ad3``, | ||
| a tuple (unary_marginals, pairwise_marginals) | ||
| containing the relaxed inference result is returned. | ||
| unary marginals is an array of shape (n_nodes, n_states), | ||
| pairwise_marginals is an array of | ||
| shape (n_edges, n_states x n_states) of accumulated | ||
| pairwise marginals. | ||
|
|
||
| """ | ||
| self.inference_calls += 1 | ||
| self._check_size_w(w) | ||
| unary_potentials = self._get_unary_potentials(x, w) | ||
| pairwise_potentials = self._get_pairwise_potentials(x, w) | ||
| edges = self._get_edges(x) | ||
| loss_augment_unaries(unary_potentials, np.asarray(y), self.class_weight) | ||
| return inference_dispatch(unary_potentials, pairwise_potentials, edges, | ||
| self.inference_method, relaxed=relaxed, | ||
| return_energy=return_energy) | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amueller @larsmans @vene @zaxtax
I have a clear idea about what psi should return is
yis an integral labeling. It's not clear to me what I should do when y is the result of a linear relaxation? Any pointers would be appreciated.