本文整理汇总了Python中pystruct.models.LatentGridCRF.loss方法的典型用法代码示例。如果您正苦于以下问题:Python LatentGridCRF.loss方法的具体用法?Python LatentGridCRF.loss怎么用?Python LatentGridCRF.loss使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pystruct.models.LatentGridCRF
的用法示例。
在下文中一共展示了LatentGridCRF.loss方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_continuous_y
# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import loss [as 别名]
def test_continuous_y():
for inference_method in ["lp", "ad3"]:
X, Y = toy.generate_blocks(n_samples=1)
x, y = X[0], Y[0]
w = np.array([1, 0, 0, 1, 0, -4, 0]) # unary # pairwise
crf = LatentGridCRF(n_labels=2, n_states_per_label=1, inference_method=inference_method)
psi = crf.psi(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
psi_cont = crf.psi(x, (y_cont, pw))
assert_array_almost_equal(psi, psi_cont)
const = find_constraint(crf, x, y, w, relaxed=False)
const_cont = find_constraint(crf, x, y, w, relaxed=True)
# dpsi and loss are equal:
assert_array_almost_equal(const[1], const_cont[1])
assert_almost_equal(const[2], const_cont[2])
# returned y_hat is one-hot version of other
assert_array_equal(const[0], np.argmax(const_cont[0][0], axis=-1))
# test loss:
assert_equal(crf.loss(y, const[0]), crf.continuous_loss(y, const_cont[0][0]))
示例2: test_blocks_crf
# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import loss [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)
示例3: test_continuous_y
# 需要导入模块: from pystruct.models import LatentGridCRF [as 别名]
# 或者: from pystruct.models.LatentGridCRF import loss [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, # unary
0, 1,
0, # pairwise
-4, 0])
crf = LatentGridCRF(n_labels=2, n_features=2, n_states_per_label=1,
inference_method=inference_method)
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, 4)
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)
if isinstance(const_cont[0], tuple):
# returned y_hat is one-hot version of other
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)