Ejemplo n.º 1
0
def time_learn(rows, cols):
    '''Return time elapsed to learn 10000 iterations on data'''
    x, y = get_test_data(rows, cols)

    net = geneticnetwork(x.shape[1], 8, 1)
    connect_feedforward(net)

    net.generations = 10

    # Time it
    start = time.time()
    net.learn(x, y)
    # Final time
    elapsed = time.time() - start

    return elapsed
Ejemplo n.º 2
0
def time_learn(rows, cols):
    '''Return time elapsed to learn 10000 iterations on data'''
    x, y = get_test_data(rows, cols)

    net = geneticnetwork(x.shape[1], 8, 1)
    connect_feedforward(net)

    net.generations=10

    # Time it
    start = time.time()
    net.learn(x, y)
    # Final time
    elapsed = time.time() - start

    return elapsed
Ejemplo n.º 3
0
def time_learn(rows, cols):
    """Return time elapsed to learn 10000 iterations on data"""
    x, y = get_test_data(rows, cols)

    net = rpropnetwork(x.shape[1], 8, 1)
    connect_feedforward(net)

    net.maxError = 0
    net.maxEpochs = 1000

    # Time it
    start = time.time()
    net.learn(x, y)
    # Final time
    elapsed = time.time() - start

    return elapsed
Ejemplo n.º 4
0
def test_output():
    """Numpy array results in different output from Python list"""

    from ann import rpropnetwork
    from ann.utils import connect_feedforward

    data = np.random.normal(size=(10,6))
    np.random.shuffle(data)

    incols = list(range(2,6))
    outcols = [1, 0]

    net = rpropnetwork(len(incols), 3, len(outcols))
    connect_feedforward(net)

    for row in data[:3, incols]:
        np_out = net.output(row)
        py_out = net.output(list(row))

        for n, p in zip(np_out, py_out):
            assert n == p
Ejemplo n.º 5
0
def test_output():
    """Numpy array results in different output from Python list"""

    from ann import rpropnetwork
    from ann.utils import connect_feedforward

    data = np.random.normal(size=(10, 6))
    np.random.shuffle(data)

    incols = list(range(2, 6))
    outcols = [1, 0]

    net = rpropnetwork(len(incols), 3, len(outcols))
    connect_feedforward(net)

    for row in data[:3, incols]:
        np_out = net.output(row)
        py_out = net.output(list(row))

        for n, p in zip(np_out, py_out):
            assert n == p
Ejemplo n.º 6
0
def test_matrixnetwork():
    from ann import matrixnetwork

    net = matrixnetwork(2, 2, 1)

    # Default start is zero
    xor_in, xor_out = getXOR()
    for val in xor_in:
        assert net.output(val) == 0, "Expected zero output as default"

    length = net.input_count + net.hidden_count + net.output_count + 1

    # Weights
    #print("Length: ", len(net.weights))
    #print(net.weights)
    assert len(net.weights.ravel()) == length**2, "Wrong length of weight vector"
    assert np.all(net.weights == 0) == True, "Expected weights to equal zero"

    weights = net.weights.ravel()

    for i in range(len(weights)):
        weights[i] = 1.0
    net.weights = weights

    #print(net.weights)
    assert np.all(net.weights == 1.0) == True, "Expected weights to equal 1"

    # Connections
    #print("Length: ", len(net.connections))
    #print(net.connections)
    assert len(net.connections.ravel()) == length**2, "Wrong length of conns vector"
    assert np.all(net.connections == 0), "Expected conns to equal zero"
    conns = net.connections.ravel()
    for i in range(len(conns)):
        conns[i] = 1
    net.connections = conns

    #print(net.connections)
    assert np.all(net.connections == 1), "Expected conns to equal 1"

    # Default dropout value
    assert np.all(net.dropout_probabilities == 1), "Expected default dropout probs to be 1"
    dp = net.dropout_probabilities
    dp[:] = 0.5
    net.dropout_probabilities = dp
    assert np.all(net.dropout_probabilities == 0.5), "Expected dropout probs to be 0.5"

    # Set one again
    dp[:] = 1.0
    net.dropout_probabilities = dp
    assert np.all(net.dropout_probabilities == 1), "Expected dropout probs to be 1"

    # ActFuncs
    #print("Length: ", len(net.activation_functions))
    #print(net.activation_functions)
    assert len(net.activation_functions) == length, \
           "Wrong length of funcs vector"
    assert np.all(net.activation_functions == net.LOGSIG), \
           "Expected funcs to be LINEAR"

    actfuncs = net.activation_functions
    #actfuncs = np.zeros(len(net.activation_functions))
    for i in range(len(actfuncs)):
        actfuncs[i] = net.TANH
        #print(actfuncs)
    net.activation_functions = actfuncs

    #print(net.activation_functions)
    assert np.all(net.activation_functions == net.TANH), \
           "Expected actfuncs to be TANH"

    # OUTPUTS
    for val in xor_in:
        assert net.output(val) != 0, "Expected some output"


    # Solve XOR
    # set all conns to zero first
    conns[:] = 0
    net.connections = conns

    # Connect feedforward
    from ann.utils import connect_feedforward
    connect_feedforward(net)

    # We care only about first two neurons and output
    actfuncs[3] = net.LOGSIG
    actfuncs[4] = net.LOGSIG
    actfuncs[5] = net.LINEAR
    net.activation_functions = actfuncs

    weights[:] = 0
    weights[3*length + 0] = -60
    weights[3*length + 1] = 60
    weights[3*length + 2] = -30

    weights[4*length + 0] = 60
    weights[4*length + 1] = -60
    weights[4*length + 2] = -30

    weights[(length-1)*length + 3] = 1
    weights[(length-1)*length + 4] = 1

    net.weights = weights

    print(net.connections.reshape((6,6)))
    print(net.weights.reshape((6,6)))
    print(net.activation_functions)

    for val in xor_in:
        print("In:", val, " out:", net.output(val))
        if sum(val) != 1:
            assert net.output(val) < 0.1, "xor solution doesnt work"
        else:
            assert net.output(val) > 0.9, "xor solution doesnt work"

    conns[:] = 0
    net.connections = conns

    for val in xor_in:
        #print("In:", val, " out:", net.output(val))
        assert net.output(val) == 0, "no conns should mean zero output!"


    # Pickle test
    import pickle

    state = pickle.dumps(net, -1)

    net2 = pickle.loads(state)

    # Make sure it's the same
    assert np.all(net.weights == net2.weights), "weights don't match"

    assert np.all(net.connections == net2.connections), "conns don't match"

    assert np.all(net.activation_functions == net2.activation_functions),\
       "functions don't match"
