tbp.monty.attention#
tbp.monty.attention.attention_system#
- class DefaultAttentionSystem(voxel_size: float = 0.05, weight_pooler: WeightPooler = <function negative_priority_max_pool>, decay: VoxelGridWeightDecay | None = None, merge: VoxelGridMerge | None = None, goal_filter: GoalFilter | None = None, telemetry: AttentionSystemTelemetry | None = None) None[source]#
Bases:
AttentionSystem- __init__(voxel_size: float = 0.05, weight_pooler: WeightPooler = <function negative_priority_max_pool>, decay: VoxelGridWeightDecay | None = None, merge: VoxelGridMerge | None = None, goal_filter: GoalFilter | None = None, telemetry: AttentionSystemTelemetry | None = None) None[source]#
tbp.monty.attention.decay#
- class LinearWeightDecay(rate: float = 0.1) None[source]#
Bases:
VoxelGridWeightDecayMove each voxel weight toward zero by a fixed amount.
Decay is applied in place to the grid’s weights. Weights that fall within [-rate, +rate] get clamped to zero.
- class NoopDecay(*args, **kwargs)[source]#
Bases:
VoxelGridWeightDecayLeave every voxel weight untouched.
- DEFAULT_LINEAR_WEIGHT_DECAY_RATE = 0.1#
Default per-step decay toward zero for linear decay.
tbp.monty.attention.goal_filter#
- class HardGoalFilter(*args, **kwargs)[source]#
Bases:
GoalFilterKeep only the goals that fall within a positively weighted voxel.
- Rules:
If the attention grid is empty, let all goals through. Otherwise…
- If all voxels are inhibited (negative-weighted), allow all goals to pass
that do not land in negative voxels.
If a goal does not fall within a voxel, filter it out. Otherwise…
- If the goals’ voxel weight is <= 0, filter the goal out. Otherwise,
let it through.
- class NoopGoalFilter(*args, **kwargs)[source]#
Bases:
GoalFilterPass every goal through unchanged.
tbp.monty.attention.merge#
- class Union(*args, **kwargs)[source]#
Bases:
VoxelGridMergeMerge grid_b into grid_a, taking the union of their voxels.
Every voxel occupied in either grid is occupied in the result. Where the grids overlap, grid_b’s weight wins.
tbp.monty.attention.telemetry#
- class DefaultAttentionSystemTelemetry None[source]#
Bases:
AttentionSystemTelemetry
- class NoopAttentionSystemTelemetry(*args, **kwargs)[source]#
Bases:
AttentionSystemTelemetry
tbp.monty.attention.voxel_grid#
- class VoxelGrid(voxel_size: float, voxels: list[Voxel], weights: npt.NDArray[np.floating]) None[source]#
Bases:
objectA sparse grid of per-voxel features over 3D space.
Backed by a pandas DataFrame whose rows are the occupied voxels – an (x, y, z) integer MultiIndex of voxel coordinates (lower corners) – and a “weight” column.
- __init__(voxel_size, voxels, weights)[source]#
Initialize the voxel grid.
- Parameters:
voxel_size – Edge length of a voxel.
voxels – The occupied voxels.
weights – The weights of the occupied voxels.
- classmethod from_pandas(voxel_size: float, data: pandas.DataFrame) VoxelGrid[source]#
Create a voxel grid from a pandas DataFrame.
- to_pandas() pandas.DataFrame[source]#
Return the backing frame, not a copy.
- Return type:
- weights() numpy.typing.NDArray.numpy.floating[source]#
Return the weights of the occupied voxels.
- Return type:
floating
- weights_at_points(points: numpy.typing.NDArray.numpy.floating, fill_value: float = numpy.nan) numpy.typing.NDArray.numpy.floating[source]#
Look up voxel’s weight at each point.
- Parameters:
points (
floating) – A (N, 3) array of points.fill_value (
float) – The value reported for points whose voxel is not in the grid; defaults to NaN.
- Return type:
floating- Returns:
An (N,) array of weights;
fill_valueis used for any points in unoccupied voxels.
- encode_voxel_grid(grid: VoxelGrid) dict[source]#
Encode a voxel grid into a JSON-encodable dictionary.
- validate_dataframe(df: pandas.DataFrame) None[source]#
Validate the structure of the voxel grid DataFrame.
- Parameters:
df (
DataFrame) – The DataFrame to validate.- Raises:
ValueError – If -
dfdoes not have a MultiIndex with level names (‘x’, ‘y’, ‘z’). -df’s index is not unique. -dfdoes not have a “weight” column. -df’s “weight” column is not 1D.- Return type:
- voxelize_and_bin_points(voxel_size: float, points: numpy.typing.NDArray.numpy.floating, weights: numpy.typing.NDArray.numpy.floating) pandas.DataFrame[source]#
Tabulate each point’s voxel alongside its weight.
The table is the input to per-voxel aggregation: grouping it by
voxelbins the points, with each group’s index giving the indices of the points inside that voxel.- Parameters:
voxel_size (
float) – Edge length of a voxel.points (
floating) – An (N, 3) array of points.weights (
floating) – An (N,) array of weights, aligned withpoints.
- Raises:
ValueError – If
weightsis not an (N,) array.- Return type:
- Returns:
A frame with one row per point, holding the point’s
voxelandweight.
- voxelize_points(voxel_size: float, points: npt.NDArray[np.floating]) list[Voxel][source]#
Find the voxel containing each point.
Voxels are half-open: a point on the face shared by two voxels belongs to the one with the larger coordinate, i.e. voxel v spans [v, v + 1) along each axis, in units of
voxel_size.- Parameters:
points – An (N, 3) array of points.
voxel_size – Edge length of a voxel.
- Returns:
The voxel of each point, in point order.
- Raises:
ValueError – If
pointsis not an (N, 3) array.
- VOXEL_LEVELS = ('x', 'y', 'z')#
Names of the row index levels. This is for a pandas DataFrame MultiIndex.