def get_stats(self, clean_result_vector, goal, other_answers, fp):
        size = np.linalg.norm(clean_result_vector)

        if not goal:
            comparisons = self.find_matches(clean_result_vector,
                                            self.semantic_pointers)

            largest_match = max(comparisons, key=lambda x: x[1])
            return (largest_match[0], largest_match[1], size)
        else:
            comparisons = self.find_matches(clean_result_vector,
                                            self.semantic_pointers,
                                            exempt=[goal])

            if other_answers:
                invalids = []
                valids = []
                for c in comparisons:
                    if c[0] in other_answers:
                        valids.append(c)
                    else:
                        invalids.append(c)

                max_invalid = max(invalids, key=lambda x: x[1])
                max_invalid_key, max_invalid_match = max_invalid

                if len(valids) == 0:
                    second_key, second_match = max_invalid
                else:
                    max_valid = max(valids, key=lambda x: x[1])
                    max_valid_key, max_valid_match = max_valid

                    if max_invalid_match > max_valid_match:
                        second_key, second_match = max_invalid
                    else:
                        second_key, second_match = max_valid

            else:
                second_key, second_match = max(comparisons, key=lambda x: x[1])
                max_invalid_match = second_match

            hrr_vec = HRR(data=self.semantic_pointers[goal])
            target_match = hrr_vec.compare(HRR(data=clean_result_vector))

            if target_match > second_match:
                clean_result = goal
            else:
                clean_result = second_key

            return (clean_result, target_match, second_match, size,
                    max_invalid_match)
Beispiel #2
0
    def get_stats(self, clean_result_vector, goal, other_answers, fp):
        size = np.linalg.norm(clean_result_vector)

        if not goal:
            comparisons = self.find_matches(clean_result_vector,
                                            self.semantic_pointers)

            largest_match = max(comparisons, key=lambda x: x[1])
            return (largest_match[0], largest_match[1], size)
        else:
            comparisons = self.find_matches(
                clean_result_vector, self.semantic_pointers, exempt=[goal])

            if other_answers:
                invalids = []
                valids = []
                for c in comparisons:
                    if c[0] in other_answers:
                        valids.append(c)
                    else:
                        invalids.append(c)

                max_invalid = max(invalids, key=lambda x: x[1])
                max_invalid_key, max_invalid_match = max_invalid

                if len(valids) == 0:
                    second_key, second_match = max_invalid
                else:
                    max_valid = max(valids, key=lambda x: x[1])
                    max_valid_key, max_valid_match = max_valid

                    if max_invalid_match > max_valid_match:
                        second_key, second_match = max_invalid
                    else:
                        second_key, second_match = max_valid

            else:
                second_key, second_match = max(comparisons, key=lambda x: x[1])
                max_invalid_match = second_match

            hrr_vec = HRR(data=self.semantic_pointers[goal])
            target_match = hrr_vec.compare(HRR(data=clean_result_vector))

            if target_match > second_match:
                clean_result = goal
            else:
                clean_result = second_key

            return (clean_result, target_match,
                    second_match, size, max_invalid_match)
