예제 #1
0
def test_transpose_rowcol_1d(type_func):
    # Should cause no change
    matrix = type_func(['s1t1', 's2t1'])

    assert helpers.fix_numpy_array_equality(
        multioutputs._transpose_rowcol(matrix) ==
        helpers.fix_numpy_array_equality(type_func(['s1t1', 's2t1'])))
예제 #2
0
def test_make_train_test_sets_2d_labels():
    inputs = numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0],
                          [0.0, 1.0], [1.0, 1.0]])
    labels = numpy.array([[0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [0, 1]])

    assert (helpers.fix_numpy_array_equality(
        validation.make_train_test_sets(
            inputs, labels, 1)) == helpers.fix_numpy_array_equality(
                ((inputs[:2], labels[:2]), (inputs[2:], labels[2:]))))
예제 #3
0
def test_validate_network(monkeypatch):
    # Patch time.clock so time attribute is deterministic
    monkeypatch.setattr(time, 'clock', lambda: 0.0)

    # This lets us test training error, but is kinda complicated
    model = helpers.WeightedSumModel()

    assert (helpers.fix_numpy_array_equality(
        _drop_model_stat(
            validation._validate_model(
                model, (numpy.array([[1], [1]]), numpy.array([[0], [1]])),
                (numpy.array([[1]]), numpy.array([[2]])),
                iterations=0,
                _classification=True))) == helpers.fix_numpy_array_equality({
                    'time':
                    0.0,
                    'epochs':
                    0,
                    'training_error':
                    0.5,
                    'testing_error':
                    1.0,
                    'training_accuracy':
                    0.5,
                    'training_confusion_matrix':
                    numpy.array([[0, 1, 0], [0, 1, 0], [0, 0, 0]]),
                    'testing_accuracy':
                    0.0,
                    'testing_confusion_matrix':
                    numpy.array([[0, 0, 0], [0, 0, 0], [0, 1, 0]])
                }))

    # Make network that returns set output for a given input
    # Simpler, always 0 training error
    model = helpers.RememberPatternsModel()
    assert (_drop_model_stat(
        validation._validate_model(model,
                                   (numpy.array([[1]]), numpy.array([[1]])),
                                   (numpy.array([[1]]), numpy.array([[1.5]])),
                                   iterations=0,
                                   _classification=False)) == {
                                       'time': 0.0,
                                       'epochs': 0,
                                       'training_error': 0.0,
                                       'testing_error': 0.25
                                   })
    assert (_drop_model_stat(
        validation._validate_model(model,
                                   (numpy.array([[0]]), numpy.array([[0]])),
                                   (numpy.array([[0]]), numpy.array([[2.0]])),
                                   iterations=0,
                                   _classification=False)) == {
                                       'time': 0.0,
                                       'epochs': 0,
                                       'training_error': 0.0,
                                       'testing_error': 4.0
                                   })
예제 #4
0
def test_matrix_col_2d_list_of_arrays():
    matrix = [numpy.array(['s1t1', 's1t2']), numpy.array(['s2t1', 's2t2'])]

    assert helpers.fix_numpy_array_equality(
        multioutputs._matrix_col(matrix, 0) ==
        helpers.fix_numpy_array_equality(['s1t1', 's2t1']))

    assert helpers.fix_numpy_array_equality(
        multioutputs._matrix_col(matrix, 1) ==
        helpers.fix_numpy_array_equality(['s1t2', 's2t2']))
예제 #5
0
def test_matrix_col_2d(type_func):
    matrix = type_func([['s1t1', 's1t2'], ['s2t1', 's2t2']])

    assert helpers.fix_numpy_array_equality(
        multioutputs._matrix_col(matrix, 0) ==
        helpers.fix_numpy_array_equality(type_func(['s1t1', 's2t1'])))

    assert helpers.fix_numpy_array_equality(
        multioutputs._matrix_col(matrix, 1) ==
        helpers.fix_numpy_array_equality(type_func(['s1t2', 's2t2'])))
예제 #6
0
def test_transpose_rowcol_3d(type_func):
    matrix = type_func([[['s1t11', 's1t12'], ['s1t21', 's1t22']],
                        [['s2t11', 's2t12'], ['s2t21', 's2t22']]])

    assert helpers.fix_numpy_array_equality(
        multioutputs._transpose_rowcol(matrix) ==
        helpers.fix_numpy_array_equality(
            type_func([(
                ['s1t11', 's1t12'],
                ['s2t11', 's2t12']), (['s1t21', 's1t22'],
                                      ['s2t21', 's2t22'])])))
예제 #7
0
def test_serialize_unserialize():
    dataset = (numpy.random.random((10, 10)), numpy.random.random((10, 2, 10)))

    model = multioutputs.MultiOutputs(MLP((10, 2, 10)), 2)
    unserialized_model = multioutputs.MultiOutputs.unserialize(
        model.serialize())

    assert isinstance(unserialized_model, multioutputs.MultiOutputs)
    assert (helpers.fix_numpy_array_equality([
        model.activate(inp_vec) for inp_vec in dataset[0]
    ]) == helpers.fix_numpy_array_equality(
        [unserialized_model.activate(inp_vec) for inp_vec in dataset[0]]))
예제 #8
0
def test_benchmark(monkeypatch):
    # Patch time.clock so time attribute is deterministic
    monkeypatch.setattr(time, 'clock', lambda: 0.0)

    # Make network that returns set output for
    patterns = [([0], [1]), ([1], [1]), ([2], [1])]
    model = helpers.SetOutputModel([1])

    # Track patterns for training
    training_patterns = []

    def post_pattern_callback(network_, input_vec, target_vec):
        training_patterns.append((list(input_vec), list(target_vec)))

    # Cross validate with deterministic network, and check output
    stats = validation.benchmark(
        model,
        zip(*patterns),
        num_folds=3,
        num_runs=2,
        iterations=1,
        post_pattern_callback=post_pattern_callback)

    # Check
    assert (helpers.fix_numpy_array_equality(stats) ==
            helpers.fix_numpy_array_equality(_BENCHMARK_STATS))
    assert training_patterns == [
        # First fold
        ([1], [1]),
        ([2], [1]),
        # Second fold
        ([0], [1]),
        ([2], [1]),
        # Third fold
        ([0], [1]),
        ([1], [1]),
        # First fold 2
        ([1], [1]),
        ([2], [1]),
        # Second fold 2
        ([0], [1]),
        ([2], [1]),
        # Third fold 2
        ([0], [1]),
        ([1], [1])
    ]
예제 #9
0
def test_fix_numpy_array_equality():
    complex_obj = [(numpy.array([0, 1, 2]), 'thing', []),
                   numpy.array([0, 1]),
                   [numpy.array([0, 1]),
                    numpy.array([0]), [0, 1, 2]]]

    assert helpers.fix_numpy_array_equality(complex_obj) == \
        [(helpers.sane_equality_array([0, 1, 2]), 'thing', []), helpers.sane_equality_array([0, 1]),
         [helpers.sane_equality_array([0, 1]), helpers.sane_equality_array([0]), [0, 1, 2]]]
예제 #10
0
def test_compare(monkeypatch):
    # Patch time.clock so time attribute is deterministic
    monkeypatch.setattr(time, 'clock', lambda: 0.0)

    # Make network that returns set output for
    patterns = [([0], [1]), ([1], [1]), ([2], [1])]
    model = helpers.SetOutputModel([1])
    model2 = helpers.SetOutputModel([1])

    # Cross validate with deterministic network, and check output
    stats = validation.compare(['model', 'model2'], [model, model2],
                               zip(*patterns),
                               num_folds=3,
                               num_runs=2,
                               all_kwargs={'iterations': 1})

    # Check
    assert (helpers.fix_numpy_array_equality(_drop_models_model_stat(stats)) ==
            helpers.fix_numpy_array_equality(_COMPARE_STATS))
예제 #11
0
def test_transpose_rowcol_2d_list_of_arrays():
    matrix = [numpy.array(['s1t1', 's1t2']), numpy.array(['s2t1', 's2t2'])]

    assert helpers.fix_numpy_array_equality(
        multioutputs._transpose_rowcol(matrix) ==
        helpers.fix_numpy_array_equality([('s1t1', 's2t1'), ('s1t2', 's2t2')]))