

def test_inference_trivial_features():
    features = np.array([(- 1), 1, (- 1), 1, (- 1), 1, 0, 0], dtype=np.float)
    unary_parameters = np.array([(- 1), 1, 0, 0])
    pairwise_parameters = np.array([(+ 0), (+ 0), 0, (+ 3), 0, 0, (+ 0), 3, 0, 0])
    w = np.hstack([unary_parameters, pairwise_parameters])
    crf = LatentNodeCRF(n_labels=2, n_features=1, n_hidden_states=2, latent_node_features=True)
    node_indices = np.arange(6)
    other_edges = []
    for n in node_indices[:3]:
        other_edges.append([n, 6])
    for n in node_indices[3:]:
        other_edges.append([n, 7])
    all_edges = np.vstack(other_edges)
    x = (features.reshape((- 1), 1), all_edges, 2)
    (h, energy_lp) = crf.inference(x, w, return_energy=True)
    assert_array_equal(h, [0, 0, 0, 1, 1, 1, 2, 3])
    y = crf.label_from_latent(h)
    assert_array_equal(y, [0, 0, 0, 1, 1, 1])
    y_unaries = np.argmax(crf._get_unary_potentials(x, w), axis=1)[:6]
    assert_array_equal(y_unaries, (features[:6] > 0))
    energy_joint_feature = np.dot(w, crf.joint_feature(x, h))
    assert_almost_equal(energy_joint_feature, (- energy_lp))
    h_unaries = crf.latent(x, y_unaries, w)
    assert_equal(crf.loss(h, h_unaries), 2)
    (h_continuous, energy_lp) = crf.inference(x, w, return_energy=True, relaxed=True)
    energy_joint_feature = np.dot(w, crf.joint_feature(x, h))
    assert_almost_equal(energy_joint_feature, (- energy_lp))
    assert_equal(crf.loss(h, h_continuous), 0)
    (h_hat, energy_lp) = crf.loss_augmented_inference(x, h, w, return_energy=True)
    assert_almost_equal((- energy_lp), (np.dot(w, crf.joint_feature(x, h_hat)) + crf.loss(h_hat, y)))

dataset/ETHPy150Open pystruct/pystruct/pystruct/tests/test_models/test_latent_node_crf.py test_inference_trivial_features/original