Beispiel #3
0
    def plot_simulation(self, target_keys):
        then = datetime.datetime.now()

        correct_key = None
        if target_keys:
            correct_key = target_keys[0]

        sim = self.simulator
        t = sim.trange()

        max_val = 5.0 / np.sqrt(self.dimension)

        gs = gridspec.GridSpec(7, 2)
        fig = plt.figure(figsize=(10, 10))

        ax = plt.subplot(gs[0, 0])

        plt.plot(t, self.data[self.D_probe], label='D')
        title = 'Input to associative memory'
        ax.text(.01,
                1.20,
                title,
                horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-max_val, max_val))

        ax = plt.subplot(gs[0, 1])
        plt.plot(t, self.data[self.output_probe], label='Output')
        title = 'Output of associative memory'
        ax.text(.01,
                1.20,
                title,
                horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-max_val, max_val))

        ax = plt.subplot(gs[1:3, :])

        if len(self.index_vectors) < 1000:
            for key, v in self.index_vectors.iteritems():
                input_sims = np.dot(self.data[self.D_probe], v)
                label = str(key[1])
                if key == correct_key:
                    plt.plot(t, input_sims, '--', label=label + '*')
                else:
                    plt.plot(t, input_sims, label=label)

            title = (
                'Dot product between id vectors and input to assoc memory.\n'
                'Target %s is dashed line.' % str(correct_key))

            ax.text(.01,
                    0.80,
                    title,
                    horizontalalignment='left',
                    transform=ax.transAxes)
            # plt.legend(bbox_to_anchor=(-0.03, 0.5), loc='center right')
            if self.ideal_dot:
                ax.text(.01,
                        0.10,
                        "Ideal dot: " + str(self.ideal_dot),
                        horizontalalignment='left',
                        transform=ax.transAxes)
            if self.second_dot:
                ax.text(.99,
                        0.10,
                        "Second dot: " + str(self.second_dot),
                        horizontalalignment='right',
                        transform=ax.transAxes)

            plt.ylim((-1.0, 1.5))
            plt.axhline(1.0, ls=':', c='k')

        ax = plt.subplot(gs[3:5, :])
        for key, p in self.assoc_probes.iteritems():
            if key == correct_key:
                plt.plot(t, self.data[p], '--')
            else:
                plt.plot(t, self.data[p])

        title = ('Decoded values of association populations.\n' +
                 'Target %s is dashed line.' % str(correct_key))

        ax.text(.01,
                0.80,
                title,
                horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-0.2, 1.5))
        plt.axhline(y=1.0, ls=':', c='k')

        ax = plt.subplot(gs[5:7, :])
        before_ls = '--'
        after_ls = '-'
        before_norms = [np.linalg.norm(v) for v in self.data[self.D_probe]]
        after_norms = [np.linalg.norm(v) for v in self.data[self.output_probe]]

        plt.plot(t, before_norms, before_ls, c='g', label='Norm - Before')
        plt.plot(t, after_norms, after_ls, c='g', label='Norm - After')

        if correct_key is not None:
            correct_index_hrr = HRR(data=self.index_vectors[correct_key])
            correct_stored_hrr = HRR(data=self.stored_vectors[correct_key])

            before_sims = [
                correct_index_hrr.compare(HRR(data=i))
                for i in self.data[self.D_probe]
            ]

            after_sims = [
                correct_stored_hrr.compare(HRR(data=o))
                for o in self.data[self.output_probe]
            ]

            plt.plot(t,
                     before_sims,
                     before_ls,
                     c='b',
                     label='Cosine Sim - Before')
            plt.plot(t,
                     after_sims,
                     after_ls,
                     c='b',
                     label='Cosine Sim - After')

        title = 'Before and After Associative Memory'
        ax.text(.01,
                0.90,
                title,
                horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-1.0, 1.5))
        plt.legend(loc=4, prop={'size': 6})
        plt.axhline(y=1.0, ls=':', c='k')
        ax.set_xlabel('Time (s)')

        date_time_string = str(datetime.datetime.now()).split('.')[0]
        date_time_string = reduce(lambda y, z: string.replace(y, z, "_"),
                                  [date_time_string, ":", ".", " ", "-"])

        plot_name = 'neural_extraction_' + date_time_string + ".png"
        plot_path = os.path.join(self.output_dir, plot_name)

        plt.savefig(plot_path)

        symlink_name = os.path.join(self.output_dir,
                                    'latest_neural_extraction')
        make_sym_link(plot_name, symlink_name)

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

        plt.close(fig)
    def plot_simulation(self, target_keys):
        then = datetime.datetime.now()

        correct_key = None
        if target_keys:
            correct_key = target_keys[0]

        sim = self.simulator
        t = sim.trange()

        max_val = 5.0 / np.sqrt(self.dimension)

        gs = gridspec.GridSpec(7, 2)
        fig = plt.figure(figsize=(10, 10))

        ax = plt.subplot(gs[0, 0])

        plt.plot(t, self.data[self.D_probe], label='D')
        title = 'Input to associative memory'
        ax.text(.01, 1.20, title, horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-max_val, max_val))

        ax = plt.subplot(gs[0, 1])
        plt.plot(t, self.data[self.output_probe], label='Output')
        title = 'Output of associative memory'
        ax.text(.01, 1.20, title, horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-max_val, max_val))

        ax = plt.subplot(gs[1:3, :])

        if len(self.index_vectors) < 1000:
            for key, v in self.index_vectors.iteritems():
                input_sims = np.dot(self.data[self.D_probe], v)
                label = str(key[1])
                if key == correct_key:
                    plt.plot(t, input_sims, '--', label=label + '*')
                else:
                    plt.plot(t, input_sims, label=label)

            title = (
                'Dot product between id vectors and input to assoc memory.\n'
                'Target %s is dashed line.' % str(correct_key))

            ax.text(.01, 0.80, title, horizontalalignment='left',
                    transform=ax.transAxes)
            # plt.legend(bbox_to_anchor=(-0.03, 0.5), loc='center right')
            if self.ideal_dot:
                ax.text(.01, 0.10, "Ideal dot: " + str(self.ideal_dot),
                        horizontalalignment='left', transform=ax.transAxes)
            if self.second_dot:
                ax.text(.99, 0.10, "Second dot: " + str(self.second_dot),
                        horizontalalignment='right', transform=ax.transAxes)

            plt.ylim((-1.0, 1.5))
            plt.axhline(1.0, ls=':', c='k')

        ax = plt.subplot(gs[3:5, :])
        for key, p in self.assoc_probes.iteritems():
            if key == correct_key:
                plt.plot(t, self.data[p], '--')
            else:
                plt.plot(t, self.data[p])

        title = (
            'Decoded values of association populations.\n' +
            'Target %s is dashed line.' % str(correct_key))

        ax.text(.01, 0.80, title, horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-0.2, 1.5))
        plt.axhline(y=1.0, ls=':', c='k')

        ax = plt.subplot(gs[5:7, :])
        before_ls = '--'
        after_ls = '-'
        before_norms = [np.linalg.norm(v) for v in self.data[self.D_probe]]
        after_norms = [np.linalg.norm(v) for v in self.data[self.output_probe]]

        plt.plot(t, before_norms, before_ls, c='g', label='Norm - Before')
        plt.plot(t, after_norms, after_ls, c='g', label='Norm - After')

        if correct_key is not None:
            correct_index_hrr = HRR(data=self.index_vectors[correct_key])
            correct_stored_hrr = HRR(data=self.stored_vectors[correct_key])

            before_sims = [correct_index_hrr.compare(HRR(data=i))
                           for i in self.data[self.D_probe]]

            after_sims = [correct_stored_hrr.compare(HRR(data=o))
                          for o in self.data[self.output_probe]]

            plt.plot(t, before_sims, before_ls, c='b',
                     label='Cosine Sim - Before')
            plt.plot(t, after_sims, after_ls, c='b',
                     label='Cosine Sim - After')

        title = 'Before and After Associative Memory'
        ax.text(.01, 0.90, title, horizontalalignment='left',
                transform=ax.transAxes)
        plt.ylim((-1.0, 1.5))
        plt.legend(loc=4, prop={'size': 6})
        plt.axhline(y=1.0, ls=':', c='k')
        ax.set_xlabel('Time (s)')

        date_time_string = str(datetime.datetime.now()).split('.')[0]
        date_time_string = reduce(lambda y, z: string.replace(y, z, "_"),
                                  [date_time_string, ":", ".", " ", "-"])

        plot_name = 'neural_extraction_' + date_time_string + ".png"
        plot_path = os.path.join(self.output_dir, plot_name)

        plt.savefig(plot_path)

        symlink_name = os.path.join(
            self.output_dir, 'latest_neural_extraction')
        make_sym_link(plot_name, symlink_name)

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

        plt.close(fig)
    def findAllParents(self,
                       start_key,
                       target_key=None,
                       rtype=[],
                       use_HRR=False,
                       print_output=False):

        if print_output:
            print >> self.output_file, \
                "In find all parents, useHRR=", use_HRR

            print >> self.output_file, "Start:", start_key

            if target_key is not None:
                print >> self.output_file, "Target:", target_key

        use_vecs = use_HRR and self.extractor.return_vec

        level = 0
        if use_vecs:
            layerA = [self.semantic_pointers[start_key]]

            if target_key:
                target_vector = self.semantic_pointers[target_key]
                target_hrr = HRR(data=target_vector)
        else:
            layerA = [start_key]

        layerB = []
        parents = set()

        while len(layerA) > 0:
            word = layerA.pop()

            # test whether we've found the target
            found = False
            if use_vecs:
                word_hrr = HRR(data=word)
                found = target_hrr.compare(word_hrr) > self.decision_threshold
            else:
                found = word == target_key

            if found:
                if print_output:
                    print >> self.output_file, target_key, \
                        "found at level ", level

                return level

            if use_vecs:
                key = self.get_key_from_vector(word, self.semantic_pointers)
            else:
                key = word

            if key:
                if key in parents:
                    continue

                if level > 0:
                    parents.add(key)

                    if print_output:
                        print >> self.output_file, key, \
                            "found at level ", level

                links = []

                if not use_HRR:
                    links = [
                        r[1] for r in self.corpus_dict[word] if r[0] in rtype
                    ]
                else:

                    for symbol in rtype:
                        answers = [
                            r[1] for r in self.corpus_dict[key]
                            if r[0] == symbol
                        ]
                        relation_vec = self.relation_type_vectors[symbol]

                        if len(answers) == 0:
                            target = None
                        else:
                            target = answers[0]

                        relations = filter(
                            lambda x: x[0] in self.relation_type_vectors,
                            self.corpus_dict[key])

                        num_relations = len(relations)

                        if use_vecs:
                            result = self.test_link(
                                relation_vec,
                                word,
                                key,
                                target,
                                self.output_file,
                                return_vec=True,
                                depth=level,
                                num_relations=num_relations,
                                answers=answers)

                            links.append(result)

                        else:
                            results = self.test_link(
                                relation_vec,
                                None,
                                key,
                                target,
                                self.output_file,
                                return_vec=False,
                                depth=level,
                                num_relations=num_relations,
                                answers=answers)

                            if answers:
                                results = results[0]

                            links.extend(results)

                if len(links) > 0:
                    layerB.extend(links)

            if len(layerA) == 0:
                level = level + 1
                layerA = layerB
                layerB = []

        if target_key is None:
            return list(parents)
        else:
            return -1
    def find_matches(self, vector, vector_dict, exempt=[]):
        hrr_vec = HRR(data=vector)

        for key in vector_dict.keys():
            if key not in exempt:
                yield (key, hrr_vec.compare(HRR(data=vector_dict[key])))
