def build_association(self):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        # Add a nengo.Node which calls out to a GPU library for
        # simulating the associative memory
        self.assoc_memory = \
            AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                 self.stored_vectors,
                                 threshold=self.threshold,
                                 neurons_per_item=self.neurons_per_item,
                                 tau_ref=tau_ref, tau_rc=tau_rc,
                                 eval_points=eval_points,
                                 intercepts=intercepts,
                                 radius=radius, do_print=False,
                                 identical=self.identical,
                                 probe_keys=self.probe_keys,
                                 seed=self.seed,
                                 num_steps=self.timesteps)

        self.assoc_output = np.zeros((1, self.dimension))
    def build_association(self):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        # Add a nengo.Node which calls out to a GPU library for
        # simulating the associative memory
        self.assoc_memory = \
            AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                 self.stored_vectors,
                                 threshold=self.threshold,
                                 neurons_per_item=self.neurons_per_item,
                                 tau_ref=tau_ref, tau_rc=tau_rc,
                                 eval_points=eval_points,
                                 intercepts=intercepts,
                                 radius=radius, do_print=False,
                                 identical=self.identical,
                                 probe_keys=self.probe_keys,
                                 seed=self.seed,
                                 num_steps=self.timesteps)

        self.assoc_output = np.zeros((1, self.dimension))
class FastNeuralExtractor(NeuralExtractor):

    _type = "Neural"

    def __init__(self, *args, **kwargs):
        """
        A GPU neural associative memory with a unique simulation strategy.
        Instead of making one big model with all the necessary components in
        it, we make multiple models, and simulate each completely before
        simulating the next one. The main benefit of this is that the GPU won't
        have to pause every time step to load data on and off. This is made
        possible by the fact that the network is completely feedforward, and so
        earlier components can be simulated without requiring input from later
        components.

        index_vectors and stored_vectors are both dictionaries mapping from
        tuples of the form (POS, number), indicating a synset, to numpy
        ndarrays containing the assigned vector
        """
        super(FastNeuralExtractor, self).__init__(*args, **kwargs)

    def setup_simulator(self):
        self.unbind_model = nengo.Network(label="Unbind", seed=self.seed)
        self.build_unbind(self.unbind_model)

        self.build_association()

        self.output_model = nengo.Network(label="Output", seed=self.seed)
        self.build_output(self.output_model)

        print "Building simulators"
        self.unbind_simulator = self.build_simulator(self.unbind_model)
        self.output_simulator = self.build_simulator(self.output_model)

        self.simulator = self.output_simulator

    def build_association(self):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        # Add a nengo.Node which calls out to a GPU library for
        # simulating the associative memory
        self.assoc_memory = \
            AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                 self.stored_vectors,
                                 threshold=self.threshold,
                                 neurons_per_item=self.neurons_per_item,
                                 tau_ref=tau_ref, tau_rc=tau_rc,
                                 eval_points=eval_points,
                                 intercepts=intercepts,
                                 radius=radius, do_print=False,
                                 identical=self.identical,
                                 probe_keys=self.probe_keys,
                                 seed=self.seed,
                                 num_steps=self.timesteps)

        self.assoc_output = np.zeros((1, self.dimension))

    def build_output(self, model):

        assoc_probes = OrderedDict()
        threshold_probes = OrderedDict()
        assoc_spike_probes = OrderedDict()

        synapse = self.assoc_params.synapse

        with model:
            input = nengo.Node(output=self.assoc_output_func)
            output = EnsembleArray(n_neurons=self.neurons_per_dim,
                                   n_ensembles=self.dimension,
                                   label="output",
                                   radius=self.radius)

            nengo.Connection(input, output.input, synapse=synapse)
            self.output_probe = nengo.Probe(output.output,
                                            'output',
                                            synapse=0.02)

            for k in self.probe_keys:
                n = nengo.Node(output=self.assoc_memory.probe_func(k))
                probe = nengo.Probe(n, synapse=synapse)

                threshold_probes[k] = probe

                node = nengo.Node(output=self.assoc_memory.spike_func(k))
                assoc_spike_probes[k] = nengo.Probe(node, synapse=None)

        self.assoc_probes = assoc_probes
        self.threshold_probes = threshold_probes
        self.assoc_spike_probes = assoc_spike_probes

    def extract(self, item, query, *args, **kwargs):
        self.print_instance_difficulty(item, query)

        then = datetime.datetime.now()

        self.A_input_vector = item
        self.B_input_vector = query

        print "Simulating unbind model"
        self.unbind_simulator.run(self.timesteps * self.dt)

        assoc_input = self.unbind_simulator.data[self.D_probe]

        print "Simulating associative memory"
        self.assoc_output = self.assoc_memory.multi_step(assoc_input)

        self.output_index = 0

        print "Simulating output"
        self.output_simulator.run(self.timesteps * self.dt)

        now = datetime.datetime.now()
        self.write_to_runtime_file(now - then, "unbind")

        # for plotting
        self.data = {k: v for (k, v) in self.output_simulator.data.iteritems()}
        self.data[self.D_probe] = self.unbind_simulator.data[self.D_probe]
        self.data[self.input_probe] = \
            self.unbind_simulator.data[self.input_probe]

        if self.plot:
            self.plot_simulation()

        vector = self.data[self.output_probe][-1, :]
        return [vector]

    def assoc_output_func(self, t):
        index = int(t / self.dt)
        return self.assoc_output[index]

    def reset_nodes(self):
        pass

    def write_to_runtime_file(self, delta, label=''):
        to_print = [
            self.dimension, self.num_items, self.neurons_per_item,
            self.neurons_per_dim, self.timesteps, "OCL: " + str(self.ocl),
            "GPUS: " + str(self.gpus), "fast", delta
        ]
        print >> self.runtimes_file, label, \
            ": " ",".join([str(tp) for tp in to_print])

    def print_config(self, output_file):
        super(FastNeuralExtractor, self).print_config(output_file)
        output_file.write("Fast neural extractor config:\n")
