Exemple #1
0
def test_possible_segfaults_genetic():
    # Setting an incorrect value should not be allowed
    from ann import geneticnetwork
    net = geneticnetwork(2, 0, 1)

    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
    Y = np.array([[0], [1], [1], [0]], dtype=float)

    failed = False
    try:
        net.learn(0, Y)
    except ValueError:
        failed = True
    assert failed

    failed = False
    try:
        net.learn(X, 0)
    except ValueError:
        failed = True
    assert failed

    failed = False
    try:
        net.learn(0, 0)
    except ValueError:
        failed = True
    assert failed
def get_net(rows, incols, func=ann.geneticnetwork.FITNESS_SURV_KAPLAN_MIN, mingroup=None,
            hidden_count=3, popsize=100, generations=200, mutchance=0.15, conchance=0,
            crossover=ann.geneticnetwork.CROSSOVER_UNIFORM,
            selection=ann.geneticnetwork.SELECTION_TOURNAMENT):
    outcount = 2
    l = incols + hidden_count + outcount + 1

    net = ann.geneticnetwork(incols, hidden_count, outcount)
    net.fitness_function = func
    
    if mingroup is None:
        mingroup = int(0.25 * rows)
    
    # Be explicit here even though I changed the defaults
    net.connection_mutation_chance = conchance
    net.activation_mutation_chance = 0
    # Some other values
    net.crossover_method = crossover
    net.selection_method = selection
    net.population_size = popsize
    net.generations = generations
    net.weight_mutation_chance = mutchance


    ann.utils.connect_feedforward(net, hidden_act=net.TANH, out_act=net.SOFTMAX)
    c = net.connections.reshape((l, l))
    c[-outcount:, :(incols + hidden_count)] = 1
    net.connections = c.ravel()
    
    return net
Exemple #3
0
def test_possible_segfaults_genetic():
    # Setting an incorrect value should not be allowed
    from ann import geneticnetwork
    net = geneticnetwork(2, 0, 1)

    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
    Y = np.array([[0], [1], [1], [0]], dtype=float)

    failed = False
    try:
        net.learn(0, Y)
    except ValueError:
        failed = True
    assert failed

    failed = False
    try:
        net.learn(X, 0)
    except ValueError:
        failed = True
    assert failed

    failed = False
    try:
        net.learn(0, 0)
    except ValueError:
        failed = True
    assert failed
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
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
Exemple #6
0
def test_gennetwork():
    from ann import geneticnetwork

    net = geneticnetwork(2, 4, 1)

    xor_in, xor_out = getXOR()

    net.generations = 1000
    net.crossover_chance = 0.8
    net.connection_mutation_chance = 0.2
    net.activation_mutation_chance = 0.2
    net.crossover_method = net.CROSSOVER_UNIFORM
    net.fitness_function = net.FITNESS_MSE
    net.selection_method = net.SELECTION_TOURNAMENT

    net.learn(xor_in, xor_out)

    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"
Exemple #7
0
def test_gennetwork():
    from ann import geneticnetwork

    net = geneticnetwork(2, 4, 1)

    xor_in, xor_out = getXOR()

    net.generations = 1000
    net.crossover_chance = 0.8
    net.connection_mutation_chance = 0.2
    net.activation_mutation_chance = 0.2
    net.crossover_method = net.CROSSOVER_UNIFORM
    net.fitness_function = net.FITNESS_MSE
    net.selection_method = net.SELECTION_TOURNAMENT

    net.learn(xor_in, xor_out)

    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"