def create_recurrent_phenotype(genome): """ Receives a genome and returns its phenotype (a recurrent neural network). """ # Gather inputs and expressed connections. input_nodes = [ ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT' ] output_nodes = [ ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT' ] node_inputs = {} used_nodes = set(input_nodes + output_nodes) for cg in genome.conn_genes.values(): if not cg.enabled: continue used_nodes.add(cg.out_node_id) used_nodes.add(cg.in_node_id) if cg.out_node_id not in node_inputs: node_inputs[cg.out_node_id] = [(cg.in_node_id, cg.weight)] else: node_inputs[cg.out_node_id].append((cg.in_node_id, cg.weight)) node_evals = [] for onode, inputs in node_inputs.items(): ng = genome.node_genes[onode] activation_function = activation_functions.get(ng.activation_type) node_evals.append( (onode, activation_function, ng.bias, ng.response, inputs)) return RecurrentNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
def create_feed_forward_phenotype(genome): """ Receives a genome and returns its phenotype (a neural network). """ # Gather inputs and expressed connections. input_nodes = [ ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT' ] output_nodes = [ ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT' ] connections = [(cg.in_node_id, cg.out_node_id) for cg in genome.conn_genes.values() if cg.enabled] layers = find_feed_forward_layers(input_nodes, connections) node_evals = [] used_nodes = set(input_nodes + output_nodes) for layer in layers: for node in layer: inputs = [] # TODO: This could be more efficient. for cg in genome.conn_genes.values(): if cg.out_node_id == node and cg.enabled: inputs.append((cg.in_node_id, cg.weight)) used_nodes.add(cg.in_node_id) used_nodes.add(node) ng = genome.node_genes[node] activation_function = activation_functions.get(ng.activation_type) node_evals.append( (node, activation_function, ng.bias, ng.response, inputs)) return FeedForwardNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
def create_feed_forward_phenotype(genome): """ Receives a genome and returns its phenotype (a neural network). """ # Gather inputs and expressed connections. input_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT'] output_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT'] connections = [(cg.in_node_id, cg.out_node_id) for cg in genome.conn_genes.values() if cg.enabled] layers = find_feed_forward_layers(input_nodes, connections) node_evals = [] used_nodes = set(input_nodes + output_nodes) for layer in layers: for node in layer: inputs = [] # TODO: This could be more efficient. for cg in genome.conn_genes.values(): if cg.out_node_id == node and cg.enabled: inputs.append((cg.in_node_id, cg.weight)) used_nodes.add(cg.in_node_id) used_nodes.add(node) ng = genome.node_genes[node] activation_function = activation_functions.get(ng.activation_type) node_evals.append((node, activation_function, ng.bias, ng.response, inputs)) return FeedForwardNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
def create_recurrent_phenotype(genome): """ Receives a genome and returns its phenotype (a recurrent neural network). """ # Gather inputs and expressed connections. input_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'INPUT'] output_nodes = [ng.ID for ng in genome.node_genes.values() if ng.type == 'OUTPUT'] node_inputs = {} used_nodes = set(input_nodes + output_nodes) for cg in genome.conn_genes.values(): if not cg.enabled: continue used_nodes.add(cg.out_node_id) used_nodes.add(cg.in_node_id) if cg.out_node_id not in node_inputs: node_inputs[cg.out_node_id] = [(cg.in_node_id, cg.weight)] else: node_inputs[cg.out_node_id].append((cg.in_node_id, cg.weight)) node_evals = [] for onode, inputs in node_inputs.items(): ng = genome.node_genes[onode] activation_function = activation_functions.get(ng.activation_type) node_evals.append((onode, activation_function, ng.bias, ng.response, inputs)) return RecurrentNetwork(max(used_nodes), input_nodes, output_nodes, node_evals)
def __init__(self, genome, config): """ FeedForwardPhenome - A feedforward network Adapted from: https://github.com/CodeReclaimers/neat-python, accessed May 2016 :param genome: the genome to create the phenome """ self.graph, node_lists = self._construct_graph(genome) self.input_nodes, self.hidden_nodes, self.output_nodes = node_lists self.links = [(g.src, g.sink) for g in genome.link_genes] self.node_evals = [] self.config = config layers = find_feed_forward_layers(self.input_nodes, self.links) used_nodes = set(self.input_nodes + self.output_nodes) for layer in layers: for node in layer: inputs = [] # TODO: This could be more efficient. for cg in genome.link_genes: if cg.sink == node and cg.enabled: inputs.append((cg.src, cg.weight)) used_nodes.add(cg.src) used_nodes.add(node) ng = genome.get_node_by_index(node) activation_function = activation_functions.get(ng.activation) self.node_evals.append((node, activation_function, inputs)) self.values = [0.0] * (1 + max(used_nodes))
def __init__(self, neuron_type, ID, bias, response, activation_type): assert neuron_type in ('INPUT', 'OUTPUT', 'HIDDEN') self.type = neuron_type self.ID = self.indexer.next(ID) self.bias = bias self.response = response self.activation = activation_functions.get(activation_type) self._synapses = [] self.output = 0.0 # for recurrent networks all neurons must have an "initial state"