File size: 2,374 Bytes
361a6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Vector-Symbolic Architecture and Holographic Binding.



Implements circular convolution binding and circular correlation unbinding

using Real Fast Fourier Transforms (rfft/irfft):

z = x (*) y = irfft(rfft(x) * rfft(y))

x_approx = z (*) y_inv = irfft(rfft(z) * conj(rfft(y)))



Enables compositional neural symbolic binding of roles, values, and concepts.

"""
from __future__ import annotations

import numpy as np
import torch
import torch.nn as nn
import torch.fft


class VectorSymbolicBinder:
    """Holographic vector symbolic operator supporting binding, unbinding, and superposition."""

    @staticmethod
    def bind(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        """Circular convolution binding: z = x (*) y."""
        dim = x.shape[-1]
        x_fft = torch.fft.rfft(x, n=dim)
        y_fft = torch.fft.rfft(y, n=dim)
        z = torch.fft.irfft(x_fft * y_fft, n=dim)
        # Unit norm normalization
        norm = torch.norm(z, dim=-1, keepdim=True) + 1e-8
        return z / norm

    @staticmethod
    def unbind(z: torch.Tensor, key: torch.Tensor) -> torch.Tensor:
        """Circular correlation unbinding: x_approx = z (*) key*."""
        dim = z.shape[-1]
        z_fft = torch.fft.rfft(z, n=dim)
        k_fft = torch.fft.rfft(key, n=dim)
        # Correlation in frequency domain is multiplication by complex conjugate of key
        x_approx = torch.fft.irfft(z_fft * torch.conj(k_fft), n=dim)
        norm = torch.norm(x_approx, dim=-1, keepdim=True) + 1e-8
        return x_approx / norm

    @staticmethod
    def bundle(vectors: list[torch.Tensor], weights: list[float] | None = None) -> torch.Tensor:
        """Vector superposition / addition: s = sum w_i * v_i."""
        if not vectors:
            raise ValueError("Empty vector list for bundling.")
        if weights is None:
            weights = [1.0] * len(vectors)

        s = sum(w * v for w, v in zip(weights, vectors))
        norm = torch.norm(s, dim=-1, keepdim=True) + 1e-8
        return s / norm

    @staticmethod
    def similarity(x: torch.Tensor, y: torch.Tensor) -> float:
        """Cosine similarity between two representations."""
        norm_x = torch.norm(x) + 1e-8
        norm_y = torch.norm(y) + 1e-8
        return float((torch.dot(x.flatten(), y.flatten()) / (norm_x * norm_y)).item())