本文整理汇总了Python中pystruct.models.GraphCRF.inference方法的典型用法代码示例。如果您正苦于以下问题:Python GraphCRF.inference方法的具体用法?Python GraphCRF.inference怎么用?Python GraphCRF.inference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pystruct.models.GraphCRF
的用法示例。
在下文中一共展示了GraphCRF.inference方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_graph_crf_energy_lp_relaxed
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_energy_lp_relaxed():
crf = GraphCRF(n_states=2, inference_method="lp")
for i in xrange(10):
w_ = np.random.uniform(size=w.shape)
inf_res, energy_lp = crf.inference((x_1, g_1), w_, relaxed=True, return_energy=True)
assert_almost_equal(energy_lp, -np.dot(w_, crf.psi((x_1, g_1), inf_res)))
# now with fractional solution
x = np.array([[0, 0], [0, 0], [0, 0]])
inf_res, energy_lp = crf.inference((x, g_1), w, relaxed=True, return_energy=True)
assert_almost_equal(energy_lp, -np.dot(w, crf.psi((x, g_1), inf_res)))
示例2: test_graph_crf_continuous_inference
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_continuous_inference():
for inference_method in get_installed(['lp', 'ad3']):
crf = GraphCRF(n_states=2, inference_method=inference_method)
y_hat = crf.inference((x_1, g_1), w, relaxed=True)
if isinstance(y_hat, tuple):
assert_array_equal(np.argmax(y_hat[0], axis=-1), y_1)
else:
# ad3 produces integer result if it found the exact solution
assert_array_equal(y_hat, y_1)
y_hat = crf.inference((x_2, g_2), w, relaxed=True)
if isinstance(y_hat, tuple):
assert_array_equal(np.argmax(y_hat[0], axis=-1), y_2)
else:
assert_array_equal(y_hat, y_2)
示例3: test_graph_crf_inference
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_inference():
# create two samples with different graphs
# two states only, pairwise smoothing
for inference_method in ["qpbo", "lp", "ad3", "dai"]:
crf = GraphCRF(n_states=2, inference_method=inference_method)
assert_array_equal(crf.inference((x_1, g_1), w), y_1)
assert_array_equal(crf.inference((x_2, g_2), w), y_2)
示例4: test_graph_crf_inference
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_inference():
# create two samples with different graphs
# two states only, pairwise smoothing
for inference_method in get_installed(['qpbo', 'lp', 'ad3', 'ogm']):
crf = GraphCRF(n_states=2, n_features=2,
inference_method=inference_method)
assert_array_equal(crf.inference((x_1, g_1), w), y_1)
assert_array_equal(crf.inference((x_2, g_2), w), y_2)
示例5: test_graph_crf_energy_lp_integral
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_energy_lp_integral():
crf = GraphCRF(n_states=2, inference_method="lp")
inf_res, energy_lp = crf.inference((x_1, g_1), w, relaxed=True, return_energy=True)
# integral solution
assert_array_almost_equal(np.max(inf_res[0], axis=-1), 1)
y = np.argmax(inf_res[0], axis=-1)
# energy and psi check out
assert_almost_equal(energy_lp, -np.dot(w, crf.psi((x_1, g_1), y)))
示例6: test_directed_graph_crf_inference
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_directed_graph_crf_inference():
# create two samples with different graphs
# two states only, pairwise smoothing
# same as above, only with full symmetric matrix
for inference_method in get_installed(['qpbo', 'lp', 'ad3', 'ogm']):
crf = GraphCRF(n_states=2, n_features=2,
inference_method=inference_method, directed=True)
assert_array_equal(crf.inference((x_1, g_1), w_sym), y_1)
assert_array_equal(crf.inference((x_2, g_2), w_sym), y_2)
示例7: test_graph_crf_class_weights
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_class_weights():
# no edges
crf = GraphCRF(n_states=3, n_features=3, inference_method="dai")
w = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]) # unary # pairwise
x = (np.array([[1, 1.5, 1.1]]), np.empty((0, 2)))
assert_equal(crf.inference(x, w), 1)
# loss augmented inference picks last
assert_equal(crf.loss_augmented_inference(x, [1], w), 2)
# with class-weights, loss for class 1 is smaller, loss-augmented inference
# will find it
crf = GraphCRF(n_states=3, n_features=3, inference_method="dai", class_weight=[1, 0.1, 1])
assert_equal(crf.loss_augmented_inference(x, [1], w), 1)
示例8: make_random_trees
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def make_random_trees(n_samples=50, n_nodes=100, n_states=7, n_features=10):
crf = GraphCRF(inference_method='max-product', n_states=n_states,
n_features=n_features)
weights = np.random.randn(crf.size_joint_feature)
X, y = [], []
for i in range(n_samples):
distances = np.random.randn(n_nodes, n_nodes)
features = np.random.randn(n_nodes, n_features)
tree = minimum_spanning_tree(sparse.csr_matrix(distances))
edges = np.c_[tree.nonzero()]
X.append((features, edges))
y.append(crf.inference(X[-1], weights))
return X, y, weights
示例9: test_directed_graph_chain
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_directed_graph_chain():
# check that a directed model actually works differntly in the two
# directions. chain of length three, three states 0, 1, 2 which want to be
# in this order, evidence only in the middle
x = (np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]),
np.array([[0, 1], [1, 2]]))
w = np.array([1, 0, 0, # unary
0, 1, 0,
0, 0, 1,
0, 1, 0, # pairwise
0, 0, 1,
0, 0, 0])
crf = GraphCRF(n_states=3, n_features=3, directed=True)
y = crf.inference(x, w)
assert_array_equal([0, 1, 2], y)
示例10: test_graph_crf_continuous_inference
# 需要导入模块: from pystruct.models import GraphCRF [as 别名]
# 或者: from pystruct.models.GraphCRF import inference [as 别名]
def test_graph_crf_continuous_inference():
for inference_method in ["lp", "ad3"]:
crf = GraphCRF(n_states=2, inference_method=inference_method)
assert_array_equal(np.argmax(crf.inference((x_1, g_1), w, relaxed=True)[0], axis=-1), y_1)
assert_array_equal(np.argmax(crf.inference((x_2, g_2), w, relaxed=True)[0], axis=-1), y_2)