当前位置: 首页>>代码示例>>Python>>正文


Python models.GraphCRF类代码示例

本文整理汇总了Python中pystruct.models.GraphCRF的典型用法代码示例。如果您正苦于以下问题:Python GraphCRF类的具体用法?Python GraphCRF怎么用?Python GraphCRF使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了GraphCRF类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_graph_crf_inference

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)
开发者ID:hushell,项目名称:pystruct,代码行数:7,代码来源:test_graph_crf.py

示例2: test_graph_crf_loss_augment

def test_graph_crf_loss_augment():
    x = (x_1, g_1)
    y = y_1
    crf = GraphCRF(n_states=2, inference_method="lp")
    y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True)
    # check that y_hat fulfills energy + loss condition
    assert_almost_equal(np.dot(w, crf.psi(x, y_hat)) + crf.loss(y, y_hat), -energy)
开发者ID:hushell,项目名称:pystruct,代码行数:7,代码来源:test_graph_crf.py

示例3: test_graph_crf_inference

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)
开发者ID:tolga-b,项目名称:pystruct,代码行数:8,代码来源:test_graph_crf.py

示例4: test_graph_crf_energy_lp_integral

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)))
开发者ID:hushell,项目名称:pystruct,代码行数:8,代码来源:test_graph_crf.py

示例5: test_directed_graph_crf_inference

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)
开发者ID:tolga-b,项目名称:pystruct,代码行数:9,代码来源:test_graph_crf.py

示例6: test_graph_crf_energy_lp_relaxed

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)))
开发者ID:hushell,项目名称:pystruct,代码行数:11,代码来源:test_graph_crf.py

示例7: test_graph_crf_class_weights

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)
开发者ID:hushell,项目名称:pystruct,代码行数:13,代码来源:test_graph_crf.py

示例8: make_random_trees

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
开发者ID:DATAQC,项目名称:pystruct,代码行数:14,代码来源:random_tree_crf.py

示例9: test_graph_crf_continuous_inference

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)
开发者ID:aurora1625,项目名称:pystruct,代码行数:15,代码来源:test_graph_crf.py

示例10: test_directed_graph_chain

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)
开发者ID:tolga-b,项目名称:pystruct,代码行数:16,代码来源:test_graph_crf.py

示例11: test_graph_crf_loss_augment

def test_graph_crf_loss_augment():
    x = (x_1, g_1)
    y = y_1
    crf = GraphCRF()
    crf.initialize([x], [y])
    y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True)
    # check that y_hat fulfills energy + loss condition
    assert_almost_equal(np.dot(w, crf.joint_feature(x, y_hat)) + crf.loss(y, y_hat),
                        -energy)
开发者ID:tolga-b,项目名称:pystruct,代码行数:9,代码来源:test_graph_crf.py

示例12: test_initialize

def test_initialize():
    x = (x_1, g_1)
    y = y_1
    crf = GraphCRF(n_states=2, n_features=2)
    # no-op
    crf.initialize([x], [y])

    #test initialization works
    crf = GraphCRF()
    crf.initialize([x], [y])
    assert_equal(crf.n_states, 2)
    assert_equal(crf.n_features, 2)

    crf = GraphCRF(n_states=3)
    assert_raises(ValueError, crf.initialize, X=[x], Y=[y])
开发者ID:tolga-b,项目名称:pystruct,代码行数:15,代码来源:test_graph_crf.py

示例13: __init__

	def __init__(self, n_states=None, n_features=None, inference_method=None,
			 neighborhood=4, class_weight=None):
		self.neighborhood = neighborhood
		GraphCRF.__init__(self, n_states=n_states, n_features=n_features,
			inference_method=inference_method, 
			class_weight=class_weight)
开发者ID:maarteninja,项目名称:PPP,代码行数:6,代码来源:weightedgridcrf.py

示例14: test_graph_crf_continuous_inference

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)
开发者ID:hushell,项目名称:pystruct,代码行数:5,代码来源:test_graph_crf.py

示例15: __init__

 def __init__(self, n_labels=None, n_features=None, n_states_per_label=None,
              inference_method=None):
     self.n_labels = n_labels
     self.n_states_per_label = n_states_per_label
     GraphCRF.__init__(self, n_states=None, n_features=n_features,
                       inference_method=inference_method)
开发者ID:manelhr,项目名称:hidden_states,代码行数:6,代码来源:GraphLDCRF.py


注:本文中的pystruct.models.GraphCRF类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。