Ejemplo n.º 7
0
def test_matrixnetwork():
    from ann import matrixnetwork

    net = matrixnetwork(2, 2, 1)

    # Default start is zero
    xor_in, xor_out = getXOR()
    for val in xor_in:
        assert net.output(val) == 0, "Expected zero output as default"

    length = net.input_count + net.hidden_count + net.output_count + 1

    # Weights
    #print("Length: ", len(net.weights))
    #print(net.weights)
    assert len(
        net.weights.ravel()) == length**2, "Wrong length of weight vector"
    assert np.all(net.weights == 0) == True, "Expected weights to equal zero"

    weights = net.weights.ravel()

    for i in range(len(weights)):
        weights[i] = 1.0
    net.weights = weights

    #print(net.weights)
    assert np.all(net.weights == 1.0) == True, "Expected weights to equal 1"

    # Connections
    #print("Length: ", len(net.connections))
    #print(net.connections)
    assert len(
        net.connections.ravel()) == length**2, "Wrong length of conns vector"
    assert np.all(net.connections == 0), "Expected conns to equal zero"
    conns = net.connections.ravel()
    for i in range(len(conns)):
        conns[i] = 1
    net.connections = conns

    #print(net.connections)
    assert np.all(net.connections == 1), "Expected conns to equal 1"

    # Default dropout value
    assert np.all(net.dropout_probabilities ==
                  1), "Expected default dropout probs to be 1"
    dp = net.dropout_probabilities
    dp[:] = 0.5
    net.dropout_probabilities = dp
    assert np.all(
        net.dropout_probabilities == 0.5), "Expected dropout probs to be 0.5"

    # Set one again
    dp[:] = 1.0
    net.dropout_probabilities = dp
    assert np.all(
        net.dropout_probabilities == 1), "Expected dropout probs to be 1"

    # ActFuncs
    #print("Length: ", len(net.activation_functions))
    #print(net.activation_functions)
    assert len(net.activation_functions) == length, \
           "Wrong length of funcs vector"
    assert np.all(net.activation_functions == net.LOGSIG), \
           "Expected funcs to be LINEAR"

    actfuncs = net.activation_functions
    #actfuncs = np.zeros(len(net.activation_functions))
    for i in range(len(actfuncs)):
        actfuncs[i] = net.TANH
        #print(actfuncs)
    net.activation_functions = actfuncs

    #print(net.activation_functions)
    assert np.all(net.activation_functions == net.TANH), \
           "Expected actfuncs to be TANH"

    # OUTPUTS
    for val in xor_in:
        assert net.output(val) != 0, "Expected some output"

    # Solve XOR
    # set all conns to zero first
    conns[:] = 0
    net.connections = conns

    # Connect feedforward
    from ann.utils import connect_feedforward
    connect_feedforward(net)

    # We care only about first two neurons and output
    actfuncs[3] = net.LOGSIG
    actfuncs[4] = net.LOGSIG
    actfuncs[5] = net.LINEAR
    net.activation_functions = actfuncs

    weights[:] = 0
    weights[3 * length + 0] = -60
    weights[3 * length + 1] = 60
    weights[3 * length + 2] = -30

    weights[4 * length + 0] = 60
    weights[4 * length + 1] = -60
    weights[4 * length + 2] = -30

    weights[(length - 1) * length + 3] = 1
    weights[(length - 1) * length + 4] = 1

    net.weights = weights

    print(net.connections.reshape((6, 6)))
    print(net.weights.reshape((6, 6)))
    print(net.activation_functions)

    for val in xor_in:
        print("In:", val, " out:", net.output(val))
        if sum(val) != 1:
            assert net.output(val) < 0.1, "xor solution doesnt work"
        else:
            assert net.output(val) > 0.9, "xor solution doesnt work"

    conns[:] = 0
    net.connections = conns

    for val in xor_in:
        #print("In:", val, " out:", net.output(val))
        assert net.output(val) == 0, "no conns should mean zero output!"

    # Pickle test
    import pickle

    state = pickle.dumps(net, -1)

    net2 = pickle.loads(state)

    # Make sure it's the same
    assert np.all(net.weights == net2.weights), "weights don't match"

    assert np.all(net.connections == net2.connections), "conns don't match"

    assert np.all(net.activation_functions == net2.activation_functions),\
       "functions don't match"