Beispiel #7
0
    def findAllParents(self, start_key, target_key=None, rtype=[],
                       use_HRR=False, print_output=False):

        if print_output:
            print >> self.output_file, \
                "In find all parents, useHRR=", use_HRR

            print >> self.output_file, "Start:", start_key

            if target_key is not None:
                print >> self.output_file, "Target:", target_key

        use_vecs = use_HRR and self.extractor.return_vec

        level = 0
        if use_vecs:
            layerA = [self.semantic_pointers[start_key]]

            if target_key:
                target_vector = self.semantic_pointers[target_key]
                target_hrr = HRR(data=target_vector)
        else:
            layerA = [start_key]

        layerB = []
        parents = set()

        while len(layerA) > 0:
            word = layerA.pop()

            # test whether we've found the target
            found = False
            if use_vecs:
                word_hrr = HRR(data=word)
                found = target_hrr.compare(word_hrr) > self.decision_threshold
            else:
                found = word == target_key

            if found:
                if print_output:
                    print >> self.output_file, target_key, \
                        "found at level ", level

                return level

            if use_vecs:
                key = self.get_key_from_vector(word, self.semantic_pointers)
            else:
                key = word

            if key:
                if key in parents:
                    continue

                if level > 0:
                    parents.add(key)

                    if print_output:
                        print >> self.output_file, key, \
                            "found at level ", level

                links = []

                if not use_HRR:
                    links = [r[1] for r in self.corpus_dict[word]
                             if r[0] in rtype]
                else:

                    for symbol in rtype:
                        answers = [r[1] for r in self.corpus_dict[key]
                                   if r[0] == symbol]
                        relation_vec = self.relation_type_vectors[symbol]

                        if len(answers) == 0:
                            target = None
                        else:
                            target = answers[0]

                        relations = filter(
                            lambda x: x[0] in self.relation_type_vectors,
                            self.corpus_dict[key])

                        num_relations = len(relations)

                        if use_vecs:
                            result = self.test_link(
                                relation_vec, word, key, target,
                                self.output_file,
                                return_vec=True, depth=level,
                                num_relations=num_relations,
                                answers=answers)

                            links.append(result)

                        else:
                            results = self.test_link(
                                relation_vec, None, key, target,
                                self.output_file,
                                return_vec=False, depth=level,
                                num_relations=num_relations, answers=answers)

                            if answers:
                                results = results[0]

                            links.extend(results)

                if len(links) > 0:
                    layerB.extend(links)

            if len(layerA) == 0:
                level = level + 1
                layerA = layerB
                layerB = []

        if target_key is None:
            return list(parents)
        else:
            return -1
Beispiel #8
0
    def find_matches(self, vector, vector_dict, exempt=[]):
        hrr_vec = HRR(data=vector)

        for key in vector_dict.keys():
            if key not in exempt:
                yield (key, hrr_vec.compare(HRR(data=vector_dict[key])))