def populate(self, hidden_neurons): """ Fills the cortex with neurons and establishes connections Cortexes have a layer of hidden neurons, and a layer of output neurons to start with, but everything is set up such that more complex interconnections may evolve with future work. """ HiddenLayer = [] OutLayer = [] for i in range(hidden_neurons): HiddenLayer.append("n_{0}".format(i)) for i in range(self.size_output): OutLayer.append("n_{0}".format(i + hidden_neurons)) self.hidden_neurons = [ neuron(name) for name in HiddenLayer] self.output_neurons = [ neuron(name) for name in OutLayer] for neur in self.output_neurons: for child in self.hidden_neurons: neur.add_child(child) child.add_parent(neur) self.neurons = self.hidden_neurons + self.output_neurons self.size = len(self.neurons) self.size_hidden = len(self.hidden_neurons) self.size_output = len(self.output_neurons) return
def import_state(self,filepath): """ Imports an already trained cortex thats been saved before this function demonstrates my lack of understanding of python regular expressions.....i think Check out the "export_state" funtction for insight on how this should be formatted. """ print("="*50) print("Importing cortex state from '{0}'".format(filepath)) print("="*50 + '\n') self.imported_from = filepath self.neurons = [] self.output_neurons = [] self.hidden_neurons = [] with open(filepath,'r+') as f: # grabs cortex level information from the top first cortex_header = next(f).replace('\n','').replace(' ','').split(';') cortex_line = next(f).replace('\n','').replace(' ','') cortex_line = cortex_line.replace("[","").replace("]","") cortex_info = cortex_line.split(';') for i,field in enumerate(cortex_info): if ',' in field: cortex_info[i] = cortex_info[i].replace("'",'').split(',') for i,attribute in enumerate(cortex_header): setattr(self, attribute, cortex_info[i]) print("imported attribute '{0}' as {1}".format(attribute,cortex_info[i])) # correct formats that shouldn't be strings self.size = int(self.size) self.size_hidden = int(self.size_hidden) self.size_output = int(self.size_output) self.accuracy = float(self.accuracy) self.min_acc = float(self.min_acc) self.col_accuracy = map(float,self.col_accuracy) self.max_err = float(self.max_err) self.crit_instability = float(self.crit_instability) # moves on to grab neuron information neuron_header = next(f).replace('\n','').replace(' ','').split(';') for line in f: line = line.replace('\n','').replace(' ','').replace("[","").replace("]","") info = line.split(';') # handles neuron info one row at a time for i,field in enumerate(info): if ',' in field: info[i] = info[i].split(',') if 'True' in field: info[i] = True elif 'False' in field: info[i] = False neur = neuron(info[0]) for i,attribute in enumerate(neuron_header): setattr(neur,attribute,info[i]) neur.weights = map(float,neur.weights) neur.bias = float(neur.bias) neur.f_age = int(neur.f_age) neur.r_age = int(neur.r_age) neur.size = int(neur.size) neur.dec = int(neur.dec) neur.delta = float(neur.delta) neur.del_delta = float(neur.del_delta) neur.hasparents = bool(neur.hasparents) neur.haschildren= bool(neur.haschildren) self.neurons.append(neur) print("imported member neuron with name '{0}'".format(neur.name)) for neur in self.neurons: # presently, each neurons children and parents lists # are just strings. turn these into object lists. if neur.hasparents: if not isinstance(neur.parents,list):#handles single parent lists neur.parents = [neur.parents] for i,parent in enumerate(neur.parents): neur.parents[i] = self.find_object(parent) self.hidden_neurons.append(neur) else: neur.parents = [] if neur.haschildren: if not isinstance(neur.children,list):# handles single child lists neur.children = [neur.children] for i,child in enumerate(neur.children): neur.children[i] = self.find_object(child) if not neur.hasparents: self.output_neurons.append(neur) else: neur.children = [] f.close() print("="*50 + '\n') return