예제 #1
0
파일: __init__.py 프로젝트: openre/openre
    def tick_layers_output_data(self, domain):
        """
        Convert layer ticks to numpy array
        (if layer.config.get('output') is True)
        """
        length = len(domain.output_index.address)
        if not length:
            return
        output_index = domain.output_index
        self.program.tick_numpy_output_data_uint8(
            self.queue, (length,), None,
            # domain
            types.tick(domain.ticks),
            # output index
            output_index.address.device_data_pointer,
            output_index.data.device_data_pointer,
            output_index.tick.device_data_pointer,
            # neurons
            domain.neurons.flags.device_data_pointer
        ).wait()
        # find all source consumers and cache it
        if self._source_cache is None:
            self._source_cache = {}
            cache = self._source_cache
            for layer in domain.layers:
                if 'output' not in layer.config:
                    continue
                source_id = layer.config['output']
                cache[source_id] = []
            net = domain.net
            # consumers
            for other_domain in net.domains:
                for layer_index, layer in enumerate(other_domain.layers):
                    if not layer.config.get('input'):
                        continue
                    source_id = layer.config['input']
                    # not our source
                    if source_id not in cache:
                        continue
                    cache[source_id].append([other_domain, layer_index])
                for input_index, input_row in enumerate(
                    other_domain.config['device'].get('input', [])
                ):
                    source_id = input_row['name']
                    # not our source
                    if source_id not in cache:
                        continue
                    cache[source_id].append([other_domain, input_index])

        domain.neurons.flags.from_device(self)
        output_index.data.from_device(self)
        cache = self._source_cache
        for source_id, data in output_index.data_to_send():
            for consumer_domain, layer_index in cache[source_id]:
                consumer_domain.register_input_layer_data(layer_index, data)
예제 #2
0
파일: __init__.py 프로젝트: openre/openre
 def tick_neurons(self, domain):
     length = domain.neurons.length
     if not length:
         return
     self.tick_layers_input_data(domain)
     self.program.tick_neurons(
         self.queue, (length,), None,
         # domain
         types.tick(domain.ticks),
         # layers
         domain.layers_vector.threshold.device_data_pointer,
         domain.layers_vector.relaxation.device_data_pointer,
         domain.layers_vector.spike_cost.device_data_pointer,
         domain.layers_vector.max_vitality.device_data_pointer,
         # neurons
         domain.neurons.level.device_data_pointer,
         domain.neurons.flags.device_data_pointer,
         domain.neurons.spike_tick.device_data_pointer,
         domain.neurons.layer.device_data_pointer,
         domain.neurons.vitality.device_data_pointer,
         domain.neurons.threshold.device_data_pointer
     ).wait()
     self.tick_layers_output_data(domain)
예제 #3
0
파일: __init__.py 프로젝트: openre/openre
 def tick_synapses(self, domain):
     length = domain.neurons.length
     if not length:
         return
     self.program.tick_synapses(
         self.queue, (length,), None,
         # domain
         types.synapse_level(domain.learn_rate),
         types.synapse_level(domain.learn_threshold),
         types.tick(domain.spike_learn_threshold),
         types.tick(domain.spike_forget_threshold),
         # neurons
         domain.neurons.level.device_data_pointer,
         domain.neurons.flags.device_data_pointer,
         domain.neurons.spike_tick.device_data_pointer,
         # synapses
         domain.synapses.level.device_data_pointer,
         domain.synapses.pre.device_data_pointer,
         domain.synapses.post.device_data_pointer,
         domain.synapses.learn.device_data_pointer,
         domain.synapses.flags.device_data_pointer,
         # pre-neuron - synapse index
         domain.pre_synapse_index.key.device_data_pointer,
         domain.pre_synapse_index.value.device_data_pointer,
         # post-neuron - synapse index
         domain.post_synapse_index.key.device_data_pointer,
         domain.post_synapse_index.value.device_data_pointer
     ).wait()
     # download layers stats from device once
     # per domain.config['stat_size'] ticks
     if  domain.ticks % domain.config['stat_size'] == 0:
         domain.stat_vector.data.fill(0)
         self.program.update_layers_stat(
             self.queue, (domain.neurons.length,), None,
             # domain
             types.tick(domain.ticks),
             types.address(domain.config['stat_size']),
             types.address(domain.stat_fields),
             # layers
             domain.layers_stat.device_data_pointer,
             domain.layers_vector.max_vitality.device_data_pointer,
             # neurons
             domain.neurons.flags.device_data_pointer,
             domain.neurons.spike_tick.device_data_pointer,
             domain.neurons.layer.device_data_pointer,
             domain.neurons.vitality.device_data_pointer
         ).wait()
         domain.layers_stat.from_device(self)
         stat_length = len(domain.stat_vector)
         for layer_address in range(len(domain.layers)):
             layer_stat_start = \
                     domain.stat_fields \
                     * layer_address
             domain.stat_vector.data += domain.layers_stat.data[
                 layer_stat_start : layer_stat_start + stat_length
             ]
         self.program.init_layers_stat(
             self.queue, (len(domain.layers_stat),), None,
             domain.layers_stat.device_data_pointer
         ).wait()
         if len(domain.synapses):
             # count synapses stats
             domain.stat_vector.to_device(self)
             self.program.update_synapses_stat(
                 self.queue, (len(domain.synapses),), None,
                 domain.stat_vector.device_data_pointer,
                 # synapses
                 domain.synapses.learn.device_data_pointer,
                 domain.synapses.flags.device_data_pointer
             ).wait()
             domain.stat_vector.from_device(self)
         domain.stat_set('stat_size', domain.config['stat_size'])
         # 0 - total spikes (one per neuron) per self.config['stat_size']
         # ticks
         domain.stat_set('total_spikes', domain.stat_vector.data[0])
         # 1 - number of the dead neurons
         domain.stat_set('dead_neurons', domain.stat_vector.data[1])
         # 2 - number of synapses with flag IS_STRENGTHENED
         domain.stat_set('strengthened_synapses', domain.stat_vector.data[2])
         # 3 - neurons tiredness = sum(layer.max_vitality - neuron.vitality)
         domain.stat_set('neurons_tiredness', domain.stat_vector.data[3])
         # 4 - synapse learn level
         domain.stat_set('synapse_learn_level', domain.stat_vector.data[4])