def deserialize_json(self, json_str): j = json.loads(json_str) j_neurons = j["neurons"] j_synapses = j["synapses"] self._effective_deepness = j["effective_deepness"] self._response_time = j["response_time"] self._resolving_ability = j["resolving_ability"] self._quality = j["quality"] self._adaptability = j["adaptability"] self._extra_data = j["extra_data"] n_id_to_ind = {} self._neurons = [] for jn in j_neurons: ind = len(self._neurons) n_id_to_ind[jn["id"]] = ind self._neurons.append( nn.Neuron([0] * jn["axon_len"], jn["bias"], jn["transfer_function_type"], jn["transfer_function_params"])) self._synapses = [ nn.Synapse(n_id_to_ind[js["src"]], n_id_to_ind[js["own"]], js["weight"]) for js in j_synapses ] self._input_neurons_inds = [ n_id_to_ind[id] for id in j["input_neurons"] ] self._output_neurons_inds = [ n_id_to_ind[id] for id in j["output_neurons"] ] nn.Estimator.fill_all(self)
def _load_neuron_bodies(self, nnid): neurons_data = Engine.db().load_neurons_data(nnid) neurons = [(id, nn.Neuron([0] * axon_len, bias, tf_type, json.loads(tf_params))) for id, tf_type, tf_params, bias, axon_len in neurons_data] self.__data._neurons = [n[1] for n in neurons] self._map_neuron_id2ind = {n[0]: ind for ind, n in enumerate(neurons)}
def __add_receptor(self, input_data_name): new_neuron_ind = len(self.data.neurons) new_neuron_tf = Type.relu new_neuron_tf_params = () new_neuron = nn.Neuron([0], 0, new_neuron_tf, new_neuron_tf_params) self._temp_data._map_input_sid2ind[input_data_name] = new_neuron_ind self.data.neurons.append(new_neuron)
def __add_indicator(self, output_data_name): new_neuron_ind = len(self.data.neurons) new_neuron_tf = Type.linear new_neuron_tf_params = () new_neuron = nn.Neuron([0, 0], 0, new_neuron_tf, new_neuron_tf_params) # В настоящее время при создании нейрона-индикатора он присоединяется напрямую к рецептору # (у которого глубина по-определению равна нулю), # поэтому в данном случае глубина индикатора будет 1 new_neuron.deepness = 1 self._temp_data._map_output_sid2ind[output_data_name] = new_neuron_ind self.data.neurons.append(new_neuron)
def _do_mutate(cls, data : nn.Data): d = data r = random.Random() c = Engine.config() receptors_list = [ d.neurons[i] for i in d.input_neurons_inds ] indicators_list = [ d.neurons[i] for i in d.output_neurons_inds ] neurons = set(d.neurons) receptors = set(receptors_list) indicators = set(indicators_list) workers = { n for n in d.neurons if n not in receptors | indicators } synapses = { nn.Synapse(d.neurons[s.src], d.neurons[s.own], s.weight) for s in d.synapses } connections = { (s.src, s.own) for s in synapses } neuron_to_synapses = collections.defaultdict(set) for s in synapses: neuron_to_synapses[s.src].add(s) neuron_to_synapses[s.own].add(s) # удаление нейронов и синапсов neurons_to_del = { n for n in workers if r.uniform(0, 1) < c.mutator_neuron_deleting_probability_factor } synapses_to_del = { s for s in synapses if r.uniform(0, 1) < c.mutator_synapse_deleting_probability_factor } for n in neurons_to_del: synapses_to_del |= neuron_to_synapses[n] neurons -= neurons_to_del synapses -= synapses_to_del del workers, neurons_to_del, synapses_to_del del neuron_to_synapses # добавление нейронов и синапсов neurons_to_add = set() synapses_to_add = set() for n1, n3 in itertools.product(list(neurons), list(neurons - receptors)): if r.uniform(0, 1) < c.mutator_synapse_adding_probability_factor: if (n1, n3) not in connections: synapses_to_add.add(nn.Synapse(n1, n3, 1)) connections.add((n1, n3)) if r.uniform(0, 1) < c.mutator_neuron_adding_probability_factor: n2 = nn.Neuron([0, 0], 0, nn.Type.relu, ()) neurons_to_add.add(n2) synapses_to_add.add(nn.Synapse(n1, n2, 1)) synapses_to_add.add(nn.Synapse(n2, n3, 1)) neurons |= neurons_to_add synapses |= synapses_to_add del neurons_to_add, synapses_to_add del n1, n3 neurons_list = list(neurons) synapses_list = list(synapses) neuron_to_ind = { n: i for i, n in enumerate(neurons_list) } for s in synapses_list: s.src = neuron_to_ind[s.src] s.own = neuron_to_ind[s.own] d._input_neurons_inds = [ neuron_to_ind[n] for n in receptors_list ] d._output_neurons_inds = [ neuron_to_ind[n] for n in indicators_list ] d._neurons = neurons_list d._synapses = synapses_list nn.Estimator.fill_all(d)