Spatial Semantic Pointers¶
Continuous spatial representation using Fractional Power Encoding.
NEW in v1.2.0 - Based on Komer et al. (2019).
SpatialSemanticPointers¶
vsax.spatial.SpatialSemanticPointers
¶
Spatial Semantic Pointers for continuous spatial representation.
Encodes continuous spatial locations using fractional power encoding
S(x, y) = X^x ⊗ Y^y
This enables
- Encoding arbitrary spatial coordinates
- Binding objects to locations
- Querying "what is at location (x, y)?"
- Querying "where is object O?"
- Shifting entire scenes by a displacement vector
- Decoding approximate locations from encoded vectors
Based on Komer et al. 2019 which demonstrates that SSPs can represent continuous spatial relationships in a compositional, distributed manner.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
VSAModel instance (must use ComplexHypervector/FHRR). |
|
memory |
VSAMemory for storing axis basis vectors and named objects. |
|
config |
SSPConfig with spatial configuration. |
|
encoder |
FractionalPowerEncoder for encoding spatial coordinates. |
Example
import jax from vsax import create_fhrr_model, VSAMemory from vsax.spatial import SpatialSemanticPointers, SSPConfig
Create 2D spatial representation¶
model = create_fhrr_model(dim=512, key=jax.random.PRNGKey(0)) memory = VSAMemory(model) config = SSPConfig(dim=512, num_axes=2) # 2D space ssp = SpatialSemanticPointers(model, memory, config)
Encode a location¶
location = ssp.encode_location([3.5, 2.1]) # (x=3.5, y=2.1)
Bind object to location¶
memory.add("apple") scene = ssp.bind_object_location("apple", [3.5, 2.1])
Query: what is at location (3.5, 2.1)?¶
result = ssp.query_location(scene, [3.5, 2.1])
result should be similar to apple hypervector¶
See Also
- :class:
~vsax.encoders.FractionalPowerEncoder: Underlying encoder - Komer et al. 2019: "A neural representation of continuous space using fractional binding"
Source code in vsax/spatial/ssp.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
Functions¶
__init__(model, memory, config=None)
¶
Initialize Spatial Semantic Pointers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
VSAModel
|
VSAModel instance (must use ComplexHypervector). |
required |
memory
|
VSAMemory
|
VSAMemory for basis vectors and objects. |
required |
config
|
Optional[SSPConfig]
|
SSPConfig, defaults to 2D (512-dim) if not provided. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If model doesn't use ComplexHypervector. |
Source code in vsax/spatial/ssp.py
encode_location(coordinates)
¶
Encode a spatial location as a hypervector.
For 2D: S(x, y) = X^x ⊗ Y^y For 3D: S(x, y, z) = X^x ⊗ Y^y ⊗ Z^z
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
list[float]
|
List of coordinate values, one per axis. |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
ComplexHypervector representing the spatial location. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If coordinates length doesn't match num_axes. |
Example
location = ssp.encode_location([3.5, 2.1]) # 2D point location3d = ssp.encode_location([1.0, 2.0, 3.0]) # 3D point
Source code in vsax/spatial/ssp.py
bind_object_location(object_name, coordinates)
¶
Bind an object to a spatial location.
Creates: Object ⊗ S(x, y) = Object ⊗ X^x ⊗ Y^y
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_name
|
str
|
Name of object in memory. |
required |
coordinates
|
list[float]
|
Spatial coordinates for the object. |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
ComplexHypervector representing object-at-location. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If object_name not in memory. |
ValueError
|
If coordinates length doesn't match num_axes. |
Example
memory.add("apple") apple_at_pos = ssp.bind_object_location("apple", [3.5, 2.1])
Source code in vsax/spatial/ssp.py
query_location(scene, coordinates)
¶
Query what object is at a given location in the scene.
For scene containing Object ⊗ S(x, y), querying at (x, y) returns a vector similar to Object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scene
|
ComplexHypervector
|
Scene hypervector (typically a bundle of object-location pairs). |
required |
coordinates
|
list[float]
|
Location to query. |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
ComplexHypervector representing the object at that location. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If coordinates length doesn't match num_axes. |
Example
Create scene with apple at (3.5, 2.1)¶
scene = ssp.bind_object_location("apple", [3.5, 2.1])
Query: what's at (3.5, 2.1)?¶
result = ssp.query_location(scene, [3.5, 2.1])
result should be similar to memory["apple"]¶
Source code in vsax/spatial/ssp.py
query_object(scene, object_name)
¶
Query where an object is located in the scene.
For scene containing Object ⊗ S(x, y), querying for Object returns a vector similar to S(x, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scene
|
ComplexHypervector
|
Scene hypervector. |
required |
object_name
|
str
|
Name of object to locate. |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
ComplexHypervector representing the location of the object. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If object_name not in memory. |
Example
scene = ssp.bind_object_location("apple", [3.5, 2.1])
Query: where is the apple?¶
location_hv = ssp.query_object(scene, "apple")
location_hv should be similar to ssp.encode_location([3.5, 2.1])¶
Source code in vsax/spatial/ssp.py
shift_scene(scene, offset)
¶
Shift all objects in a scene by a displacement vector.
For scene S containing objects at various locations, shift by (dx, dy) moves all objects by that offset.
(Object ⊗ X^x ⊗ Y^y) ⊗ (X^dx ⊗ Y^dy) =
Object ⊗ X^(x+dx) ⊗ Y^(y+dy)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scene
|
ComplexHypervector
|
Scene hypervector to shift. |
required |
offset
|
list[float]
|
Displacement vector, one value per axis. |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
Shifted scene hypervector. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If offset length doesn't match num_axes. |
Example
Apple at (3.5, 2.1)¶
scene = ssp.bind_object_location("apple", [3.5, 2.1])
Shift scene by (1.0, -0.5)¶
shifted = ssp.shift_scene(scene, [1.0, -0.5])
Now apple is at (4.5, 1.6)¶
Source code in vsax/spatial/ssp.py
decode_location(location_hv, search_range, resolution=20)
¶
Decode approximate coordinates from a location hypervector.
Uses grid search to find coordinates that best match the encoded location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location_hv
|
ComplexHypervector
|
Encoded location hypervector to decode. |
required |
search_range
|
list[tuple[float, float]]
|
List of (min, max) tuples, one per axis. |
required |
resolution
|
int
|
Number of grid points to sample per axis (default: 20). |
20
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of decoded coordinates (approximate). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If search_range length doesn't match num_axes. |
Example
location_hv = ssp.encode_location([3.5, 2.1]) decoded = ssp.decode_location( ... location_hv, ... search_range=[(0.0, 5.0), (0.0, 5.0)], ... resolution=50 ... )
decoded should be close to [3.5, 2.1]¶
Source code in vsax/spatial/ssp.py
SSPConfig¶
vsax.spatial.SSPConfig
dataclass
¶
Configuration for Spatial Semantic Pointers.
Attributes:
| Name | Type | Description |
|---|---|---|
dim |
int
|
Dimensionality of hypervectors (e.g., 512, 1024). |
num_axes |
int
|
Number of spatial dimensions (1D, 2D, 3D, etc.). |
scale |
Optional[float]
|
Optional scaling factor for spatial coordinates. |
axis_names |
Optional[list[str]]
|
Optional custom names for axes (defaults to ["x", "y", "z", ...]). |
Source code in vsax/spatial/ssp.py
Functions¶
__post_init__()
¶
Set default axis names if not provided.
Source code in vsax/spatial/ssp.py
Utilities¶
create_spatial_scene¶
vsax.spatial.utils.create_spatial_scene(ssp, objects_and_locations)
¶
Create a scene by bundling multiple object-location bindings.
Convenience function that: 1. Binds each object to its location: Object_i ⊗ S(x_i, y_i) 2. Bundles all bindings into a single scene hypervector
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ssp
|
SpatialSemanticPointers
|
SpatialSemanticPointers instance. |
required |
objects_and_locations
|
dict[str, list[float]]
|
Dictionary mapping object names to coordinates. Example: {"apple": [1.0, 2.0], "banana": [3.0, 4.0]} |
required |
Returns:
| Type | Description |
|---|---|
ComplexHypervector
|
Scene hypervector containing all object-location pairs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If objects_and_locations is empty. |
KeyError
|
If any object name is not in ssp.memory. |
Example
scene = create_spatial_scene(ssp, { ... "apple": [1.0, 2.0], ... "banana": [3.0, 4.0], ... "cherry": [5.0, 1.0] ... })
Query: what's at (1.0, 2.0)?¶
result = ssp.query_location(scene, [1.0, 2.0])
Source code in vsax/spatial/utils.py
similarity_map_2d¶
vsax.spatial.utils.similarity_map_2d(ssp, query_hv, x_range, y_range, resolution=50)
¶
Generate 2D similarity heatmap for visualization.
Computes similarity between query_hv and encoded locations across a 2D grid. Useful for visualizing "where" an object is located, or what regions match a given hypervector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ssp
|
SpatialSemanticPointers
|
SpatialSemanticPointers instance (must be 2D). |
required |
query_hv
|
ComplexHypervector
|
Query hypervector to compare against locations. |
required |
x_range
|
tuple[float, float]
|
(min_x, max_x) range for grid. |
required |
y_range
|
tuple[float, float]
|
(min_y, max_y) range for grid. |
required |
resolution
|
int
|
Number of grid points per axis (default: 50). |
50
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
Tuple of (X, Y, similarities): - X: 2D meshgrid of x coordinates (resolution x resolution) - Y: 2D meshgrid of y coordinates (resolution x resolution) - similarities: 2D array of similarity values (resolution x resolution) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If ssp is not 2D (num_axes != 2). |
Example
Create scene with apple at (3.5, 2.1)¶
scene = ssp.bind_object_location("apple", [3.5, 2.1])
Query where apple is¶
apple_location = ssp.query_object(scene, "apple")
Generate heatmap¶
X, Y, sims = similarity_map_2d( ... ssp, apple_location, ... x_range=(0, 5), y_range=(0, 5), resolution=50 ... )
Peak should be near (3.5, 2.1)¶
Source code in vsax/spatial/utils.py
plot_ssp_2d_scene¶
vsax.spatial.utils.plot_ssp_2d_scene(ssp, scene, object_names, x_range=(0, 5), y_range=(0, 5), resolution=30, figsize=(12, 4))
¶
Plot 2D scene showing where each object is located.
Creates a figure with subplots showing similarity heatmaps for each object. Requires matplotlib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ssp
|
SpatialSemanticPointers
|
SpatialSemanticPointers instance (must be 2D). |
required |
scene
|
ComplexHypervector
|
Scene hypervector containing object-location bindings. |
required |
object_names
|
list[str]
|
List of object names to visualize. |
required |
x_range
|
tuple[float, float]
|
(min_x, max_x) for plot (default: (0, 5)). |
(0, 5)
|
y_range
|
tuple[float, float]
|
(min_y, max_y) for plot (default: (0, 5)). |
(0, 5)
|
resolution
|
int
|
Grid resolution (default: 30). |
30
|
figsize
|
tuple[int, int]
|
Figure size (default: (12, 4)). |
(12, 4)
|
Returns:
| Type | Description |
|---|---|
Any
|
Matplotlib Figure object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If matplotlib is not installed. |
ValueError
|
If ssp is not 2D. |
KeyError
|
If any object name is not in ssp.memory. |
Example
import matplotlib.pyplot as plt scene = create_spatial_scene(ssp, { ... "apple": [1.0, 2.0], ... "banana": [3.0, 4.0] ... }) fig = plot_ssp_2d_scene(ssp, scene, ["apple", "banana"]) plt.show()
Source code in vsax/spatial/utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
region_query¶
vsax.spatial.utils.region_query(ssp, scene, object_names, center, radius, resolution=20)
¶
Find which objects are within a spatial region.
Searches a circular region (2D) or spherical region (3D) around a center point and returns objects with high similarity to locations in that region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ssp
|
SpatialSemanticPointers
|
SpatialSemanticPointers instance. |
required |
scene
|
ComplexHypervector
|
Scene hypervector. |
required |
object_names
|
list[str]
|
List of candidate object names to check. |
required |
center
|
list[float]
|
Center coordinates of the search region. |
required |
radius
|
float
|
Radius of the search region. |
required |
resolution
|
int
|
Number of sample points to check in the region (default: 20). |
20
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary mapping object names to maximum similarity scores. |
dict[str, float]
|
Higher scores indicate the object is likely in the region. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If center length doesn't match num_axes. |
KeyError
|
If any object name is not in ssp.memory. |
Example
scene = create_spatial_scene(ssp, { ... "apple": [1.0, 1.0], ... "banana": [3.0, 3.0], ... "cherry": [5.0, 5.0] ... })
Search region around (3.0, 3.0) with radius 0.5¶
results = region_query( ... ssp, scene, ["apple", "banana", "cherry"], ... center=[3.0, 3.0], radius=0.5 ... )
results["banana"] should be highest¶
Source code in vsax/spatial/utils.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |