def test_derivate_large(self): classes = ['a', 'b', 'c'] y = 'b' x = random.randn(8, 3, 10) * 5 + 3 state_machine = DefaultStateMachine(classes) parameters = Hacrf._initialize_parameters(state_machine, x.shape[2]) parameters = random.randn(*parameters.shape) * 10 - 2 test_model = _AdjacentModel(state_machine, x, y) expected_dll = np.zeros(parameters.shape) # Finite difference gradient approximation delta = 10.0**-7 S, D = expected_dll.shape for s in range(S): for d in range(D): dg = np.zeros(parameters.shape) dg[s, d] = delta y0, _ = test_model.forward_backward(parameters) y1, _ = test_model.forward_backward(parameters + dg) print(s, d, y0, y1) expected_dll[s, d] = (y1 - y0) / delta actual_ll, actual_dll = test_model.forward_backward(parameters) print(expected_dll) print(actual_dll) self.assertEqual((np.isnan(actual_dll)).any(), False) assert_array_almost_equal(actual_dll, expected_dll, decimal=TEST_PRECISION)
def test_derivate_medium(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype='float64').reshape((8, 2)) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0.0]]]) y = 'a' state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) expected_dll = np.zeros(parameters.shape) # Finite difference gradient approximation delta = 10.0**-7 S, D = expected_dll.shape for s in range(S): for d in range(D): dg = np.zeros(parameters.shape) dg[s, d] = delta y0, _ = test_model.forward_backward(parameters) y1, _ = test_model.forward_backward(parameters + dg) print(s, d, y0, y1) expected_dll[s, d] = (y1 - y0) / delta actual_ll, actual_dll = test_model.forward_backward(parameters) print(expected_dll) print(actual_dll) assert_array_almost_equal(actual_dll, expected_dll, decimal=TEST_PRECISION)
def test_backward_connected(self): parameters = np.array(range(-4, 4), dtype=np.float64).reshape((4, 2)) # parameters = #0([[-4, -3], #1 [-2, -1], #2 [ 0, 1], #3 [ 2, 3]]) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' expected_beta = { (0, 0, 0): -3.872776558098594, (0, 0, 0, 0, 1, 0, 2): -13, (0, 0, 0, 1, 0, 0, 3): -7, (0, 0, 0, 1, 1, 0, 1): -4, (0, 1, 0): -2.0, (0, 1, 0, 1, 1, 0, 3): -4.0, (1, 0, 0): -4.0, (1, 0, 0, 1, 1, 0, 2): -4.0, (1, 1, 0): 0.0 } state_machine = DefaultStateMachine(['a']) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_beta = test_model._backward(x_dot_parameters) print(sorted(actual_beta.items())) print(sorted(expected_beta.items())) self.assertEqual(len(actual_beta), len(expected_beta)) for key in sorted(expected_beta.keys(), reverse=True): print(key, expected_beta[key], actual_beta[key]) self.assertAlmostEqual(actual_beta[key], expected_beta[key])
def test_forward_connected(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype=np.float64).reshape((8, 2)) # parameters = #0([[-8, -7], #1 [-6, -5], #2 [-4, -3], #3 [-2, -1], #4 [ 0, 1], #5 [ 2, 3], #6 [ 4, 5], #7 [ 6, 7]]) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' expected_alpha = { (0, 0, 0): np.exp(-7), (0, 0, 0, 0, 1, 0, 4): np.exp(-7) * np.exp(1), (0, 0, 0, 1, 0, 0, 6): np.exp(-7) * np.exp(5), (0, 0, 0, 1, 1, 0, 2): np.exp(-7) * np.exp(-4), (0, 0, 1): np.exp(-5), (0, 0, 1, 0, 1, 1, 5): np.exp(-5) * np.exp(7), (0, 0, 1, 1, 0, 1, 7): np.exp(-5) * np.exp(7), (0, 0, 1, 1, 1, 1, 3): np.exp(-5) * np.exp(-2), (0, 1, 0): np.exp(-7) * np.exp(1) * np.exp(-23), (0, 1, 0, 1, 1, 0, 6): np.exp(-7) * np.exp(1) * np.exp(-23) * np.exp(4), (0, 1, 1): np.exp(-5) * np.exp(7) * np.exp(-17), (0, 1, 1, 1, 1, 1, 7): np.exp(-5) * np.exp(7) * np.exp(-17) * np.exp(6), (1, 0, 0): np.exp(-7) * np.exp(5) * np.exp(-7), (1, 0, 0, 1, 1, 0, 4): np.exp(-7) * np.exp(5) * np.exp(-7) * np.exp(0), (1, 0, 1): np.exp(-5) * np.exp(7) * np.exp(-5), (1, 0, 1, 1, 1, 1, 5): np.exp(-5) * np.exp(7) * np.exp(-5) * np.exp(2), (1, 1, 0): (np.exp(-11) + np.exp(-25) + np.exp(-9)) * np.exp(-8), (1, 1, 1): (np.exp(-1) + np.exp(-9) + np.exp(-7)) * np.exp(-6) } expected_alpha = { k: np.emath.log(v) for k, v in expected_alpha.items() } state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_alpha = test_model._forward(x_dot_parameters) self.assertEqual(len(actual_alpha), len(expected_alpha)) for key in sorted(expected_alpha.keys()): try: expected_alpha[key], actual_alpha[key] except: print(key) print('expected', sorted(expected_alpha)) print('actual', sorted(actual_alpha)) raise self.assertAlmostEqual(actual_alpha[key], expected_alpha[key])
def test_forward_connected(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype=np.float64).reshape((8, 2)) # parameters = #0([[-8, -7], #1 [-6, -5], #2 [-4, -3], #3 [-2, -1], #4 [ 0, 1], #5 [ 2, 3], #6 [ 4, 5], #7 [ 6, 7]]) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' expected_alpha = { (0, 0, 0): np.exp(-7), (0, 0, 0, 0, 1, 0, 4): np.exp(-7) * np.exp(1), (0, 0, 0, 1, 0, 0, 6): np.exp(-7) * np.exp(5), (0, 0, 0, 1, 1, 0, 2): np.exp(-7) * np.exp(-4), (0, 0, 1): np.exp(-5), (0, 0, 1, 0, 1, 1, 5): np.exp(-5) * np.exp(7), (0, 0, 1, 1, 0, 1, 7): np.exp(-5) * np.exp(7), (0, 0, 1, 1, 1, 1, 3): np.exp(-5) * np.exp(-2), (0, 1, 0): np.exp(-7) * np.exp(1) * np.exp(-23), (0, 1, 0, 1, 1, 0, 6): np.exp(-7) * np.exp(1) * np.exp(-23) * np.exp(4), (0, 1, 1): np.exp(-5) * np.exp(7) * np.exp(-17), (0, 1, 1, 1, 1, 1, 7): np.exp(-5) * np.exp(7) * np.exp(-17) * np.exp(6), (1, 0, 0): np.exp(-7) * np.exp(5) * np.exp(-7), (1, 0, 0, 1, 1, 0, 4): np.exp(-7) * np.exp(5) * np.exp(-7) * np.exp(0), (1, 0, 1): np.exp(-5) * np.exp(7) * np.exp(-5), (1, 0, 1, 1, 1, 1, 5): np.exp(-5) * np.exp(7) * np.exp(-5) * np.exp(2), (1, 1, 0): (np.exp(-11) + np.exp(-25) + np.exp(-9)) * np.exp(-8), (1, 1, 1): (np.exp(-1) + np.exp(-9) + np.exp(-7)) * np.exp(-6) } expected_alpha = {k: np.emath.log(v) for k, v in expected_alpha.items()} state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_alpha = test_model._forward(x_dot_parameters) self.assertEqual(len(actual_alpha), len(expected_alpha)) for key in sorted(expected_alpha.keys()): try: expected_alpha[key], actual_alpha[key] except: print(key) print('expected', sorted(expected_alpha)) print('actual', sorted(actual_alpha)) raise self.assertAlmostEqual(actual_alpha[key], expected_alpha[key])
def test_derivate_chain(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype='float64').reshape((8, 2)) # parameters = #0([[-8, -7], #1 [-6, -5], #2 [-4, -3], #3 [-2, -1], #4 [ 0, 1], #5 [ 2, 3], #6 [ 4, 5], #7 [ 6, 7]]) x = np.array([[[0, 1], [1, 2]]], dtype='float64') y = 'a' state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) # # 0 01 --- 01 # 0 1 # states_to_classes = {0: 'a', 1: 'b'} # (0, 0, 0) : exp(-7) # (0, 0, 0, 0, 1, 0, 4) : exp(-7) * exp(2) # (0, 0, 1) : exp(-5) # (0, 0, 1, 0, 1, 1, 5) : exp(-5) * exp(8) # (0, 1, 0) : exp(-7) * exp(2) * exp(-8 - 14) = exp(-27) # (0, 1, 1) : exp(-5) * exp(8) * exp(-6 - 10) = exp(-13) # p(y|G,X) = f0(g00,g01,x00,x01,y) f1(g40,g41,x10,x11,y) f2(g00,g01,x00,x01,y) + # f0(g10,g11,x00,x01,y) f1(g50,g51,x10,x11,y) f2(g10,g11,x00,x01,y) # = exp(-27) / (exp(-27) + exp(-13)) expected_ll = np.emath.log(np.exp(-27) / (np.exp(-27) + np.exp(-13))) expected_dll = np.zeros(parameters.shape) # Finite difference gradient approximation delta = 10.0**-7 S, D = expected_dll.shape for s in range(S): for d in range(D): dg = np.zeros(parameters.shape) dg[s, d] = delta y0, _ = test_model.forward_backward(parameters) y1, _ = test_model.forward_backward(parameters + dg) print(s, d, y0, y1) expected_dll[s, d] = (y1 - y0) / delta actual_ll, actual_dll = test_model.forward_backward(parameters) print(expected_ll, actual_ll) print(expected_dll) print(actual_dll) self.assertAlmostEqual(actual_ll, expected_ll) assert_array_almost_equal(actual_dll, expected_dll, decimal=TEST_PRECISION)
def test_forward_backward_same_partition_value(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype='float64').reshape((8, 2)) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_alpha = test_model._forward(x_dot_parameters) actual_beta = test_model._backward(x_dot_parameters) print(actual_alpha[(1, 1, 0)], actual_beta[(0, 0, 0)]) print(actual_alpha[(1, 1, 1)], actual_beta[(0, 0, 1)]) self.assertAlmostEqual(actual_alpha[(1, 1, 0)], actual_beta[(0, 0, 0)] + (np.dot(x[0, 0, :], parameters[0, :]))) self.assertAlmostEqual(actual_alpha[(1, 1, 1)], actual_beta[(0, 0, 1)] + (np.dot(x[0, 0, :], parameters[1, :])))
def test_forward_backward_same_partition_value(self): classes = ['a', 'b'] parameters = np.array(range(-8, 8), dtype='float64').reshape((8, 2)) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' state_machine = DefaultStateMachine(classes) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_alpha = test_model._forward(x_dot_parameters) actual_beta = test_model._backward(x_dot_parameters) print(actual_alpha[(1, 1, 0)], actual_beta[(0, 0, 0)]) print(actual_alpha[(1, 1, 1)], actual_beta[(0, 0, 1)]) self.assertAlmostEqual( actual_alpha[(1, 1, 0)], actual_beta[(0, 0, 0)] + (np.dot(x[0, 0, :], parameters[0, :]))) self.assertAlmostEqual( actual_alpha[(1, 1, 1)], actual_beta[(0, 0, 1)] + (np.dot(x[0, 0, :], parameters[1, :])))
def test_backward_connected(self): parameters = np.array(range(-3, 3), dtype=np.float64).reshape((3, 2)) # parameters = #0 ([[-3, -2], #1 [-1, 0], #2 [ 1, 2]]) x = np.array([[[0, 1], [2, 1]], [[0, 1], [1, 0]]], dtype=np.float64) y = 'a' expected_beta = { (0, 0, 0): -3.905077043579039, (0, 0, 0, 0, 1, 0, 2): -11.0, (0, 0, 0, 1, 0, 0, 3): -4.0, (0, 0, 0, 1, 1, 0, 1): -3.0, (0, 1, 0): -3.0, (0, 1, 0, 1, 1, 0, 3): -3.0, (1, 0, 0): -2.0, (1, 0, 0, 1, 1, 0, 2): -3.0, (1, 1, 0): 0.0} state_machine = DefaultStateMachine(['a']) test_model = _AdjacentModel(state_machine, x, y) x_dot_parameters = np.dot(x, parameters.T) # Pre-compute the dot product actual_beta = test_model._backward(x_dot_parameters) print(sorted(actual_beta.items())) print(sorted(expected_beta.items())) print self.assertEqual(len(actual_beta), len(expected_beta)) for key in sorted(expected_beta.keys(), reverse=True): print(key, expected_beta[key], actual_beta[key]) self.assertAlmostEqual(actual_beta[key], expected_beta[key])