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


Python LatentGridCRF.inference方法代码示例

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


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

示例1: test_blocks_crf_directional

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
def test_blocks_crf_directional():
    # test latent directional CRF on blocks
    # test that all results are the same as equivalent LatentGridCRF
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    pw_directional = np.array(
        [0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0, 0, 0, -4, -4, 0, 0, -4, -4, -4, -4, 0, 0, -4, -4, 0, 0]
    )
    w_directional = np.hstack([unary_weights.ravel(), pw_directional])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    directional_crf = LatentDirectionalGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    h_hat_d = directional_crf.inference(x, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    h = crf.latent(x, y, w)
    h_d = directional_crf.latent(x, y, w_directional)
    assert_array_equal(h, h_d)

    h_hat = crf.loss_augmented_inference(x, y, w)
    h_hat_d = directional_crf.loss_augmented_inference(x, y, w_directional)
    assert_array_equal(h_hat, h_hat_d)

    psi = crf.psi(x, h_hat)
    psi_d = directional_crf.psi(x, h_hat)
    assert_array_equal(np.dot(psi, w), np.dot(psi_d, w_directional))
开发者ID:hushell,项目名称:pystruct,代码行数:31,代码来源:test_latent_crf.py

示例2: test_blocks_crf_unaries

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
def test_blocks_crf_unaries():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    pairwise_weights = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(h_hat / 2, np.argmax(x, axis=-1))
开发者ID:hushell,项目名称:pystruct,代码行数:11,代码来源:test_latent_crf.py

示例3: test_blocks_crf

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
def test_blocks_crf():
    X, Y = toy.generate_blocks(n_samples=1)
    x, y = X[0], Y[0]
    pairwise_weights = np.array([0, 0, 0, -4, -4, 0, -4, -4, 0, 0])
    unary_weights = np.repeat(np.eye(2), 2, axis=0)
    w = np.hstack([unary_weights.ravel(), pairwise_weights])
    crf = LatentGridCRF(n_labels=2, n_states_per_label=2)
    h_hat = crf.inference(x, w)
    assert_array_equal(y, h_hat / 2)

    h = crf.latent(x, y, w)
    assert_equal(crf.loss(h, h_hat), 0)
开发者ID:hushell,项目名称:pystruct,代码行数:14,代码来源:test_latent_crf.py

示例4: main

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
def main():
    X, Y = toy.generate_crosses(n_samples=20, noise=5, n_crosses=1,
                                total_size=8)
    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.5)
    n_labels = len(np.unique(Y_train))
    crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=[1, 2],
                        inference_method='lp')
    #clf = LatentSSVM(model=crf, max_iter=500, C=1000., verbose=2,
                     #check_constraints=True, n_jobs=-1, break_on_bad=True,
                     #base_svm='1-slack', inference_cache=20, tol=.1)
    clf = LatentSubgradientSSVM(
        model=crf, max_iter=500, C=1000., verbose=2,
        n_jobs=-1, learning_rate=0.1, show_loss_every=10)
    clf.fit(X_train, Y_train)

    #for X_, Y_, H, name in [[X_train, Y_train, clf.H_init_, "train"],
                            #[X_test, Y_test, [None] * len(X_test), "test"]]:
    for X_, Y_, H, name in [[X_train, Y_train, [None] * len(X_test), "train"],
                            [X_test, Y_test, [None] * len(X_test), "test"]]:
        Y_pred = clf.predict(X_)
        i = 0
        loss = 0
        for x, y, h_init, y_pred in zip(X_, Y_, H, Y_pred):
            loss += np.sum(y != y_pred)
            fig, ax = plt.subplots(3, 2)
            ax[0, 0].matshow(y, vmin=0, vmax=crf.n_labels - 1)
            ax[0, 0].set_title("ground truth")
            ax[0, 1].matshow(np.argmax(x, axis=-1),
                             vmin=0, vmax=crf.n_labels - 1)
            ax[0, 1].set_title("unaries only")
            if h_init is None:
                ax[1, 0].set_visible(False)
            else:
                ax[1, 0].matshow(h_init, vmin=0, vmax=crf.n_states - 1)
                ax[1, 0].set_title("latent initial")
            ax[1, 1].matshow(crf.latent(x, y, clf.w),
                             vmin=0, vmax=crf.n_states - 1)
            ax[1, 1].set_title("latent final")
            ax[2, 0].matshow(crf.inference(x, clf.w),
                             vmin=0, vmax=crf.n_states - 1)
            ax[2, 0].set_title("prediction latent")
            ax[2, 1].matshow(y_pred,
                             vmin=0, vmax=crf.n_labels - 1)
            ax[2, 1].set_title("prediction")
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            fig.savefig("data_%s_%03d.png" % (name, i), bbox_inches="tight")
            i += 1
        print("loss %s set: %f" % (name, loss))
    print(clf.w)
