def read_network(fn): with open(fn, 'r') as f: s = f.read() j = json.loads(s) net = neuro.Network() net.from_json(j) return net
def setup_network(self): self.net = neuro.Network() self.net.set_properties(self.proc.get_properties()) self.start_outputs = 0 neurons = [] neurons.append([]) #print("Here", self.threshold) # Create neurons total = 0 for i in range(1,len(self.neurons_per_layer)): neurons.append([]) for j in range(self.neurons_per_layer[i]): node = self.net.add_node(j+total) neurons[i].append(j+total) self.net.add_input(j+total) self.net.add_output(j+total) #print("Bad here", self.threshold) node.set("Threshold", self.threshold) total += self.neurons_per_layer[i] if (i != len(self.neurons_per_layer)-1): self.start_outputs += self.neurons_per_layer[i] self.total_neurons = total # Create synapses total = 0 for i in range(1,len(self.neurons_per_layer)-1): for j in range(self.neurons_per_layer[i]): #print(self.weights[i-1]) for k in range(self.neurons_per_layer[i+1]): w = self.weights[i-1][j][k] if (w != 0): edge = self.net.add_edge(neurons[i][j], neurons[i+1][k]) #edge = self.net.add_edge(j+total, k+total+self.neurons_per_layer[i]) edge.set("Weight", w) edge.set("Delay", 1) total+= self.neurons_per_layer[i] # Create bias neurons/synapses total = 0 intermediate_total = self.neurons_per_layer[1] bias_id = self.total_neurons self.bias_neuron = self.net.add_node(bias_id) self.net.add_input(bias_id) self.bias_neuron.set("Threshold", max(self.min_threshold, 0)) for i in range(2, len(self.neurons_per_layer)): #print(self.biases[i-2]) for j in range(self.neurons_per_layer[i]): edge = self.net.add_edge(bias_id, neurons[i][j]) edge.set("Weight", self.biases[i-2][j]) edge.set("Delay", i-1) total += self.neurons_per_layer[i] intermediate_total += self.neurons_per_layer[i]
def read_network(self, fn=None): if (fn == None): fn = self.network_filename with open(fn, 'r') as f: s = f.read() j = json.loads(s) net = neuro.Network() net.from_json(j) return net
def make_feed_forward(props, layer_size, seed=0): moa = neuro.MOA() moa.seed(seed) net = neuro.Network() net.set_properties(props) if (len(layer_size) < 2): raise ValueError( "make_feed_forward requires layers to have at least an input layer and an output layer" ) num_inputs = layer_size[0] num_outputs = layer_size[-1] layers = [] layers.append([]) for i in range(num_inputs): node = net.add_node(i) net.randomize_node_properties(moa, node) net.add_input(i) layers[0].append(i) current_index = num_inputs + num_outputs for i in range(1, len(layer_size) - 1): layers.append([]) for j in range(layer_size[i]): node = net.add_node(current_index) net.randomize_node_properties(moa, node) layers[-1].append(current_index) current_index += 1 layers.append([]) for i in range(num_outputs): node = net.add_node(num_inputs + i) net.randomize_node_properties(moa, node) net.add_output(num_inputs + i) layers[-1].append(num_inputs + i) for i in range(len(layers) - 1): for j in range(len(layers[i])): for k in range(len(layers[i + 1])): edge = net.add_edge(layers[i][j], layers[i + 1][k]) net.randomize_edge_properties(moa, edge) return net
def generate_template_network(properties, n_inputs, n_outputs, seed=0): net = neuro.Network() net.set_properties(properties) moa = neuro.MOA() moa.seed(seed) for i in range(n_inputs): node = net.add_node(i) net.add_input(i) net.randomize_node_properties(moa, node) for i in range(n_outputs): node = net.add_node(i + n_inputs) net.add_output(i + n_inputs) net.randomize_node_properties(moa, node) return net
def initialize_network(self, proc, seed, num_hidden, prob_edge): properties = proc.get_properties() net = neuro.Network() net.set_properties(properties) moa = neuro.MOA() moa.seed(seed) for i in range(self.n_inputs): node = net.add_node(i) net.add_input(i) net.randomize_node_properties(moa, node) for i in range(self.n_outputs): node = net.add_node(i + self.n_inputs) net.add_output(i + self.n_inputs) net.randomize_node_properties(moa, node) total = self.n_inputs + self.n_outputs for i in range(num_hidden): node = net.add_node(i + total) net.randomize_node_properties(moa, node) total += num_hidden A = scipy.sparse.random(total, total, density=prob_edge, random_state=seed).toarray() for i in range(total): for j in range(total): if (A[i][j] != 0 and i != j): edge = net.add_edge(i, j) net.randomize_edge_properties(moa, edge) return net