本文整理汇总了Python中pystruct.datasets.generate_blocks函数的典型用法代码示例。如果您正苦于以下问题:Python generate_blocks函数的具体用法?Python generate_blocks怎么用?Python generate_blocks使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了generate_blocks函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: 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)
示例2: test_binary_blocks_one_slack_graph
def test_binary_blocks_one_slack_graph():
#testing cutting plane ssvm on easy binary dataset
# generate graphs explicitly for each example
print("testing %s" % inference_method)
X, Y = generate_blocks(n_samples=3)
crf = GraphCRF(inference_method=inference_method)
clf = OneSlackSSVM(model=crf, max_iter=100, C=1,
check_constraints=True, break_on_bad=True,
n_jobs=1, tol=.1)
x1, x2, x3 = X
y1, y2, y3 = Y
n_states = len(np.unique(Y))
# delete some rows to make it more fun
x1, y1 = x1[:, :-1], y1[:, :-1]
x2, y2 = x2[:-1], y2[:-1]
# generate graphs
X_ = [x1, x2, x3]
G = [make_grid_edges(x) for x in X_]
# reshape / flatten x and y
X_ = [x.reshape(-1, n_states) for x in X_]
Y = [y.ravel() for y in [y1, y2, y3]]
X = zip(X_, G)
clf.fit(X, Y)
Y_pred = clf.predict(X)
for y, y_pred in zip(Y, Y_pred):
assert_array_equal(y, y_pred)
示例3: test_binary_blocks
def test_binary_blocks():
X, Y = generate_blocks(n_samples=10)
crf = GridCRF()
clf = StructuredPerceptron(model=crf, max_iter=40)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例4: 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)
示例5: 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)
示例6: test_binary_blocks_batches_n_slack
def test_binary_blocks_batches_n_slack():
#testing cutting plane ssvm on easy binary dataset
X, Y = generate_blocks(n_samples=5)
crf = GridCRF(inference_method=inference_method)
clf = NSlackSSVM(model=crf, max_iter=20, batch_size=1, C=100)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例7: test_binary_blocks
def test_binary_blocks():
#testing subgradient ssvm on easy binary dataset
X, Y = generate_blocks(n_samples=5)
crf = GridCRF(inference_method=inference_method)
clf = SubgradientSSVM(model=crf)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例8: test_binary_blocks_cutting_plane
def test_binary_blocks_cutting_plane():
#testing cutting plane ssvm on easy binary dataset
X, Y = generate_blocks(n_samples=5)
crf = GridCRF(inference_method=inference_method)
clf = NSlackSSVM(model=crf, max_iter=20, C=100,
check_constraints=True, break_on_bad=False)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例9: test_loss_augmentation
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
def test_binary_blocks():
#testing subgradient ssvm on easy binary dataset
X, Y = generate_blocks(n_samples=5)
crf = GridCRF(inference_method=inference_method)
clf = SubgradientSSVM(model=crf, C=100, learning_rate=1, decay_exponent=1,
momentum=0, decay_t0=10)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例11: test_binary_blocks_perceptron_online
def test_binary_blocks_perceptron_online():
#testing subgradient ssvm on easy binary dataset
X, Y = generate_blocks(n_samples=10)
inference_method = get_installed(['qpbo', 'ad3', 'lp'])[0]
crf = GridCRF(inference_method=inference_method)
clf = StructuredPerceptron(model=crf, max_iter=20)
clf.fit(X, Y)
Y_pred = clf.predict(X)
assert_array_equal(Y, Y_pred)
示例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, 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)
示例13: test_binary_ssvm_attractive_potentials
def test_binary_ssvm_attractive_potentials():
# test that submodular SSVM can learn the block dataset
X, Y = generate_blocks(n_samples=10)
crf = GridCRF(inference_method=inference_method)
submodular_clf = NSlackSSVM(model=crf, max_iter=200, C=100,
check_constraints=True,
negativity_constraint=[5])
submodular_clf.fit(X, Y)
Y_pred = submodular_clf.predict(X)
assert_array_equal(Y, Y_pred)
assert_true(submodular_clf.w[5] < 0)
示例14: test_blocks_crf_unaries
def test_blocks_crf_unaries():
X, Y = generate_blocks(n_samples=1)
x, _ = 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_states_per_label=2, n_labels=2, n_features=2)
h_hat = crf.inference(x, w)
assert_array_equal(h_hat // 2, np.argmax(x, axis=-1))
示例15: 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)