Пример #1
0
    def add(self, key, p):
        """Add the semantic pointer *p* to the vocabulary.

        Parameters
        ----------
        key : str
            Name of the Semantic Pointer. Must be a valid Python 2 identifier
            starting with a capital letter. Must not be *AbsorbingElement*,
            *Identity*, or *Zero*.
        p : SemanticPointer or array_like
            Semantic Pointer to add.
        """
        if (not valid_sp_regex.match(key) or iskeyword(key) or
                key in reserved_sp_names):
            raise SpaParseError(
                "Invalid Semantic Pointer name {!r}. Valid names are valid "
                "Python 2 identifiers beginning with a capital letter.".format(
                    key))
        if not isinstance(p, semantic_pointer.SemanticPointer):
            p = semantic_pointer.SemanticPointer(p, vocab=self)

        if key in self._key2idx:
            raise ValidationError("The semantic pointer %r already exists"
                                  % key, attr='', obj=self)
        if p.vocab is not None and p.vocab is not self:
            raise ValidationError(
                "Cannot add a semantic pointer that belongs to a different "
                "vocabulary.", attr='', obj=self)

        self._key2idx[key] = len(self._key2idx)
        self._keys.append(key)
        self._vectors = np.vstack([self._vectors, p.v])
Пример #2
0
 def __getitem__(self, key):
     """Return the semantic pointer with the requested name."""
     # __tracebackhide__ is used in py.test to hide stack frames from the
     # traceback. That means py.test might try to look up this attribute
     # in a test which will result in an exception hiding the actual
     # exception. By raising a KeyError we indicate that there is no
     # __tracebackhide__ attribute on this object and preserve the relevant
     # exception.
     if key == '__tracebackhide__':
         raise KeyError()
     if key in special_sps:
         return special_sps[key](self.dimensions, self)
     if not self.strict and key not in self:
         self.add(key, self.create_pointer())
     return semantic_pointer.SemanticPointer(
         self._vectors[self._key2idx[key]], vocab=self, name=key)
Пример #3
0
    def create_pointer(self, attempts=100, transform=None):
        """Create a new semantic pointer and add it to the vocabulary.

        This will take into account the `max_similarity` attribute.  If a
        pointer satisfying max_similarity is not generated after the specified
        number of attempts, the candidate pointer with lowest maximum cosine
        similarity with all existing pointers is returned.

        Parameters
        ----------
        attempts : int, optional
            Maximum number of attempts to create a Semantic Pointer not
            exceeding `max_similarity`.
        transform : str, optional
            A transform to apply to the generated vector. Needs to be the name
            of a method of `.SemanticPointer`. Currently, the only sensible
            value is 'unitary'.

        Returns
        -------
        SemanticPointer
            The generated Semantic Pointer.
        """
        best_p = None
        best_sim = np.inf
        for _ in range(attempts):
            p = semantic_pointer.SemanticPointer(
                next(self.pointer_gen), vocab=self)
            if transform is not None:
                p = eval('p.' + transform, dict(self), {'p': p})
            if len(self) == 0:
                best_p = p
                break
            else:
                p_sim = np.max(np.dot(self._vectors, p.v))
                if p_sim < best_sim:
                    best_p = p
                    best_sim = p_sim
                    if p_sim < self.max_similarity:
                        break
        else:
            warnings.warn(
                'Could not create a semantic pointer with '
                'max_similarity=%1.2f (D=%d, M=%d, similarity=%1.2f)'
                % (self.max_similarity, self.dimensions,
                   len(self._key2idx), best_sim))
        return best_p