Beispiel #4
0
    def build_association(self, model):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        synapse = self.assoc_params.synapse
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        neurons_per_item = self.neurons_per_item
        threshold = self.threshold

        assoc_probes = OrderedDict()
        threshold_probes = OrderedDict()
        assoc_spike_probes = OrderedDict()

        with model:
            if self.gpus:

                if not self.identical:
                    raise NotImplementedError(
                        "Currently, can only use gpu if --identical "
                        "is also specified")

                # Add a nengo.Node which calls out to a GPU library for
                # simulating the associative memory
                self.assoc_mem = \
                    AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                         self.stored_vectors,
                                         threshold=threshold,
                                         neurons_per_item=neurons_per_item,
                                         tau_ref=tau_ref, tau_rc=tau_rc,
                                         eval_points=eval_points,
                                         intercepts=intercepts,
                                         radius=radius, do_print=False,
                                         identical=self.identical,
                                         probe_keys=self.probe_keys,
                                         seed=self.seed,
                                         collect_spikes=self.collect_spikes)

                def gpu_function(t, input_vector):
                    output_vector = self.assoc_mem.step(input_vector)
                    return output_vector

                assoc = nengo.Node(output=gpu_function,
                                   size_in=self.dimension,
                                   size_out=self.dimension)

                nengo.Connection(self.D_output, assoc, synapse=synapse)
                nengo.Connection(assoc, self.output.input, synapse=synapse)

                for k in self.probe_keys:
                    node = nengo.Node(output=self.assoc_mem.probe_func(k))
                    probe = nengo.Probe(node, synapse=synapse)

                    threshold_probes[k] = probe

                    node = nengo.Node(output=self.assoc_mem.spike_func(k))
                    assoc_spike_probes[k] = nengo.Probe(node, synapse=None)

            else:
                self.assoc_mem = AssociativeMemory(
                    input_vocab=np.array(self.index_vectors.values()),
                    output_vocab=np.array(self.stored_vectors.values()),
                    threshold=self.threshold,
                    neuron_type=nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref),
                    n_neurons_per_ensemble=neurons_per_item)

                nengo.Connection(self.D_output,
                                 self.assoc_mem.input,
                                 synapse=synapse)
                nengo.Connection(self.assoc_mem.output,
                                 self.output.input,
                                 synapse=synapse)

                assoc_ensembles = (
                    self.assoc_mem.thresholded_ens_array.ea_ensembles)

                for ens, k in zip(assoc_ensembles, self.index_vectors):
                    if k in self.probe_keys:
                        assoc_probes[k] = nengo.Probe(ens,
                                                      'decoded_output',
                                                      synapse=synapse)
                        assoc_spike_probes[k] = nengo.Probe(ens.neurons,
                                                            'spikes',
                                                            synapse=None)

        self.assoc_probes = assoc_probes
        self.threshold_probes = threshold_probes
        self.assoc_spike_probes = assoc_spike_probes
