Пример #1
0
def test_rbf_obj_and_obj_jac_match():
    """obj and obj_jac functions should return the same obj value."""
    attrs = random.randint(1, 10)
    outs = random.randint(1, 10)
    model = rbf.RBF(attrs, random.randint(1, 10), outs)

    dataset = datasets.get_random_regression(10, attrs, outs)

    # Don't use exactly the same parameters, to ensure obj functions are actually
    # using the given parameters
    parameters = random.uniform(-1.0, 1.0) * model._weight_matrix.ravel()
    assert helpers.approx_equal(
        model._get_obj(parameters, dataset[0], dataset[1]),
        model._get_obj_jac(parameters, dataset[0], dataset[1])[0])
Пример #2
0
def _check_jacobian(make_model_func):
    attrs = random.randint(1, 10)
    outs = random.randint(1, 10)

    model = make_model_func(attrs, random.randint(1, 10), outs)
    inp_matrix, tar_matrix = datasets.get_random_regression(10, attrs, outs)

    # Test jacobian of error function
    f = lambda xk: mlp._mlp_obj(model, inp_matrix, tar_matrix, xk)
    df = lambda xk: mlp._mlp_obj_jac(model, inp_matrix, tar_matrix, xk)[1]

    helpers.check_gradient(f,
                           df,
                           inputs=mlp._flatten(model._weight_matrices),
                           f_shape='scalar')
Пример #3
0
def _check_obj_and_obj_jac_match(make_model_func, classification=False):
    """obj and obj_jac functions should return the same obj value."""
    attrs = random.randint(1, 10)
    outs = random.randint(1, 10)
    model = make_model_func(attrs, random.randint(1, 10), outs)

    if classification:
        dataset = datasets.get_random_classification(10, attrs, outs)
    else:
        dataset = datasets.get_random_regression(10, attrs, outs)

    # Don't use exactly the same parameters, to ensure obj functions are actually
    # using the given parameters
    parameters = random.uniform(-1.0, 1.0) * mlp._flatten(
        model._weight_matrices)
    assert helpers.approx_equal(
        mlp._mlp_obj(model, dataset[0], dataset[1], parameters),
        mlp._mlp_obj_jac(model, dataset[0], dataset[1], parameters)[0])
Пример #4
0
def test_split_dataset():
    input_matrix, target_matrix = datasets.get_random_regression(
        random.randint(100, 150), random.randint(2, 5), random.randint(1, 3))
    num_sets = random.randint(2, 5)
    sets = validation._split_dataset(input_matrix, target_matrix, num_sets)

    # The right number of sets is created
    assert len(sets) == num_sets

    for i in range(num_sets):
        for j in range(i+1, num_sets):
            # Check that each set is about equal in size
            assert len(sets[i]) >= len(sets[j])-5 and len(sets[i]) <= len(sets[j])+5

            # Check that each set has unique patterns
            patterns = zip(*sets[i])
            other_patterns = zip(*sets[j])

            for pattern in patterns:
                for other_pattern in other_patterns:
                    assert not ((pattern[0] == other_pattern[0]).all()
                                and (pattern[1] == other_pattern[1]).all())
Пример #5
0
def test_get_random_regression_dataset():
    num_points = random.randint(1, 100)
    input_size = random.randint(1, 100)
    num_targets = random.randint(2, 10)

    input_matrix, target_matrix = datasets.get_random_regression(
        num_points, input_size, num_targets)

    assert len(input_matrix) == num_points
    assert len(target_matrix) == num_points

    for inp_vec, tar_vec in zip(input_matrix, target_matrix):
        assert len(inp_vec) == input_size
        assert len(tar_vec) == num_targets

        # Check values in range
        for val in inp_vec:
            assert -1 <= val <= 1

        # Target is a random vector
        for val in tar_vec:
            assert -1 <= val <= 1