def test_subgradient(): for h in hypergraphs(): w = utils.random_log_viterbi_potentials(h) constraints, edge = random_have_constraint(h) path = ph.best_path(h, w) match = constraints.check(path) if edge not in path: nt.assert_equal(match[0], "have") cpath = opt.best_constrained_path(h, w, constraints) assert edge in cpath
# # In[55]: path = hyper.best_path(hyper1, potentials) potentials.dot(path) # Out[55]: # 3.458035945892334 # In[56]: import pydecode.optimization as opt cpath = opt.best_constrained_path(hyper1, potentials, constraints) # In[57]: CipherFormat(hyper1, [cpath]).to_ipython() # Out[57]: # <IPython.core.display.Image at 0x5381090> # In[58]: print potentials.dot(cpath) constraints.check(cpath)
constraints = cons.Constraints(hypergraph, [(cons_name(tag), 0) for tag in ["D", "V", "N"]]).build( build_constraints) # This check fails because the tags do not agree. # In[ ]: print "check", constraints.check(path) # Solve instead using subgradient. # In[ ]: gpath = opt.best_constrained_path(hypergraph, potentials, constraints) # In[ ]: import pydecode.lp as lp hypergraph_lp = lp.HypergraphLP.make_lp(hypergraph, potentials) hypergraph_lp.solve() path = hypergraph_lp.path # In[ ]: # Output the path. for edge in gpath.edges: print hypergraph.label(edge)