Beispiel #5
0
    def build_association(self, model):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        synapse = self.assoc_params.synapse
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        neurons_per_item = self.neurons_per_item
        threshold = self.threshold

        assoc_probes = OrderedDict()
        threshold_probes = OrderedDict()
        assoc_spike_probes = OrderedDict()

        with model:
            if self.gpus:

                if not self.identical:
                    raise NotImplementedError(
                        "Currently, can only use gpu if --identical "
                        "is also specified")

                # Add a nengo.Node which calls out to a GPU library for
                # simulating the associative memory
                self.assoc_mem = \
                    AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                         self.stored_vectors,
                                         threshold=threshold,
                                         neurons_per_item=neurons_per_item,
                                         tau_ref=tau_ref, tau_rc=tau_rc,
                                         eval_points=eval_points,
                                         intercepts=intercepts,
                                         radius=radius, do_print=False,
                                         identical=self.identical,
                                         probe_keys=self.probe_keys,
                                         seed=self.seed,
                                         collect_spikes=self.collect_spikes)

                def gpu_function(t, input_vector):
                    output_vector = self.assoc_mem.step(input_vector)
                    return output_vector

                assoc = nengo.Node(output=gpu_function,
                                   size_in=self.dimension,
                                   size_out=self.dimension)

                nengo.Connection(self.D_output, assoc, synapse=synapse)
                nengo.Connection(assoc, self.output.input, synapse=synapse)

                for k in self.probe_keys:
                    node = nengo.Node(output=self.assoc_mem.probe_func(k))
                    probe = nengo.Probe(node, synapse=synapse)

                    threshold_probes[k] = probe

                    node = nengo.Node(output=self.assoc_mem.spike_func(k))
                    assoc_spike_probes[k] = nengo.Probe(node, synapse=None)

            else:
                #print(self.index_vectors)
                #print(self.stored_vectors)
                '''
                index_vocab = Vocabulary(128)
                stored_vocab = Vocabulary(128)
                #index_vocab = Vocabulary(len(self.index_vectors.values()))
                #stored_vocab = Vocabulary(len(self.stored_vectors.values()))

                import string
                chars = list(string.ascii_uppercase)

                import random
                for idx, vector in enumerate(self.index_vectors.values()):
                    generated_key = ''.join([chars[i] for i in random.sample(range(len(chars)), len(chars)-1)])
                    print('index_len: {}'.format(np.shape(vector)))
                    print(vector)
                    index_vocab.add(generated_key, vector)
                for idx, vector in enumerate(self.stored_vectors.values()):
                    generated_key = ''.join([chars[i] for i in random.sample(range(len(chars)), len(chars)-1)])
                    print('stored_len: {}'.format(np.shape(vector)))
                    stored_vocab.add(generated_key, vector)
                '''

                self.assoc_mem = AssociativeMemory(
                    input_vectors=self.index_vectors.values(),
                    output_vectors=self.stored_vectors.values(),
                    threshold=self.threshold,
                    n_neurons=neurons_per_item)
                #neuron_type=nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref),
                #n_neurons_per_ensemble=neurons_per_item)
                self.assoc_mem.add_threshold_to_outputs(neurons_per_item)
                #self.assoc_mem = AssociativeMemory(
                #    input_vocab=index_vocab,
                #    output_vocab=stored_vocab,
                #    threshold=self.threshold,
                #    )
                #neuron_type=nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref),
                #n_neurons_per_ensemble=neurons_per_item)

                nengo.Connection(self.D_output,
                                 self.assoc_mem.input,
                                 synapse=synapse)
                nengo.Connection(self.assoc_mem.output,
                                 self.output.input,
                                 synapse=synapse)

                assoc_ensembles = (self.assoc_mem.thresh_ens.ensembles)
                #self.assoc_mem.thresholded_ens_array.ea_ensembles)

                for ens, k in zip(assoc_ensembles, self.index_vectors):
                    if k in self.probe_keys:
                        assoc_probes[k] = nengo.Probe(ens,
                                                      'decoded_output',
                                                      synapse=synapse)
                        assoc_spike_probes[k] = nengo.Probe(ens.neurons,
                                                            'spikes',
                                                            synapse=None)

        self.assoc_probes = assoc_probes
        self.threshold_probes = threshold_probes
        self.assoc_spike_probes = assoc_spike_probes