开发者ID:hushell,项目名称:pystruct,代码行数:53,代码来源:latent_crf.py

示例5: LatentSSVM

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
                         tol=.1)
clf = LatentSSVM(base_ssvm=base_ssvm)
clf.fit(X_train, Y_train)
print("loss training set: %f" % clf.score(X_train, Y_train))
print("loss test set: %f" % clf.score(X_test, Y_test))

Y_pred = clf.predict(X_test)

x, y, y_pred = X_test[1], Y_test[1], Y_pred[1]
fig, ax = plt.subplots(3, 2)
ax[0, 0].matshow(y, vmin=0, vmax=crf.n_labels - 1)
ax[0, 0].set_title("ground truth")
ax[0, 1].matshow(np.argmax(x, axis=-1),
                 vmin=0, vmax=crf.n_labels - 1)
ax[0, 1].set_title("unaries only")
ax[1, 0].set_visible(False)
ax[1, 1].matshow(crf.latent(x, y, clf.w),
                 vmin=0, vmax=crf.n_states - 1)
ax[1, 1].set_title("latent final")
ax[2, 0].matshow(crf.inference(x, clf.w),
                 vmin=0, vmax=crf.n_states - 1)
ax[2, 0].set_title("prediction latent")
ax[2, 1].matshow(y_pred,
                 vmin=0, vmax=crf.n_labels - 1)
ax[2, 1].set_title("prediction")
for a in ax.ravel():
    a.set_xticks(())
    a.set_yticks(())

plt.show()
开发者ID:amelio-vazquez-reina,项目名称:pystruct,代码行数:32,代码来源:plot_latent_crf.py

示例6: generate_crosses

# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import inference [as 别名]
X, Y = generate_crosses(n_samples=20, noise=5, n_crosses=1, total_size=8)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.5)

crf = LatentGridCRF(n_states_per_label=[1, 2])
base_ssvm = OneSlackSSVM(model=crf, C=10.0, n_jobs=-1, inference_cache=20, tol=0.1)
clf = LatentSSVM(base_ssvm=base_ssvm)
clf.fit(X_train, Y_train)
print("loss training set: %f" % clf.score(X_train, Y_train))
print("loss test set: %f" % clf.score(X_test, Y_test))

Y_pred = clf.predict(X_test)

x, y, y_pred = X_test[1], Y_test[1], Y_pred[1]
fig, ax = plt.subplots(3, 2)
ax[0, 0].matshow(y, vmin=0, vmax=crf.n_labels - 1)
ax[0, 0].set_title("ground truth")
ax[0, 1].matshow(np.argmax(x, axis=-1), vmin=0, vmax=crf.n_labels - 1)
ax[0, 1].set_title("unaries only")
ax[1, 0].set_visible(False)
ax[1, 1].matshow(crf.latent(x, y, clf.w), vmin=0, vmax=crf.n_states - 1)
ax[1, 1].set_title("latent final")
ax[2, 0].matshow(crf.inference(x, clf.w), vmin=0, vmax=crf.n_states - 1)
ax[2, 0].set_title("prediction latent")
ax[2, 1].matshow(y_pred, vmin=0, vmax=crf.n_labels - 1)
ax[2, 1].set_title("prediction")
for a in ax.ravel():
    a.set_xticks(())
    a.set_yticks(())

plt.show()
开发者ID:robeth,项目名称:pystruct,代码行数:32,代码来源:plot_latent_crf.py


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