Esempio n. 1
0
def build_feedforward_multilayered(input_number = 2, hidden_numbers = [2], output_number = 1, hidden_function = "tanh", output_function = "logsig"):
    net = network()
    net.num_of_inputs = input_number
    inputs = range(input_number)
    
    biasnode = net.bias_node

    #Hidden layers
    prev_layer = inputs
    for hidden_number in hidden_numbers:
        current_layer = []
        for i in xrange(int(hidden_number)):
            hidden = node(hidden_function)
            connect_node(hidden, biasnode)
            connect_nodes(hidden, prev_layer)
            net.hidden_nodes.append(hidden)
            current_layer.append(hidden)
        prev_layer = current_layer

    #Output nodes
    for i in xrange(int(output_number)):
        output = node(output_function)
        connect_node(output, biasnode)
        connect_nodes(output, prev_layer)
        net.output_nodes.append(output)

    return net
Esempio n. 2
0
def crossover_node(mother, father):
    child = network()
    child.num_of_inputs = mother.num_of_inputs

    tr = {}
    for i in xrange(mother.num_of_inputs):
        tr[i] = i

    for bias in (mother.bias_node, father.bias_node):
        tr[bias] = child.bias_node

    for mother_node, father_node in zip(mother.hidden_nodes, father.hidden_nodes):
        child_node = node(mother_node.activation_function, 1)
        tr[mother_node] = child_node
        tr[father_node] = child_node

        #choose one node to pass on
        _choice = sample([mother_node, father_node], 1)[0]

        #map the weights and bias of that node to the child node
        for keynode, weight in _choice.weights.items():
            child_node.weights[tr[keynode]] = weight

        child.hidden_nodes.append(child_node)

    for mother_node, father_node in zip(mother.output_nodes, father.output_nodes):
        child_node = node(mother_node.activation_function, 1)
        tr[mother_node] = child_node
        tr[father_node] = child_node

        #choose one node to pass on
        _choice = sample([mother_node, father_node], 1)[0]

        #map the weights and bias of that node to the child node
        for keynode, weight in _choice.weights.items():
            child_node.weights[tr[keynode]] = weight

        child.output_nodes.append(child_node)

    return child
Esempio n. 3
0
            #glogger.debugPlot('Test Error\nMutation rate: ' + str(mutation_chance), best_error, style = 'r-', subset='best')
            #glogger.debugPlot('Test Error\nMutation rate: ' + str(mutation_chance), error[top_networks[0]], style = 'b-', subset='top')

            #Select the best 5 networks, mate them randomly and create 50 new networks
            population = []
            for child_index in xrange(population_size):
                [mother, father] = sample(top_networks, 2)
                if random() < cross_over_chance:
                    father = mother #Will create full mutation, no cross-over

                population.append(network())
                population[child_index].num_of_inputs = mother.num_of_inputs
                bias_child = population[child_index].bias_node
                #Hidden layer
                for mother_node, father_node in zip(mother.hidden_nodes, father.hidden_nodes):
                    hidden = node(mother_node.activation_function, random_range)
                    weights = {}
                    for input_number in xrange(mother.num_of_inputs):
                        choice = sample([mother_node, father_node], 1)[0]
                        weights[input_number] = choice.weights[input_number]
                    choice = sample([(mother, mother_node), (father, father_node)], 1)[0]
                    weights[bias_child] = choice[1].weights[choice[0].bias_node]
                    
                    _all = range(mother.num_of_inputs)
                    _all.append(bias_child)
                    connect_nodes(hidden, _all, weights)
                    population[child_index].hidden_nodes.append(hidden)

                hidden_nodes = population[child_index].hidden_nodes

                #Output nodes