class FastNeuralExtractor(NeuralExtractor):

    _type = "Neural"

    def __init__(self, *args, **kwargs):
        """
        A GPU neural associative memory with a unique simulation strategy.
        Instead of making one big model with all the necessary components in
        it, we make multiple models, and simulate each completely before
        simulating the next one. The main benefit of this is that the GPU won't
        have to pause every time step to load data on and off. This is made
        possible by the fact that the network is completely feedforward, and so
        earlier components can be simulated without requiring input from later
        components.

        index_vectors and stored_vectors are both dictionaries mapping from
        tuples of the form (POS, number), indicating a synset, to numpy
        ndarrays containing the assigned vector
        """
        super(FastNeuralExtractor, self).__init__(*args, **kwargs)

    def setup_simulator(self):
        self.unbind_model = nengo.Network(label="Unbind", seed=self.seed)
        self.build_unbind(self.unbind_model)

        self.build_association()

        self.output_model = nengo.Network(label="Output", seed=self.seed)
        self.build_output(self.output_model)

        print "Building simulators"
        self.unbind_simulator = self.build_simulator(self.unbind_model)
        self.output_simulator = self.build_simulator(self.output_model)

        self.simulator = self.output_simulator

    def build_association(self):

        tau_rc = self.assoc_params.tau_rc
        tau_ref = self.assoc_params.tau_ref
        radius = self.assoc_params.radius
        eval_points = self.assoc_params.eval_points
        intercepts = self.assoc_params.intercepts

        # Add a nengo.Node which calls out to a GPU library for
        # simulating the associative memory
        self.assoc_memory = \
            AssociativeMemoryGPU(self.gpus, self.index_vectors,
                                 self.stored_vectors,
                                 threshold=self.threshold,
                                 neurons_per_item=self.neurons_per_item,
                                 tau_ref=tau_ref, tau_rc=tau_rc,
                                 eval_points=eval_points,
                                 intercepts=intercepts,
                                 radius=radius, do_print=False,
                                 identical=self.identical,
                                 probe_keys=self.probe_keys,
                                 seed=self.seed,
                                 num_steps=self.timesteps)

        self.assoc_output = np.zeros((1, self.dimension))

    def build_output(self, model):

        assoc_probes = OrderedDict()
        threshold_probes = OrderedDict()
        assoc_spike_probes = OrderedDict()

        synapse = self.assoc_params.synapse

        with model:
            input = nengo.Node(output=self.assoc_output_func)
            output = EnsembleArray(n_neurons=self.neurons_per_dim,
                                   n_ensembles=self.dimension,
                                   label="output",
                                   radius=self.radius)

            nengo.Connection(input, output.input, synapse=synapse)
            self.output_probe = nengo.Probe(output.output, 'output',
                                            synapse=0.02)

            for k in self.probe_keys:
                n = nengo.Node(output=self.assoc_memory.probe_func(k))
                probe = nengo.Probe(n, synapse=synapse)

                threshold_probes[k] = probe

                node = nengo.Node(output=self.assoc_memory.spike_func(k))
                assoc_spike_probes[k] = nengo.Probe(node, synapse=None)

        self.assoc_probes = assoc_probes
        self.threshold_probes = threshold_probes
        self.assoc_spike_probes = assoc_spike_probes

    def extract(self, item, query, *args, **kwargs):
        self.print_instance_difficulty(item, query)

        then = datetime.datetime.now()

        self.A_input_vector = item
        self.B_input_vector = query

        print "Simulating unbind model"
        self.unbind_simulator.run(self.timesteps * self.dt)

        assoc_input = self.unbind_simulator.data[self.D_probe]

        print "Simulating associative memory"
        self.assoc_output = self.assoc_memory.multi_step(assoc_input)

        self.output_index = 0

        print "Simulating output"
        self.output_simulator.run(self.timesteps * self.dt)

        now = datetime.datetime.now()
        self.write_to_runtime_file(now - then, "unbind")

        # for plotting
        self.data = {k: v for (k, v) in self.output_simulator.data.iteritems()}
        self.data[self.D_probe] = self.unbind_simulator.data[self.D_probe]
        self.data[self.input_probe] = \
            self.unbind_simulator.data[self.input_probe]

        if self.plot:
            self.plot_simulation()

        vector = self.data[self.output_probe][-1, :]
        return [vector]

    def assoc_output_func(self, t):
        index = int(t / self.dt)
        return self.assoc_output[index]

    def reset_nodes(self):
        pass

    def write_to_runtime_file(self, delta, label=''):
        to_print = [self.dimension, self.num_items,
                    self.neurons_per_item, self.neurons_per_dim,
                    self.timesteps, "OCL: "+str(self.ocl),
                    "GPUS: "+str(self.gpus), "fast", delta]
        print >> self.runtimes_file, label, \
            ": " ",".join([str(tp) for tp in to_print])

    def print_config(self, output_file):
        super(FastNeuralExtractor, self).print_config(output_file)
        output_file.write("Fast neural extractor config:\n")