Beispiel #1
0
 def tick_layers_input_data(self, domain):
     """
     Add value from layer.input_data to neurons.level
     """
     ticks = domain.ticks
     for layer in domain.layers:
         if layer.input_data is None and layer.input_data_cache is None:
             continue
         data = layer.input_data
         layer.input_data = None
         input_data_vector = layer.input_data_cache
         if input_data_vector is None:
             input_data_vector = StandaloneVector()
             if isinstance(data, basestring):
                 input_data_vector.from_bytes(data)
             else:
                 input_data_vector.set_data(data)
             assert len(input_data_vector) == layer.length, \
                 "Domain '%s': len(input_data_vector)=%s, layer.length=%s" \
                 % (domain.name, len(input_data_vector), layer.length)
             layer.input_data_cache = input_data_vector
         length = len(input_data_vector)
         if not length:
             return
         input_data_vector.to_device(self)
         # only one layer with the same dims as input data
         assert length == len(layer.neurons_metadata.level)
         self.program.tick_numpy_input_data_uint8(
             self.queue, (length,), None,
             # data
             input_data_vector.device_data_pointer,
             # layer
             types.address(layer.neurons_metadata.address),
             # neurons
             domain.neurons.level.device_data_pointer
         ).wait()
         if layer.input_expire <= ticks:
             layer.input_data = None
             layer.input_data_cache = None
Beispiel #2
0
 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])