def test_energy_continuous():
    np.random.seed(0)
    if True:
        found_fractional = False
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
        while (not found_fractional):
            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)
            (res, energy) = crf.inference(x, w, relaxed=True, return_energy=True)
            found_fractional = np.any((np.max(res[0], axis=(- 1)) != 1))
            joint_feature = crf.joint_feature(x, res)
            energy_svm = np.dot(joint_feature, w)
            assert_almost_equal(energy, (- energy_svm))
def test_energy_continuous():
    np.random.seed(0)
    if True:
        found_fractional = False
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
        while (not found_fractional):
            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)
            (res, energy) = crf.inference(x, w, relaxed=True, return_energy=True)
            found_fractional = np.any((np.max(res[0], axis=(- 1)) != 1))
            joint_feature = crf.joint_feature(x, res)
            energy_svm = np.dot(joint_feature, w)
            assert_almost_equal(energy, (- energy_svm))