本文整理汇总了Python中pystruct.models.GridCRF.initialize方法的典型用法代码示例。如果您正苦于以下问题:Python GridCRF.initialize方法的具体用法?Python GridCRF.initialize怎么用?Python GridCRF.initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pystruct.models.GridCRF
的用法示例。
在下文中一共展示了GridCRF.initialize方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_binary_grid_unaries
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_binary_grid_unaries():
# test handling on unaries for binary grid CRFs
for ds in binary:
X, Y = ds(n_samples=1)
x, y = X[0], Y[0]
for inference_method in get_installed():
# dai is to expensive
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
w_unaries_only = np.zeros(7)
w_unaries_only[:4] = np.eye(2).ravel()
# test that inference with unaries only is the
# same as argmax
inf_unaries = crf.inference(x, w_unaries_only)
assert_array_equal(inf_unaries, np.argmax(x, axis=2),
"Wrong unary inference for %s"
% inference_method)
try:
assert(np.mean(inf_unaries == y) > 0.5)
except:
print(ds)
# check that the right thing happens on noise-free data
X, Y = ds(n_samples=1, noise=0)
inf_unaries = crf.inference(X[0], w_unaries_only)
assert_array_equal(inf_unaries, Y[0],
"Wrong unary result for %s"
% inference_method)
示例2: test_continuous_y
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_continuous_y():
for inference_method in get_installed(["lp", "ad3"]):
X, Y = generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
joint_feature = crf.joint_feature(x, y)
y_cont = np.zeros_like(x)
gx, gy = np.indices(x.shape[:-1])
y_cont[gx, gy, y] = 1
# need to generate edge marginals
vert = np.dot(y_cont[1:, :, :].reshape(-1, 2).T, y_cont[:-1, :, :].reshape(-1, 2))
# horizontal edges
horz = np.dot(y_cont[:, 1:, :].reshape(-1, 2).T, y_cont[:, :-1, :].reshape(-1, 2))
pw = vert + horz
joint_feature_cont = crf.joint_feature(x, (y_cont, pw))
assert_array_almost_equal(joint_feature, joint_feature_cont)
const = find_constraint(crf, x, y, w, relaxed=False)
const_cont = find_constraint(crf, x, y, w, relaxed=True)
# djoint_feature and loss are equal:
assert_array_almost_equal(const[1], const_cont[1], 4)
assert_almost_equal(const[2], const_cont[2], 4)
# returned y_hat is one-hot version of other
if isinstance(const_cont[0], tuple):
assert_array_equal(const[0], np.argmax(const_cont[0][0], axis=-1))
# test loss:
assert_almost_equal(crf.loss(y, const[0]), crf.continuous_loss(y, const_cont[0][0]), 4)
示例3: test_binary_grid_unaries
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_binary_grid_unaries():
# test handling on unaries for binary grid CRFs
for ds in binary:
X, Y = ds(n_samples=1)
x, y = X[0], Y[0]
for inference_method in get_installed():
#NOTE: ad3+ fails because it requires a different data structure
if inference_method == 'ad3+': continue
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
w_unaries_only = np.zeros(7)
w_unaries_only[:4] = np.eye(2).ravel()
# test that inference with unaries only is the
# same as argmax
inf_unaries = crf.inference(x, w_unaries_only)
assert_array_equal(inf_unaries, np.argmax(x, axis=2),
"Wrong unary inference for %s"
% inference_method)
assert(np.mean(inf_unaries == y) > 0.5)
# check that the right thing happens on noise-free data
X, Y = ds(n_samples=1, noise=0)
inf_unaries = crf.inference(X[0], w_unaries_only)
assert_array_equal(inf_unaries, Y[0],
"Wrong unary result for %s"
% inference_method)
示例4: test_binary_blocks_crf_n8_lp
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_binary_blocks_crf_n8_lp():
X, Y = generate_blocks(n_samples=1, noise=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 1, -1.4, 1]) # unary # pairwise
crf = GridCRF(neighborhood=8)
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例5: test_max_product_multinomial_crf
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_max_product_multinomial_crf():
X, Y = generate_blocks_multinomial(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.4, -0.3, 0.3, -0.5, -0.1, 0.3]) # unary # pairwise
crf = GridCRF(inference_method="max-product")
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例6: test_max_product_binary_blocks
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_max_product_binary_blocks():
X, Y = generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise
crf = GridCRF(inference_method="max-product")
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例7: test_blocks_multinomial_crf
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_blocks_multinomial_crf():
X, Y = generate_blocks_multinomial(n_samples=1, size_x=9, seed=0)
x, y = X[0], Y[0]
w = np.array([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.4, -0.3, 0.3, -0.5, -0.1, 0.3]) # unaryA # pairwise
for inference_method in get_installed():
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例8: test_binary_blocks_crf
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_binary_blocks_crf():
X, Y = generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise
for inference_method in get_installed(["dai", "qpbo", "lp", "ad3", "ogm"]):
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例9: test_loss_augmentation
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_loss_augmentation():
X, Y = generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise
crf = GridCRF()
crf.initialize(X, Y)
y_hat, energy = crf.loss_augmented_inference(x, y, w, return_energy=True)
assert_almost_equal(energy + crf.loss(y, y_hat), -np.dot(w, crf.joint_feature(x, y_hat)))
示例10: test_binary_blocks_crf
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_binary_blocks_crf():
X, Y = generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, # unary
0, 1,
0, # pairwise
-4, 0])
for inference_method in get_installed(['dai', 'qpbo', 'lp', 'ad3', 'ogm']):
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例11: test_blocks_multinomial_crf
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_blocks_multinomial_crf():
X, Y = generate_blocks_multinomial(n_samples=1, size_x=9, seed=0)
x, y = X[0], Y[0]
w = np.array([1., 0., 0., # unaryA
0., 1., 0.,
0., 0., 1.,
.4, # pairwise
-.3, .3,
-.5, -.1, .3])
for inference_method in get_installed():
#NOTE: ad3+ fails because it requires a different data structure
if inference_method == 'ad3+': continue
crf = GridCRF(inference_method=inference_method)
crf.initialize(X, Y)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
示例12: test_multinomial_grid_unaries
# 需要导入模块: from pystruct.models import GridCRF [as 别名]
# 或者: from pystruct.models.GridCRF import initialize [as 别名]
def test_multinomial_grid_unaries():
# test handling on unaries for multinomial grid CRFs
# on multinomial datasets
for ds in multinomial:
X, Y = ds(n_samples=1, size_x=9)
x, y = X[0], Y[0]
n_labels = len(np.unique(Y))
crf = GridCRF(n_states=n_labels)
crf.initialize(X, Y)
w_unaries_only = np.zeros(crf.size_psi)
w_unaries_only[:n_labels ** 2] = np.eye(n_labels).ravel()
# test that inference with unaries only is the
# same as argmax
inf_unaries = crf.inference(x, w_unaries_only)
assert_array_equal(inf_unaries, np.argmax(x, axis=2))
# check that the right thing happens on noise-free data
X, Y = ds(n_samples=1, noise=0)
inf_unaries = crf.inference(X[0], w_unaries_only)
assert_array_equal(inf_unaries, Y[0])