예제 #1
0
def test_setup_2_3_1():
    nn = js_neural_network()
    # data = [{imput: [], output: []}]
    data = [{
        'input': [1, 1],
        'output': [0]
    }, {
        'input': [1, 0],
        'output': [1]
    }, {
        'input': [0, 1],
        'output': [1]
    }, {
        'input': [0, 0],
        'output': [0]
    }]
    examples = nn.normalize(data)
    nn.setup(examples=examples, default_weight=0.1)
    assert len(nn.weights) == 2
    np.testing.assert_array_equal(nn.weights[0],
                                  np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1]]))
    np.testing.assert_array_equal(nn.weights[1], np.array([[0.1], [0.1],
                                                           [0.1]]))
    assert nn.weights[0].shape == (2, 3)
    assert nn.weights[1].shape == (3, 1)
예제 #2
0
def test_or_2_3_3():
    print('Testing ------- OR --------')
    nn = js_neural_network()  # (iterations=1)
    # data = [{imput: [], output: []}]
    data = [
        {
            'input': [1, 1, 1, 1],
            'output': [1, 0, 0]
        },
        {
            'input': [1, 1, 0, 1],
            'output': [0, 1, 0]
        },
        {
            'input': [1, 0, 0, 1],
            'output': [0, 0, 1]
        },
        # { 'input': [0, 0], 'output': [0, 0, 0] }
    ]
    # examples = nn.normalize(data)
    # nn.setup(examples=examples, default_weight=0.1)
    nn.learn(data)  # , default_weight=0.1)
    to_predict = [1, 1, 1, 1]
    res = nn.predict(to_predict)
    print('Prediction for {} = {}'.format(to_predict, res))
    to_predict = [1, 1, 0, 1]
    res = nn.predict(to_predict)
    print('Prediction for {} = {}'.format(to_predict, res))
    to_predict = [1, 0, 0, 1]
    res = nn.predict(to_predict)
    print('Prediction for {} = {}'.format(to_predict, res))
    to_predict = [1, 0, 1, 1]
    res = nn.predict(to_predict)
    print('Prediction for {} = {}'.format(to_predict, res))
예제 #3
0
def test_train_ocr_121():
    characters = alphabet_121()
    nn = js_neural_network()
    nn.learn(characters, default_weight=0.1)
    for ch in altered_c:
        res = nn.predict(ch['glyph'])
        print('Prediction for {} = {}'.format(ch['character'], res))
예제 #4
0
def test_train_ocr():
    characters = alphabet()
    nn = js_neural_network(iterations=1)  # ()
    nn.learn(characters)
    for ch in altered_c:
        res = nn.predict(ch['glyph'])
        print('Prediction for {} = {}'.format(ch['character'], res))
예제 #5
0
파일: test_wine.py 프로젝트: pajmd/ann
def test_wine():
    X_train, y_train, X_test, y_test = get_data()
    nn = gen_neural_network.js_neural_network(iterations=5000,
                                              hidden_units=13,
                                              learning_rate=0.001,
                                              hidden_layers=1)
    wine_data = {'input': np.array(X_train), 'output': np.array(y_train)}
    nn.learn(wine_data, default_weight=None, normalize_data=True)
    y_test = np.array(y_test)
    count = 0
    for i, x in enumerate(np.array(X_test)):
        prediction = nn.predict(x)
        if all(prediction == y_test[i]):
            count += 1
        else:
            print('x: {} prediction: {} expected: {}'.format(
                'x', prediction, y_test[i]))
    print('correct {} out of {}'.format(count, i))
예제 #6
0
def test_setup_2_3_2_random_weight():
    nn = js_neural_network()
    # data = [{imput: [], output: []}]
    data = [{
        'input': [1, 1],
        'output': [0, 1]
    }, {
        'input': [1, 0],
        'output': [1, 0]
    }, {
        'input': [0, 1],
        'output': [1, 0]
    }, {
        'input': [0, 0],
        'output': [0, 1]
    }]
    examples = nn.normalize(data)
    nn.setup(examples=examples)
    assert len(nn.weights) == 2
    assert nn.weights[0].shape == (3, 2)
    assert nn.weights[1].shape == (2, 3)
예제 #7
0
def test_normalize():
    nn = js_neural_network()
    # data = [{imput: [], output: []}]
    data = [{
        'input': [1, 1],
        'output': [0]
    }, {
        'input': [1, 0],
        'output': [1]
    }, {
        'input': [0, 1],
        'output': [1]
    }, {
        'input': [0, 0],
        'output': [0]
    }]
    examples = nn.normalize(data)
    assert len(examples) == 2
    assert examples['input'] is not None
    assert examples['output'] is not None
    np.testing.assert_array_equal(examples['input'],
                                  np.array([[1, 1], [1, 0], [0, 1], [0, 0]]))
    np.testing.assert_array_equal(examples['output'],
                                  np.array([[0], [1], [1], [0]]))