def test_piecewise_linear_multiplied(self): piecewise_spn = ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[1])) evidence = np.array([ [-2, -2], [-1.5, -1.5], [-1, -1], [-0.5, -0.5], [0, 0], [0.5, 0.5], [1, 1], [1.5, 1.5], [2, 2], [3, 3], [-3, -3], [0, 100], ]) results = likelihood(piecewise_spn, evidence) expected_results = np.array([[0], [0.25], [0.5], [0.25], [0], [0.25], [0.5], [0.25], [0], [0], [0], [0]])**2 self.assertTrue(np.all(np.equal(results, expected_results)))
def test_piecewise_linear_simple(self): piecewise_spn = 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + \ 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0]) self.assertTrue(is_valid(piecewise_spn)) mean = get_means(piecewise_spn) self.assertTrue(np.all(mean == np.array([[0]])))
def test_piecewise_linear_simple(self): piecewise_spn = 0.5 * PiecewiseLinear( [0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear( [-2, -1, 0], [0, 1, 0], [], scope=[0]) self.assertTrue(is_valid(piecewise_spn)) mean = get_mean(piecewise_spn) self.assertAlmostEqual(np.array([[0]]), mean, 5)
def test_piecewise_linear_constant(self): piecewise_spn = ((0.5 * PiecewiseLinear([1, 2], [1, 1], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1], [1, 1], [], scope=[0]))) evidence = np.array([[-3000]]) results = likelihood(piecewise_spn, evidence) expected_results = np.array([[1]]) self.assertTrue(np.all(np.equal(results, expected_results)))
def test_piecewise_linear_simple(self): piecewise_spn = 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + \ 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0]) evidence = np.array([[-2], [-1.5], [-1], [-0.5], [0], [0.5], [1], [1.5], [2], [3], [-3]]) results = likelihood(piecewise_spn, evidence) expected_results = np.array([[0], [0.25], [0.5], [0.25], [0], [0.25], [0.5], [0.25], [0], [0], [0]]) self.assertTrue(np.all(np.equal(results, expected_results)))
def test_histogram_combined(self): piecewise_spn = ((0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * (0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-1, 0, 1], [0, 1, 0], [], scope=[1]))) self.assertTrue(is_valid(piecewise_spn)) mean = get_means(piecewise_spn) self.assertTrue(np.all(mean == np.array([[0., 0.5]])))
def test_histogram_combined(self): piecewise_spn = ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-1, 0, 1], [0, 1, 0], [], scope=[1])) self.assertTrue(is_valid(piecewise_spn)) mean = get_mean(piecewise_spn) self.assertAlmostEqual(0.0, mean[0, 0], 5) self.assertAlmostEqual(0.5, mean[0, 1], 5)
def test_piecewise_linear_simple(self): piecewise_spn = 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + \ 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0]) self.assertTrue(is_valid(piecewise_spn)) evidence = np.array([[0.5], [1.5], [-0.5], [-1.5]]) results = gradient_forward(piecewise_spn, evidence) expected_results = np.array([[0.5], [-0.5], [-0.5], [0.5]]) for i, _ in enumerate(evidence): self.assertTrue( results[i] == expected_results[i], 'Expected result was {}, but computed result was {}'.format( expected_results[i], results[i]))
def test_piecewise_leaf(self): piecewise1 = PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) piecewise2 = PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0]) self.assertTrue(is_valid(piecewise1)) self.assertTrue(is_valid(piecewise2)) self.assertTrue( np.array_equal(mpe(piecewise1, np.array([[np.nan]])), np.array([[1]])), "mpe should be 1") self.assertTrue( np.array_equal(mpe(piecewise2, np.array([[np.nan]])), np.array([[-1]])), "mpe should be -1") with self.assertRaises(AssertionError) as error: mpe(piecewise1, np.array([[1]]))
def create_piecewise_node(x_range, y_range, scope): x_range, y_range = np.array(x_range), np.array(y_range) auc = np.trapz(y_range, x_range) y_range = y_range / auc return PiecewiseLinear(x_range=x_range, y_range=y_range, bin_repr_points=x_range[1:-1], scope=scope)
def piecewise_tree_to_spn(tree, features, obj_type, tree_to_spn): node = PiecewiseLinear(list(map(float, tree.children[1].children)), list(map(float, tree.children[2].children))) feature = str(tree.children[0]) node.scope.append(features.index(feature)) return node
def test_piecewise_linear_combined(self): piecewise_spn = ( (0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * (0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-1, 0, 1], [0, 1, 0], [], scope=[1]))) self.assertTrue(is_valid(piecewise_spn)) evidence = np.array([[0.5, 0], [100, 36], [-0.5, -0.5], [-1.5, 0.5]]) results = gradient_forward(piecewise_spn, evidence) expected_results = np.array([[0.25, 0.125], [0, 0], [-0.125, 0.125], [0.25, 0]]) for i, _ in enumerate(evidence): self.assertTrue( np.all(np.equal(results[i], expected_results[i])), 'Expected result was {}, but computed result was {}'.format( expected_results[i], results[i]))
def test_piecewise_linear_combined(self): piecewise_spn = ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-1, 0, 1], [0, 1, 0], [], scope=[1])) self.assertTrue(is_valid(piecewise_spn)) evidence = np.array([[0.5, 0], [-0.5, -0.5], [-1.5, 0.5]]) results = feature_gradient(piecewise_spn, evidence) expected_results = np.array([[0.25, 0.125], [-0.125, 0.125], [0.25, 0]]) self.assertTrue( np.all(np.isclose(results, expected_results, atol=0.000001)), "Expected result was {}, but computed result was {}".format( expected_results, results), )
def compute_expectations(): import numpy as np from spn.structure.leaves.piecewise.PiecewiseLinear import PiecewiseLinear from spn.algorithms.stats.Expectations import Expectation piecewise_spn = ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[0]) + 0.5 * PiecewiseLinear([-2, -1, 0], [0, 1, 0], [], scope=[0])) * ( 0.5 * PiecewiseLinear([0, 1, 2], [0, 1, 0], [], scope=[1]) + 0.5 * PiecewiseLinear([-1, 0, 1], [0, 1, 0], [], scope=[1])) print("Full Expectation: {}".format(Expectation(piecewise_spn))) print("Expectation for feature scope 0: {}".format( Expectation(piecewise_spn, feature_scope=[0]))) print("Expectation for feature scope 1: {}".format( Expectation(piecewise_spn, feature_scope=[1]))) print("Expectation with evidence 0: {}".format( Expectation(piecewise_spn, feature_scope=[0], evidence=np.array([[np.nan, 0]])))) print("Expectation with evidence 1: {}".format( Expectation(piecewise_spn, feature_scope=[1], evidence=np.array([[0, np.nan]]))))
rand_gen = np.random.RandomState(100) #Create SPN node1 = Categorical(p=[0.9, 0.1], scope=[0]) node2 = Categorical(p=[0.1, 0.9], scope=[0]) x = [0., 1., 2., 3., 4.] y = [0., 10., 0., 0., 0.] x, y = np.array(x), np.array(y) auc = np.trapz(y, x) y = y / auc node3 = PiecewiseLinear(x_range=x, y_range=y, bin_repr_points=x[1:-1], scope=[1]) x = [0., 1., 2., 3., 4.] y = [0., 0., 0., 10., 0.] x, y = np.array(x), np.array(y) auc = np.trapz(y, x) y = y / auc node4 = PiecewiseLinear(x_range=x, y_range=y, bin_repr_points=x[1:-1], scope=[1]) root_node = 0.49 * (node1 * node3) + 0.51 * (node2 * node4) #Set context #meta_types = [MetaType.DISCRETE, MetaType.REAL] #domains = [[0,1],[0.,4.]] #ds_context = Context(meta_types=meta_types, domains=domains)