def test_energy_discrete():
    crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
    for i in range(10):
        x = np.random.normal(size=(7, 8, 3))
        edge_list = make_grid_edges(x, 4, return_lists=True)
        edges = np.vstack(edge_list)
        edge_features = edge_list_to_features(edge_list)
        x = ([x.reshape((- 1), 3)], [edges], [edge_features])
        unary_params = np.random.normal(size=(3, 3))
        pw1 = np.random.normal(size=(3, 3))
        pw2 = np.random.normal(size=(3, 3))
        w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
        crf.initialize(x)
        y_hat = crf.inference(x, w, relaxed=False)
        energy = compute_energy(crf._get_unary_potentials(x, w)[0], crf._get_pairwise_potentials(x, w)[0], edges, y_hat)
        joint_feature = crf.joint_feature(x, y_hat)
        energy_svm = np.dot(joint_feature, w)
        assert_almost_equal(energy, energy_svm)
def test_energy_discrete():
    crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
    for i in range(10):
        x = np.random.normal(size=(7, 8, 3))
        edge_list = make_grid_edges(x, 4, return_lists=True)
        edges = np.vstack(edge_list)
        edge_features = edge_list_to_features(edge_list)
        x = ([x.reshape((- 1), 3)], [edges], [edge_features])
        unary_params = np.random.normal(size=(3, 3))
        pw1 = np.random.normal(size=(3, 3))
        pw2 = np.random.normal(size=(3, 3))
        w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
        crf.initialize(x)
        y_hat = crf.inference(x, w, relaxed=False)
        energy = compute_energy(crf._get_unary_potentials(x, w)[0], crf._get_pairwise_potentials(x, w)[0], edges, y_hat)
        joint_feature = crf.joint_feature(x, y_hat)
        energy_svm = np.dot(joint_feature, w)
        assert_almost_equal(energy, energy_svm)