Пример #1
0
    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 = _Model(state_machine, x, y)
        print(test_model._lattice)

        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)
Пример #2
0
def test_derivate_large():
    classes = ['a', 'b', 'c']
    y = 'b'
    x = random.randn(20, 3, 10) * 5 + 3
    state_machine, states_to_classes = Hacrf._default_state_machine(classes)
    parameters = Hacrf._initialize_parameters(state_machine, x.shape[2])
    parameters = random.randn(*parameters.shape) * 10 - 2

    test_model = _Model(state_machine, states_to_classes, x, y)
    expected_dll = np.zeros(parameters.shape)

    # Finite difference gradient approximation
    delta = 10.0**-7
    S, D = expected_dll.shape
    for s in xrange(S):
        for d in xrange(D):
            dg = np.zeros(parameters.shape)
            dg[s, d] = delta
            y0, _ = test_model.forward_backward(parameters)
            y1, _ = test_model.forward_backward(parameters + dg)
            expected_dll[s, d] = (y1 - y0) / delta

    actual_ll, actual_dll = test_model.forward_backward(parameters)

    print(abs(actual_dll) - abs(expected_dll)).sum()
    assert_array_almost_equal(actual_dll, expected_dll, decimal=4)
Пример #3
0
def test_derivate_large():
    classes = ['a', 'b', 'c']
    y = 'b'
    x = random.randn(20, 3, 10) * 5 + 3
    state_machine, states_to_classes = Hacrf._default_state_machine(classes)
    parameters = Hacrf._initialize_parameters(state_machine, x.shape[2])
    parameters = random.randn(*parameters.shape) * 10 - 2

    test_model = _Model(state_machine, states_to_classes, x, y)
    expected_dll = np.zeros(parameters.shape)

    # Finite difference gradient approximation
    delta = 10.0**-7
    S, D = expected_dll.shape
    for s in xrange(S):
        for d in xrange(D):
            dg = np.zeros(parameters.shape)
            dg[s, d] = delta
            y0, _ = test_model.forward_backward(parameters)
            y1, _ = test_model.forward_backward(parameters + dg)
            expected_dll[s, d] = (y1 - y0) / delta

    actual_ll, actual_dll = test_model.forward_backward(parameters)

    print (abs(actual_dll) - abs(expected_dll)).sum()
    assert_array_almost_equal(actual_dll, expected_dll, decimal=4)
Пример #4
0
    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)
Пример #5
0
    def test_initialize_parameters(self):
        start_states = [0]
        transitions = [(0, 0, (1, 1)),
                       (0, 1, (0, 1)),
                       (0, 0, (1, 0))]
        states_to_classes = {0: 'a'}
        state_machine = GeneralStateMachine(start_states=start_states,
                                            transitions=transitions,
                                            states_to_classes=states_to_classes)

        n_features = 3

        actual_parameters = Hacrf._initialize_parameters(state_machine, n_features)
        expected_parameter_shape = (5, 3)
        self.assertEqual(actual_parameters.shape, expected_parameter_shape)
Пример #6
0
    def test_initialize_parameters(self):
        start_states = [0]
        transitions = [(0, 0, (1, 1)), (0, 1, (0, 1)), (0, 0, (1, 0))]
        states_to_classes = {0: 'a'}
        state_machine = GeneralStateMachine(
            start_states=start_states,
            transitions=transitions,
            states_to_classes=states_to_classes)

        n_features = 3

        actual_parameters = Hacrf._initialize_parameters(
            state_machine, n_features)
        expected_parameter_shape = (5, 3)
        self.assertEqual(actual_parameters.shape, expected_parameter_shape)