コード例 #1
0
def test_loss_augmented_inference_energy_graph():
    crf = LatentGraphCRF(n_labels=2, n_features=2, n_states_per_label=2)
    for i in range(10):
        w = np.random.normal(size=18)
        y = np.random.randint(2, size=(3))
        x = np.random.normal(size=(3, 2))
        e = np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int)
        h_hat, energy = crf.loss_augmented_inference((x, e), y * 2, w,
                                                     relaxed=True,
                                                     return_energy=True)
        assert_almost_equal(-energy, np.dot(w, crf.joint_feature((x, e), h_hat))
                            + crf.loss(y * 2, h_hat))
コード例 #2
0
def test_loss_augmented_inference_energy_graph():
    crf = LatentGraphCRF(n_labels=2, n_features=2, n_states_per_label=2,
                         inference_method='lp')
    for i in range(10):
        w = np.random.normal(size=18)
        y = np.random.randint(2, size=(3))
        x = np.random.normal(size=(3, 2))
        e = np.array([[0, 1], [1, 2], [2, 0]], dtype=np.int)
        h_hat, energy = crf.loss_augmented_inference((x, e), y * 2, w,
                                                     relaxed=True,
                                                     return_energy=True)
        assert_almost_equal(-energy, np.dot(w, crf.joint_feature((x, e), h_hat))
                            + crf.loss(y * 2, h_hat))