本文整理汇总了Python中pystruct.models.GridCRF类的典型用法代码示例。如果您正苦于以下问题:Python GridCRF类的具体用法?Python GridCRF怎么用?Python GridCRF使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GridCRF类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_binary_grid_unaries
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)
开发者ID:al13n321,项目名称:pystruct,代码行数:29,代码来源:test_grid_crf.py
示例2: test_binary_grid_unaries
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)
开发者ID:pystruct,项目名称:pystruct,代码行数:27,代码来源:test_grid_crf.py
示例3: test_binary_blocks_crf_n8_lp
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:8,代码来源:test_grid_crf.py
示例4: test_max_product_multinomial_crf
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:8,代码来源:test_maxprod.py
示例5: test_max_product_binary_blocks
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:8,代码来源:test_maxprod.py
示例6: test_binary_blocks_crf
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:9,代码来源:test_grid_crf.py
示例7: test_blocks_multinomial_crf
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:9,代码来源:test_grid_crf.py
示例8: test_binary_blocks_crf_n8_lp
def test_binary_blocks_crf_n8_lp():
X, Y = toy.generate_blocks(n_samples=1, noise=1)
x, y = X[0], Y[0]
w = np.array([1, 0, # unary
0, 1,
1, # pairwise
-1.4, 1])
crf = GridCRF(inference_method="lp", neighborhood=8)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
开发者ID:hushell,项目名称:pystruct,代码行数:10,代码来源:test_grid_crf.py
示例9: test_binary_blocks_crf
def test_binary_blocks_crf():
X, Y = toy.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 ['dai', 'qpbo', 'lp', 'ad3']:
crf = GridCRF(inference_method=inference_method)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
开发者ID:hushell,项目名称:pystruct,代码行数:11,代码来源:test_grid_crf.py
示例10: test_energy_lp
def test_energy_lp():
# make sure that energy as computed by ssvm is the same as by lp
np.random.seed(0)
found_fractional = False
for inference_method in get_installed(["lp", "ad3"]):
crf = GridCRF(n_states=3, n_features=4, inference_method=inference_method)
while not found_fractional:
x = np.random.normal(size=(2, 2, 4))
w = np.random.uniform(size=crf.size_joint_feature)
inf_res, energy_lp = crf.inference(x, w, relaxed=True, return_energy=True)
assert_almost_equal(energy_lp, -np.dot(w, crf.joint_feature(x, inf_res)))
found_fractional = np.any(np.max(inf_res[0], axis=-1) != 1)
开发者ID:martinsch,项目名称:pystruct,代码行数:12,代码来源:test_grid_crf.py
示例11: test_loss_augmentation
def test_loss_augmentation():
X, Y = toy.generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, # unary
0, 1,
0, # pairwise
-4, 0])
crf = GridCRF(inference_method='lp')
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.psi(x, y_hat)))
开发者ID:hushell,项目名称:pystruct,代码行数:12,代码来源:test_grid_crf.py
示例12: test_binary_blocks_crf
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)
开发者ID:al13n321,项目名称:pystruct,代码行数:12,代码来源:test_grid_crf.py
示例13: test_blocks_multinomial_crf
def test_blocks_multinomial_crf():
X, Y = toy.generate_blocks_multinomial(n_samples=1)
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 ['dai', 'qpbo', 'lp', 'ad3']:
crf = GridCRF(n_states=3, inference_method=inference_method)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
开发者ID:hushell,项目名称:pystruct,代码行数:13,代码来源:test_grid_crf.py
示例14: test_blocks_multinomial_crf
def test_blocks_multinomial_crf():
X, Y = toy.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():
crf = GridCRF(n_states=3, inference_method=inference_method)
y_hat = crf.inference(x, w)
assert_array_equal(y, y_hat)
开发者ID:abhijitbendale,项目名称:pystruct,代码行数:13,代码来源:test_grid_crf.py
示例15: test_binary_crf_exhaustive
def test_binary_crf_exhaustive():
# tests qpbo inference against brute force
# on random data / weights
np.random.seed(0)
for i in xrange(10):
x = np.random.uniform(-1, 1, size=(3, 2))
x = np.dstack([-x, np.zeros_like(x)]).copy()
crf = GridCRF(n_features=2, n_states=2)
w = np.random.uniform(-1, 1, size=7)
# check map inference
y_hat = crf.inference(x, w)
y_ex = exhaustive_inference(crf, x, w)
assert_array_equal(y_hat, y_ex)
开发者ID:al13n321,项目名称:pystruct,代码行数:13,代码来源:test_grid_crf.py
示例16: test_binary_ssvm_attractive_potentials_edgefeaturegraph
def test_binary_ssvm_attractive_potentials_edgefeaturegraph(inference_method="qpbo"):
X, Y = generate_blocks(n_samples=10)
crf = GridCRF(inference_method=inference_method)
#######
# convert X,Y to EdgeFeatureGraphCRF instances
crf_edge = EdgeFeatureGraphCRF(inference_method=inference_method,
symmetric_edge_features=[0]
)
X_edge = []
Y_edge = []
for i in range(X.shape[0]):
unaries = X[i].reshape((-1, 2))
edges = crf._get_edges(X[i])
edge_feats = np.ones((edges.shape[0], 1))
X_edge.append((unaries, edges, edge_feats))
Y_edge.append((Y[i].reshape((-1,))))
submodular_clf_edge = SubgradientSSVM(model=crf_edge, max_iter=100, C=1,
verbose=1,
zero_constraint=[4,7],
negativity_constraint=[5,6],
)
# fit the model with non-negativity constraint on the off-diagonal potential
submodular_clf_edge.fit(X_edge, Y_edge)
assert submodular_clf_edge.w[5] == submodular_clf_edge.w[6] # symmetry constraint on edge features
# # # bias doesn't matter
# submodular_clf_edge.w += 10*np.ones(submodular_clf_edge.w.shape)
# print len(submodular_clf_edge.w), submodular_clf_edge.w
Y_pred = submodular_clf_edge.predict(X_edge)
assert_array_equal(Y_edge, Y_pred)
# try to fit the model with non-negativity constraint on the off-diagonal potential, this time
# with inverted sign on the edge features
X_edge_neg = [ (x[0], x[1], -x[2]) for x in X_edge ]
submodular_clf_edge = SubgradientSSVM(model=crf_edge, max_iter=100, C=1,
verbose=1,
zero_constraint=[4,7],
negativity_constraint=[5,6],
)
submodular_clf_edge.fit(X_edge_neg, Y_edge)
Y_pred = submodular_clf_edge.predict(X_edge_neg)
assert_array_equal(Y_edge, Y_pred)
开发者ID:martinsch,项目名称:coulomb_ssvm,代码行数:50,代码来源:submodular_inference.py
示例17: test_binary_crf_exhaustive_loss_augmented
def test_binary_crf_exhaustive_loss_augmented():
# tests qpbo inference against brute force
# on random data / weights
np.random.seed(0)
for inference_method in get_installed(["qpbo", "lp"]):
crf = GridCRF(n_states=2, n_features=2, inference_method=inference_method)
for i in xrange(10):
# generate data and weights
y = np.random.randint(2, size=(3, 2))
x = np.random.uniform(-1, 1, size=(3, 2))
x = np.dstack([-x, np.zeros_like(x)])
w = np.random.uniform(-1, 1, size=7)
# check loss augmented map inference
y_hat = crf.loss_augmented_inference(x, y, w)
y_ex = exhaustive_loss_augmented_inference(crf, x, y, w)
assert_array_equal(y_hat, y_ex)
开发者ID:martinsch,项目名称:pystruct,代码行数:16,代码来源:test_grid_crf.py
示例18: test_binary_crf_exhaustive
def test_binary_crf_exhaustive():
# tests graph cut inference against brute force
# on random data / weights
np.random.seed(0)
for i in xrange(50):
x = np.random.uniform(-1, 1, size=(3, 3))
x = np.dstack([-x, np.zeros_like(x)]).copy()
crf = GridCRF()
w = np.random.uniform(-1, 1, size=7)
# check map inference
y_hat = crf.inference(x, w)
y_ex = exhaustive_inference(crf, x, w)
#print(y_hat)
#print(y_ex)
#print("++++++++++++++++++++++")
assert_array_equal(y_hat, y_ex)
开发者ID:hushell,项目名称:pystruct,代码行数:16,代码来源:test_grid_crf.py
示例19: test_blocks_multinomial_crf
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)
开发者ID:pystruct,项目名称:pystruct,代码行数:16,代码来源:test_grid_crf.py
示例20: test_continuous_y
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)
开发者ID:martinsch,项目名称:pystruct,代码行数:34,代码来源:test_grid_crf.py
注:本文中的pystruct.models.GridCRF类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论