#NEWLINE# def inference_ad3( unary_potentials , pairwise_potentials , edges , relaxed = False , verbose = 0 , return_energy = False , branch_and_bound = False ) : #NEWLINE# #INDENT# "Inference with AD3 dual decomposition subgradient solver.\n\n    Parameters\n    ----------\n    unary_potentials : nd-array, shape (n_nodes, n_states)\n        Unary potentials of energy function.\n\n    pairwise_potentials : nd-array, shape (n_states, n_states) or (n_states, n_states, n_edges).\n        Pairwise potentials of energy function.\n        If the first case, edge potentials are assumed to be the same for all edges.\n        In the second case, the sequence needs to correspond to the edges.\n\n    edges : nd-array, shape (n_edges, 2)\n        Graph edges for pairwise potentials, given as pair of node indices. As\n        pairwise potentials are not assumed to be symmetric, the direction of\n        the edge matters.\n\n    relaxed : bool (default=False)\n        Whether to return the relaxed solution (``True``) or round to the next\n        integer solution (``False``).\n\n    verbose : int (default=0)\n        Degree of verbosity for solver.\n\n    return_energy : bool (default=False)\n        Additionally return the energy of the returned solution (according to\n        the solver).  If relaxed=False, this is the energy of the relaxed, not\n        the rounded solution.\n\n    branch_and_bound : bool (default=False)\n        Whether to attempt to produce an integral solution using\n        branch-and-bound.\n\n    Returns\n    -------\n    labels : nd-array\n        Approximate (usually) MAP variable assignment.\n        If relaxed=False, this is a tuple of unary and edge 'marginals'.\n    " #NEWLINE# import ad3 #NEWLINE# ( n_states , pairwise_potentials ) = _validate_params ( unary_potentials , pairwise_potentials , edges ) #NEWLINE# unaries = unary_potentials . reshape ( ( - 1 ) , n_states ) #NEWLINE# res = ad3 . general_graph ( unaries , edges , pairwise_potentials , verbose = verbose , n_iterations = 4000 , exact = branch_and_bound ) #NEWLINE# ( unary_marginals , pairwise_marginals , energy , solver_status ) = res #NEWLINE# if verbose : #NEWLINE# #INDENT# print ( solver_status [ 0 ] ) #NEWLINE# #UNINDENT# if ( ( solver_status in [ 'fractional' , 'unsolved' ] ) and relaxed ) : #NEWLINE# #INDENT# unary_marginals = unary_marginals . reshape ( unary_potentials . shape ) #NEWLINE# y = ( unary_marginals , pairwise_marginals ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y = np . argmax ( unary_marginals , axis = ( - 1 ) ) #NEWLINE# #UNINDENT# if return_energy : #NEWLINE# #INDENT# return ( y , ( - energy ) ) #NEWLINE# #UNINDENT# return y