def __rrshift__(self, other): if not isinstance(other, Node): return NotImplemented if self.routed_mode: return RoutedConnection(other, self) else: infer_types(self, other) other.connect_to(self.input)
def dot(self, other): """Return the dot product of the two vectors.""" if isinstance(other, Fixed): infer_types(self, other) other = other.evaluate().v if is_array_like(other): return np.dot(self.v, other) else: return other.dot(self)
def compare(self, other): """Return the similarity between two SemanticPointers. This is the normalized dot product, or (equivalently), the cosine of the angle between the two vectors. """ if isinstance(other, SemanticPointer): infer_types(self, other) other = other.evaluate().v scale = np.linalg.norm(self.v) * np.linalg.norm(other) if scale == 0: return 0 return np.dot(self.v, other) / scale
def _mul_with_fixed(self, other, swap_inputs=False): infer_types(self, other) if other.type == TScalar: tr = other.value elif self.type == TScalar and other.type == TAnyVocab: raise SpaTypeError( "Cannot infer vocabulary for fixed pointer when multiplying " "with scalar.") elif isinstance(other.type, TVocabulary): if self.type == TScalar: tr = other.evaluate().v else: tr = other.evaluate().get_binding_matrix( swap_inputs=swap_inputs) else: raise AssertionError("Unexpected node type in multiply.") return Transformed(self, tr, self.type)
def fractional_bind(self, other): """Return the fractional binding of a SemanticPointer.""" type_ = infer_types(self) vocab = None if type_ == TAnyVocab else type_.vocab a, b = self.v, other return SemanticPointer(data=self.algebra.fractional_bind(a, b), vocab=vocab, algebra=self.algebra, name=self._get_binary_name(other, "**", False))
def dot(self, other): type_ = infer_types(self, other) if self.type == TScalar or other.type == TScalar: raise SpaTypeError("Cannot do a dot product with a scalar.") if isinstance(other, Fixed): tr = np.atleast_2d(other.evaluate().v) return Transformed(self, tr, TScalar) else: net = DotProductRealization(type_.vocab) self.connect_to(net.input_a) other.connect_to(net.input_b) return ModuleOutput(net.output, TScalar)
def _bind(self, other, swap=False): type_ = infer_types(self, other) vocab = None if type_ == TAnyVocab else type_.vocab if vocab is None: self._ensure_algebra_match(other) other_pointer = other.evaluate() a, b = self.v, other_pointer.v if swap: a, b = b, a return SemanticPointer(data=self.algebra.bind(a, b), vocab=vocab, algebra=self.algebra, name=self._get_binary_name( other_pointer, "*", swap))
def _mul_with_dynamic(self, other, swap_inputs=False): type_ = infer_types(self, other) if type_ == TScalar: mul = ProductRealization() input_left, input_right = mul.input_a, mul.input_b elif self.type == TScalar or other.type == TScalar: raise NotImplementedError( "Dynamic scaling of semantic pointer not implemented.") else: mul = BindRealization(self.type.vocab) input_left, input_right = mul.input_left, mul.input_right if swap_inputs: a, b = other, self else: a, b = self, other a.connect_to(input_left) b.connect_to(input_right) return ModuleOutput(mul.output, type_)
def __add__(self, other): type_ = infer_types(self, other) return Summed((self, other), type_)
def __init__(self, source, sink): self.type = infer_types(source, sink) self.source = source self.sink = sink RoutedConnection.free_floating.add(self)
def __sub__(self, other): other = as_symbolic_node(other) if not isinstance(other, PointerSymbol): return NotImplemented type_ = infer_types(self, other) return PointerSymbol(self.expr + '-' + other.expr, type_)
def dot(self, other): other = as_symbolic_node(other) if not isinstance(other, PointerSymbol): return NotImplemented infer_types(self, other) return FixedScalar(self.evaluate().dot(other.evaluate()))
def __rmul__(self, other): type_ = infer_types(self, other) return PointerSymbol(other.expr + '*' + self.expr, type_)
def mse(self, other): """Return the mean-squared-error between two vectors.""" if isinstance(other, SemanticPointer): infer_types(self, other) other = other.evaluate().v return np.sum((self.v - other)**2) / len(self.v)
def __mul__(self, other): type_ = infer_types(self, other) return PointerSymbol(self.expr + "*" + other.expr, type_)